search
Homephp教程PHP源码在php中使用反射插入对象

在php中使用反射插入对象

/**
 * 插入insertModel(),利用反射,效率稍差 
 * @param class $model 对象
 * @param bool $is_returnLastInsertId 是否返回添加ID 
 * @return int 默认返回成功与否,$is_returnLastInsertId 为true,返回添加ID 
*/
 public function insertModel($model,$is_returnLastInsertId=FALSE) {
 try {
 require_once dirname(dirname(__FILE__)).'ModelsBaseModel.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();
}
}

                       


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.