Skip to content

Commit

Permalink
Phalcon入门教程之模型CURD(2)
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Jul 20, 2017
1 parent 9748475 commit 97169eb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
58 changes: 58 additions & 0 deletions app/frontend/controllers/ModelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
15 changes: 15 additions & 0 deletions app/frontend/models/ArticlesModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 97169eb

Please sign in to comment.