CakePHP containable不能以正确的方式过滤 [英] CakePHP containable doesn't filter in a proper way

查看:222
本文介绍了CakePHP containable不能以正确的方式过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像cakephp手册中的示例一样, http:// book .cakephp.org / view / 1323 / Containable#Containing-deeper-associations-1325 ,我需要通过关联模型上的条件从模型中获取数据。

Like the example in cakephp manual, http://book.cakephp.org/view/1323/Containable#Containing-deeper-associations-1325, i need to fetch data from a model through a condition on its association model.

我有:

模型语言:

class Language extends AppModel {

    var $name = 'Language';
    var $actsAs = array('Containable');

        var $hasMany = array(
        'LanguageTranslation' => array(
            'className' => 'LanguageTranslation',
            'foreignKey' => 'language_id'
        )
    );

}

和关联,ModelTranslation

And the association, ModelTranslation

class LanguageTranslation extends AppModel {

    var $name = 'LanguageTranslation';

    var $belongsTo = array(
        'Language'
    );
}

$language_array = $this->controller->Language->find('all', array(
            'contain' => "LanguageTranslation.id = 1" 
        ));

我接收所有语言,而不只是一个(因为LanguageTranslation中的id是唯一的)。结果需要是一个!

i receive all the languages, not only one (because id in LanguageTranslation is unique). The result need to be one!

SO,

debug($language_array);

结果是:

Array
(
    [0] => Array
        (
            [Language] => Array
                (
                    [id] => 1
                    [code] => it
                    [locale] => ita
                )

            [LanguageTranslation] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [language_id] => 1
                            [language] => italiano
                        )

                )

        )

    [1] => Array
        (
            [Language] => Array
                (
                    [id] => 2
                    [code] => en
                    [locale] => eng
                )

            [LanguageTranslation] => Array
                (
                )

        )

    [2] => Array
        (
            [Language] => Array
                (
                    [id] => 3
                    [code] => de
                    [locale] => ger
                )

            [LanguageTranslation] => Array
                (
                )

        )
)

为什么我不能捕获id = 1的语言?

Why i don't catch only the Language with id = 1?

推荐答案

containable中的条件仅适用于包含内的模型。因为主查询没有任何条件,它获取表中的每一行。一般来说,你不使用containable限制主查询,但限制查询通过包含树的方式(非常方便,当你需要经历3-4级递归,其中的结果可以得到相当blo肿,如果不包含)。

The conditions in containable apply only to the models inside the containment. Because the main query doesn't have any conditions it fetches every row in the table. In general you don't use containable to restrict the main query but to limit the way the query goes through the containment tree (very handy when you need to go through 3-4 levels of recursiveness where the results can get quite bloated if not contained).

在这种情况下,特别是如果你试图获取某个Language / LanguageTranslation对的数据,你可以从LanguageTranslation模型中拉取数据。 / p>

In this case in particular if you are trying to get the data of a certain Language/LanguageTranslation pair, you can just pull the data from the LanguageTranslation model.

$this->Language->LanguageTranslation->find( 
    'first',
    array( 
        'conditions' => array( 'LanguageTranslation.id' => 1 ),
        'recursive'  => 1
    )
);

这篇关于CakePHP containable不能以正确的方式过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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