1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
<?php
/**
* Database query class for INSERT queries
*
* This file is part of Zoph.
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Zoph
* @author Jeroen Roos
*/
namespace db;
/**
* The insert object is used to create INSERT queries
*
* @package Zoph
* @author Jeroen Roos
*/
class insert extends query {
/** @var array Array of fields to be SET in INSERT query */
private $set=array();
/**
* Create INSERT query
* @return string SQL query
*/
public function __toString() {
$sql = "INSERT INTO " . $this->table;
$fields=array();
$values=array();
foreach ($this->getParams() as $param) {
$fields[]=substr($param->getName(), 1);
$values[]=$param->getName();
}
foreach ($this->set as $name => $value) {
$fields[]=$name;
$values[]=$value;
}
$sql.=" (`" . implode("`, `", $fields) . "`)";
$sql.=" VALUES(" . implode(", ", $values) . ") ";
return $sql . ";";
}
/**
* Add a SET statement to this query
* @param string name of the field
* @param string value of the field
*/
public function addSet($name, $value) {
$this->set[$name]=$value;
}
/**
* Execute query and return new id
* @return int ID
*/
public function execute() {
parent::execute();
return db::getHandle()->lastInsertId();
}
}
|