【发布时间】:2015-05-11 09:12:23
【问题描述】:
我是 CakePHP 的新开发者,我对这个案例感到困惑:
现在我有一个模型用户表用户,我想检查 id = 4 的记录是否存在:如果记录存在,则返回它,如果不返回消息错误,我认为有两种方法:
解决方案 1:
$this->Model->id = 4;
if ($this->Model->exists()) {
return $this->Model->read();
} else {
return "ERROR";
}
解决方案 2:
$data = $this->Model->find('first', array(
'conditions' => array('Model.id' => 4)
));
if (!empty($data)) {
return $data;
} else {
return "ERROR";
}
我不知道什么更好或更优化(我认为在解决方案 1 中,Cake 会进行 2 次查询,是吗?),请给我答案以获得最佳解决方案。抱歉我的英语不好。
【问题讨论】: