CakePHP外键在条件中获取列

时间:2013-04-19 15:17:54

标签: php cakephp

如何在查找SQL命令中使用其他列的外部表值?

例如,

我现在使用这样的结构:

$products = $this->Product->find('all');

$suits = array();

foreach ($products as $product) {
  if($product['ProductCategory']['name'] == "Suits"){
    array_push($suits,$product);
  }
}

$this->set('products',$suits);

是否可以将其转换为:

$suits = $this->Product->find('list',array('conditions' => array('Products.ProductCategory.name' => "Suits")));

1 个答案:

答案 0 :(得分:0)

$products = $this->Product->find('all', array('recursive' => -1,
                                              'contain'=>array('ProductCategory'=>
                                                  array('conditions'=>array('name'=>"Suits"))
                                 )));

应该按类别名称过滤所有产品并返回

[0] => Array
    (
        [Product] => Array
            (
                [id] => 1
                /*etc*/
            )
        [ProductCategory] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        /*etc*/
                    )
            )
    )