Cakephp查找多个模型的所有查询 [英] Cakephp find all query on multiple models

查看:244
本文介绍了Cakephp查找多个模型的所有查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试下面的文章,以获得属于MenuItem 6的文章content_type为'blog'的文章。它找到 content_type ='blog'的所有文章。但我只想要它返回的文章,如果它属于Menuitem 7.现在,当它不返回一个空值的MenuItem,当不是7.

I try the following to get al the Articles That belong to MenuItem 6 and have an Article content_type of 'blog'. It does find all the articles with content_type='blog'. But I only want it to return the Article if it belongs to Menuitem 7. And now it return an empty value for MenuItem when not 7.

如何完成它

MenuItem与Article

MenuItem has a HABTM relationship with Article

代码具有HABTM关系:

code:

 $d=$this->Article->find('all' , array('contain' => array(
        'MenuItem' => array(
            'conditions' => array(
                'MenuItem.id ' => 7,        
             ),
                'fields'=>'id'
        ),
        'Tag'=>array(
            'conditions'=>array(
                'Tag.name'=>'tag1'
            ),
            'fields'=>'name'
        )
      ),
      'conditions'=>array('Article.content_type' => 'blog'),
      'fields'=>array('id','content_type'),
      'recursive'=>1
     ));

     debug($d);

数组:

 array(
      (int) 10 => array(
    'Article' => array(
        'id' => '15',
        'content_type' => 'blog'
    ),
    'Tag' => array(),
    'MenuItem' => array()
),
     (int) 11 => array(
    'Article' => array(
        'id' => '16',
        'content_type' => 'blog'
    ),
    'Tag' => array(),
    'MenuItem' => array(
        (int) 0 => array(
            'id' => '7',
            'MenuItemsArticle' => array(
                'id' => '18',
                'title' => '',
                'article_id' => '16',
                'menu_item_id' => '7'
            )
        )
    )
      )
 )


推荐答案

我认为这种类型的查找条件是修改您的模型以使用 hasMany通过(加入模型)。下面是我测试的一个例子,它是你想要的:

I think your best bet for this type of find condition is to modify your models to use hasMany through (The Join Model). Here's an example I tested that does what you want:

Article.php

class Article extends AppModel {
    public $hasMany = array('ArticleMenuItem');
}

MenuItem.php
$ b

MenuItem.php

class MenuItem extends AppModel {
    public $hasMany = array('ArticleMenuItem');
}

ArticleMenuItem.php
$ b

ArticleMenuItem.php

class ArticleMenuItem extends AppModel {
    public $useTable = 'articles_menu_items';

    public $belongsTo = array(
        'Article',
        'MenuItem'
    );
}

查找呼叫

$articles = $this->ArticleMenuItem->find('all', array(
    'conditions' => array(
        'menu_item_id' => 7,
        'Article.content_type' => 'blog'
    ),
    'contain' => array(
        'Article' => array(
            'fields' => array(
                'id',
                'title'
            )
        ),
        'Tag'
    )
));

这是它产生的:

array(
    (int) 0 => array(
        'ArticleMenuItem' => array(
            'article_id' => '1',
            'menu_item_id' => '7'
        ),
        'Article' => array(
            'id' => '1',
            'content_type' => 'blog',
            'title' => 'test blog'
        )
    ),
    (int) 1 => array(
        'ArticleMenuItem' => array(
            'article_id' => '4',
            'menu_item_id' => '7'
        ),
        'Article' => array(
            'id' => '4',
            'content_type' => 'blog',
            'title' => 'another'
        )
    )
)

这是它生成的查询:

SELECT `ArticleMenuItem`.`article_id`, `ArticleMenuItem`.`menu_item_id`, `Article`.`id`, `Article`.`content_type`, `Article`.`title` FROM `caketest`.`articles_menu_items` AS `ArticleMenuItem` LEFT JOIN `caketest`.`articles` AS `Article` ON (`ArticleMenuItem`.`article_id` = `Article`.`id`) WHERE `menu_item_id` = 7 AND `Article`.`content_type` = 'blog'

've set $ recursive = -1 在我的AppModel以及。我建议做同样,因为你使用可控行为,它是更有效的,因为它只拉回你需要的数据。 :)

I've set $recursive = -1 in my AppModel as well. I would suggest doing the same since you are using the containable behavior, it's much more efficient because it only pulls back the data that you need. :)

希望这有帮助,任何问题只是让我知道。

Hope this helps, any questions just let me know.

这篇关于Cakephp查找多个模型的所有查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆