diff --git a/app/frontend/controllers/ModelController.php b/app/frontend/controllers/ModelController.php index 636be8b..7a3922b 100644 --- a/app/frontend/controllers/ModelController.php +++ b/app/frontend/controllers/ModelController.php @@ -189,4 +189,62 @@ public function batchInsertAction(){ ]); var_dump($result); } + + public function updateAction(){ +// $articleModel = new ArticlesModel(); +// $article = $articleModel->findFirst([ +// 'conditions' => 'aid = :aid:', +// 'bind' => [ +// 'aid' => 3 +// ], +// ]); +// if($article) { +// $result = $article->update([ +// 'title' => 'Phalcon更新测试', +// ]); +// var_dump($result); +// } + +// $articleModel = new ArticlesModel(); +// $articleModel->aid = 3; +// $result = $articleModel->update([ +// 'title' => 'Phalcon更新测试', +// 'introduce' => "Phalcon入门教程2", +// 'status' => 2, +// 'view_number' => 2, +// 'is_recommend' => 1, +// 'is_top' => 1, +// 'create_by' => 1, +// 'create_time' => '2017-07-20 00:00:00', +// 'modify_by' => 1, +// 'modify_time' => '2017-07-20 00:00:00', +// ]); +// if(!$result){ +// throw new \Exception('数据更新失败'); +// } +// $affectedRows = $this->getDI()->get('db')->affectedRows(); +// var_dump($result); +// var_dump($affectedRows); + + $articleModel = new ArticlesModel(); + $articleModel->aid = 3; + $result = $articleModel->iupdate([ + 'title' => 'Phalcon更新测试', + ]); + if(!$result){ + throw new \Exception('数据更新失败'); + } + $affectedRows = $this->getDI()->get('db')->affectedRows(); + var_dump($result); + var_dump($affectedRows); + } + + public function deleteAction(){ + $articleModel = new ArticlesModel(); + $articleModel->aid = 4; + $result = $articleModel->delete(); + $affectedRows = $this->getDI()->get('db')->affectedRows(); + var_dump($result); + var_dump($affectedRows); + } } \ No newline at end of file diff --git a/app/frontend/models/ArticlesModel.php b/app/frontend/models/ArticlesModel.php index a445b29..0de497c 100644 --- a/app/frontend/models/ArticlesModel.php +++ b/app/frontend/models/ArticlesModel.php @@ -49,4 +49,19 @@ public function batch_insert(array $data){ } return $result; } + + /** + * 封装phalcon model的update方法,实现仅更新数据变更字段,而非所有字段更新 + * @param array|null $data + * @param null $whiteList + * @return bool + */ + public function iupdate(array $data = null, $whiteList = null) + { + if (count($data) > 0) { + $attributes = $this->getModelsMetaData()->getAttributes($this); + $this->skipAttributesOnUpdate(array_diff($attributes, array_keys($data))); + } + return parent::update($data, $whiteList); + } } \ No newline at end of file