cakephp 从控制器中检索具有自定义条件的所有记录

sauutmhj  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(149)

我有一个简单的问题,但我卡住了!
我有两个模型类,ItemTypology

  • Typology属于Item
  • Item具有多个Typology

表是这样的。

TYPOLOGY: id | item_id | title | description | published
ITEM:     id | title | price | created | published

所以我想检索的是:

  • 找到所有类型WHERE published = 1,且它们所属项目具有published = 1

TypologyControllers中我写了这样的代码:

$typologies = $this->Typology->find(
'all', 
array(
'contain' => array(
'Item' => array(
'conditions' => array(
'Item.published =' => "1"))), 
'conditions' => array(
'Typology.published'=>'1'), 
'recursive' => -1, 
'order' => array(
'Typology.' . $this->Typology->primaryKey . ' DESC')
));

    $this->set('typologies', $typologies);
pprl5pva

pprl5pva1#

为什么不使用连接:

$typologies = $this->Typology->find('all', array(
         'joins' => array(
            array(
                'table' => 'items',
                'alias' => 'Item',
                'type' => 'LEFT',
                'conditions' => array(
                'Item.id = Typology.item_id'
                )
            )
        ),
            'conditions' => array(
            'Item.published' => 1, 
            'Typology.published' => 1 
        ),
            'order' => array(
            'Typology.id' => 'DESC'
        ),
            'fields' => array('Item.*', 'Typology.*'),
            'recursive' => -1
        ));

    $this->set('typologies', $typologies);

相关问题