php使用反射插入对象示例分享

(编辑:jimmy 日期: 2024/9/28 浏览:2)

复制代码 代码如下:
/** 
    * 插入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__)).'\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();
        }
    }

一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。