色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

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__)).'/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();
        }
    }

php技術php使用反射插入對象示例分享,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 惠州市| 名山县| 云浮市| 房产| 仁怀市| 长治市| 瓮安县| 南京市| 章丘市| 保定市| 临沭县| 精河县| 耒阳市| 仙桃市| 托克托县| 巧家县| 澄江县| 土默特右旗| 铜川市| 徐汇区| 泗阳县| 萨嘎县| 昌邑市| 黎平县| 扎囊县| 缙云县| 黔江区| 磐安县| 鸡泽县| 称多县| 错那县| 海伦市| 黑龙江省| 承德市| 抚顺市| 色达县| 六安市| 潮安县| 百色市| 论坛| 泰和县|