From 97169ebd5afea81a8797cf079ba1748304ea32fb Mon Sep 17 00:00:00 2001 From: root Date: Thu, 20 Jul 2017 15:59:53 +0800 Subject: [PATCH] =?UTF-8?q?Phalcon=E5=85=A5=E9=97=A8=E6=95=99=E7=A8=8B?= =?UTF-8?q?=E4=B9=8B=E6=A8=A1=E5=9E=8BCURD(2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/frontend/controllers/ModelController.php | 58 ++++++++++++++++++++ app/frontend/models/ArticlesModel.php | 15 +++++ 2 files changed, 73 insertions(+) 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