在cakephp中使用read()检索包含数据数组的行

在cakephp中使用read()检索包含数据数组的行,cakephp,model,Cakephp,Model,我想知道是否可以使用类似于以下内容的内容从数据库检索行: if (!empty($this->params['form'])) { $place = array(); $place['city'] = $this->params['form']['city']; $place['area'] = $this->params['form']['state']; $place['country'] = $thi

我想知道是否可以使用类似于以下内容的内容从数据库检索行:

    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();
但是,这也不起作用。

在模型中,您可以执行以下操作:

$this->id = 3; //place id $this->Place->read(); $this->id=3//地点id $this->Place->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()
方法。

我认为这种方法正是您想要的(代码):

您必须使用“find()”,而不是“read()”,但是-它几乎和示例代码一样简单,应该可以工作。(另外,我相信数组_push()有一个快捷方式,但是-为了可读性,我喜欢使用它-个人偏好):


我没有可用的id,我正在尝试根据其他条件查找。我正在寻找一种更简单的方法,通过使用数组来实现这一点,而不必完整地写出所有内容。我想知道蛋糕是否有更简单的方法来做这件事。你会注意到,如果你使用$this->Place->set($Place),它实际上会将Place->data设置为提供的值,我很确定有一种方法可以检索缺少的值,但我不记得它是什么。请参阅更新的答案,
read()
只能根据ID检索行。必须同意Dunhamzzz。使用
WHERE
子句中的条件执行
SELECT
查询的模拟方法是使用
find()
。考虑<代码> Read()>代码>一个特殊的情况,它只通过ID查询并自动填充模型而不是返回数组。这可能是我的一个误解,今天再看一遍,看看你的答案,它们基本上是相同数量的编码。非常感谢。
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));
}
if (!empty($this->params['form'])) {
    $conditions = array();
    array_push($conditions, array('Place.city' => $this->params['form']['city']);
    array_push($conditions, array('Place.state' => $this->params['form']['state']);
    array_push($conditions, array('Place.country' => $this->params['form']['country']);
    array_push($conditions, array('Place.iso' => $this->params['form']['iso']);

    $this->Place->set($place);
    $place_found = $this->Place->find('all', array('conditions'=>$conditions));
}