就像 cakephp 手册中的示例一样,http://book.cakephp.org/view/1323/Containable#Containing-deeper-associations-1325,我需要通过关联模型上的条件从模型中获取数据。
我有:
模型语言:
class Language extends AppModel {
var $name = 'Language';
var $actsAs = array('Containable');
var $hasMany = array(
'LanguageTranslation' => array(
'className' => 'LanguageTranslation',
'foreignKey' => 'language_id'
)
);
}
和关联,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 是唯一的)。结果必须是一个!
所以,与
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 的语言?