[李景山php]每天TP5-20170111|thinkphp5-Model.php-4
生活随笔
收集整理的這篇文章主要介紹了
[李景山php]每天TP5-20170111|thinkphp5-Model.php-4
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/***?設置需要追加的輸出屬性*?@access?public*?@param?array?$append?屬性列表*?@return?$this*/
public?function?append($append?=?[])
{$this->append?=?$append;//?屬性追加return?$this;
}/***?設置需要隱藏的輸出屬性*?@access?public*?@param?array?$hidden?屬性列表*?@return?$this*/
public?function?hidden($hidden?=?[])
{$this->hidden?=?$hidden;//?屬性隱藏return?$this;
}/***?設置需要輸出的屬性*?@param?array?$visible*?@return?$this*/
public?function?visible($visible?=?[])
{$this->visible?=?$visible;//?輸出的屬性return?$this;
}/***?轉換當前模型對象為數組*?@access?public*?@return?array*/
public?function?toArray()
{$item?=?[];//過濾屬性if?(!empty($this->visible))?{$data?=?array_intersect_key($this->data,?array_flip($this->visible));}?elseif?(!empty($this->hidden))?{$data?=?array_diff_key($this->data,?array_flip($this->hidden));}?else?{$data?=?$this->data;}foreach?($data?as?$key?=>?$val)?{if?($val?instanceof?Model?||?$val?instanceof?Collection)?{//?關聯模型對象$item[$key]?=?$val->toArray();}?elseif?(is_array($val)?&&?reset($val)?instanceof?Model)?{//?關聯模型數據集$arr?=?[];foreach?($val?as?$k?=>?$value)?{$arr[$k]?=?$value->toArray();}$item[$key]?=?$arr;}?else?{//?模型屬性$item[$key]?=?$this->getAttr($key);}}//?追加屬性(必須定義獲取器)if?(!empty($this->append))?{foreach?($this->append?as?$name)?{$item[$name]?=?$this->getAttr($name);}}return?!empty($item)???$item?:?[];
}//?轉換為數組/***?轉換當前模型對象為JSON字符串*?@access?public*?@param?integer???$options?json參數*?@return?string*/
public?function?toJson($options?=?JSON_UNESCAPED_UNICODE)
{return?json_encode($this->toArray(),?$options);//?轉換wield?json
}/***?獲取模型對象的主鍵*?@access?public*?@param?string?$name?模型名*?@return?mixed*/
public?function?getPk($name?=?'')
{//?獲取主鍵if?(!empty($name))?{$table?=?$this->db()->getTable($name);return?$this->db()->getPk($table);}?elseif?(empty($this->pk))?{$this->pk?=?$this->db()->getPk();}return?$this->pk;
}/***?判斷一個字段名是否為主鍵字段*?@access?public*?@param?string?$key?名稱*?@return?bool*/
protected?function?isPk($key)
{//?檢測是否為主鍵$pk?=?$this->getPk();if?(is_string($pk)?&&?$pk?==?$key)?{return?true;}?elseif?(is_array($pk)?&&?in_array($key,?$pk))?{return?true;}return?false;
}/***?保存當前數據對象*?@access?public*?@param?array?????$data?數據*?@param?array?????$where?更新條件*?@param?string????$sequence?????自增序列名*?@return?integer|false*/
public?function?save($data?=?[],?$where?=?[],?$sequence?=?null)
{//?數據保存if?(!empty($data))?{//?數據不為空//?數據自動驗證if?(!$this->validateData($data))?{return?false;}//?數據對象賦值foreach?($data?as?$key?=>?$value)?{//?數據對象賦值$this->setAttr($key,?$value,?$data);}if?(!empty($where))?{//?條件不為空?代表為?更新狀態$this->isUpdate?=?true;}}//?檢測字段if?(!empty($this->field))?{//?字段不為空$this->db();foreach?($this->data?as?$key?=>?$val)?{//?刪除數據里面的?多有的字段信息if?(!in_array($key,?$this->field))?{unset($this->data[$key]);}}}//?數據自動完成$this->autoCompleteData($this->auto);//?數據自動完成//?自動寫入更新時間if?($this->autoWriteTimestamp?&&?$this->updateTime)?{//?時間屬性設置$this->setAttr($this->updateTime,?null);}//?事件回調if?(false?===?$this->trigger('before_write',?$this))?{//?回調return?false;}if?($this->isUpdate)?{//?執行更新//?自動更新$this->autoCompleteData($this->update);//?事件回調if?(false?===?$this->trigger('before_update',?$this))?{return?false;}//?去除沒有更新的字段$data?=?[];foreach?($this->data?as?$key?=>?$val)?{if?(in_array($key,?$this->change)?||?$this->isPk($key))?{$data[$key]?=?$val;}}if?(!empty($this->readonly))?{//?只讀字段不允許更新foreach?($this->readonly?as?$key?=>?$field)?{if?(isset($data[$field]))?{unset($data[$field]);}}}if?(empty($where)?&&?!empty($this->updateWhere))?{$where?=?$this->updateWhere;}if?(!empty($where))?{$pk?=?$this->getPk();if?(is_string($pk)?&&?isset($data[$pk]))?{unset($data[$pk]);}}$result?=?$this->db()->where($where)->update($data);//?還是使用數據庫?模型的更新//?清空change$this->change?=?[];//?更新回調$this->trigger('after_update',?$this);}?else?{//?自動寫入$this->autoCompleteData($this->insert);//?自動寫入創建時間if?($this->autoWriteTimestamp?&&?$this->createTime)?{$this->setAttr($this->createTime,?null);}if?(false?===?$this->trigger('before_insert',?$this))?{return?false;}$result?=?$this->db()->insert($this->data);//?數據模型的插入//?獲取自動增長主鍵if?($result)?{$insertId?=?$this->db()->getLastInsID($sequence);//?獲取自動增長的主鍵$pk???????=?$this->getPk();if?(is_string($pk)?&&?$insertId)?{$this->data[$pk]?=?$insertId;}}//?標記為更新$this->isUpdate?=?true;//?清空change$this->change?=?[];//?新增回調$this->trigger('after_insert',?$this);}//?寫入回調$this->trigger('after_write',?$this);return?$result;
}/***?保存多個數據到當前數據對象*?@access?public*?@param?array?????$dataSet?數據*?@param?boolean???$replace?是否自動識別更新和寫入*?@return?array|false*/
public?function?saveAll($dataSet,?$replace?=?true)//?基于事務的?批量插入及更新
{if?($this->validate)?{//批量數據驗證//?數據批量驗證$validate?=?$this->validate;foreach?($dataSet?as?$data)?{if?(!$this->validate($validate)->validateData($data))?{return?false;}}}$result?=?[];$db?????=?$this->db();$db->startTrans();//?啟動事務try?{$pk?=?$this->getPk();if?(is_string($pk)?&&?$replace)?{$auto?=?true;}foreach?($dataSet?as?$key?=>?$data)?{if?(!empty($auto)?&&?isset($data[$pk]))?{$result[$key]?=?self::update($data);}?else?{$result[$key]?=?self::create($data);}}$db->commit();//?提交事務return?$result;}?catch?(\Exception?$e)?{$db->rollback();//?否則回滾throw?$e;}
}
轉載于:https://blog.51cto.com/jingshanls/1880589
總結
以上是生活随笔為你收集整理的[李景山php]每天TP5-20170111|thinkphp5-Model.php-4的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: How To Make JMeter B
- 下一篇: 初见shell,设置端口参数