我想知道是否可以使用类似于以下内容的方法从数据库中检索行:
if (!empty($this->params['form'])) {
$place = array();
$place['city'] = $this->params['form']['city'];
$place['area'] = $this->params['form']['state'];
$place['country'] = $this->params['form']['country'];
$place['iso'] = $this->params['form']['iso'];
$this->Place->set($place);
$place_found = $this->Place->read();
}
我是否可以使用数组预设Place模型中的数据,然后使用Place read。我正在寻找像往常一样简单的东西:
$this->Place->id = 7;
$place_found = $this->Place->Read();
我也试过这样做:
$this->Place->city = blah;
$this->Place->area = foo; etc....
$place_found = $this->Place->read();
然而,这也行不通。
答案 0 :(得分:4)
你有没有用过find()
?! read()
仅获取传递了ID的行。
$place_found = $this->Place->find('first', array(
'conditions' => array(
'Place.city' => $city,
'Place.area' => $area
// etc
)
));
如果需要手动构建条件,可以创建一个条件数组,如下所示:
$placeConditions = array();
$placeConditions['city'] = $city;
if($area) {
$placeConditions['area'] = $area;
}
$places = $this->Place->find('first', array('conditions' => $placeConditions));
我建议您阅读我关联的网页,您很快就会发现没有理由使用read()
方法。
答案 1 :(得分:1)
在模型中你可以做到:
$this->id = 3; //place id $this->Place->read();
希望有所帮助
答案 2 :(得分:0)
我认为这种方法正是您所需要的(使用您的代码):
if (!empty($this->params['form'])) {
$place = array();
$place['city'] = $this->params['form']['city'];
$place['area'] = $this->params['form']['state'];
$place['country'] = $this->params['form']['country'];
$place['iso'] = $this->params['form']['iso'];
//$this->Place->set($place); //don't need it here I think
$place_found = $this->Place->find('all',array('conditions'=>$place));
}