Copy CodeThe code is as follows:
/**
* Insert Insertmodel (), use reflection, slightly less efficient
* @param class $model Object
* @param bool $is _returnlastinsertid whether to return add ID
* @return int is returned by default, $is _returnlastinsertid is true to return the Add ID
*/
Public Function Insertmodel ($model, $is _returnlastinsertid=false) {
try {
Require_once dirname (dirname (__file__)). ' \models\basemodel.php ';
if (!is_subclass_of ($model, "Basemodel")) {
Exit ($this->geterror (__function__, __line__));
}
$className =get_class ($model);
$tName = $this->formattabname ($className);
$reflectionClass =new Reflectionclass ($className);
$properties = $reflectionClass->getproperties ();
unset ($properties [0]);
$fields = "";
$vals = "";
foreach ($properties as $property) {
$pName = $property->getname ();
$fields. = $pName. ",";
$vals. = ' \ '. $model, $pName. ' \''.',';
}
$fields =rtrim ($fields, ', ');
$vals =rtrim ($vals, ', ');
$this->sql = "INSERT INTO {$tName} ({$fields}) VALUES ({$vals})";
if ($is _returnlastinsertid) {
$this->conn->exec ($this->sql);
$lastId = (int) $this->conn->lastinsertid ();
return $lastId;
} else {
$row = $this->conn->exec ($this->sql);
return $row;
}
} catch (Exception $exc) {
echo $exc->getmessage ();
}
}
http://www.bkjia.com/PHPjc/740206.html www.bkjia.com true http://www.bkjia.com/PHPjc/740206.html techarticle Copy the code as follows:/** * Insert Insertmodel (), use reflection, less efficient * @param class $model Object * @param bool $is _returnlastinsertid returns the Add ID * @r Eturn ...