关联项的CakePHP find()条件
我有以下命令,从我的数据库中获取带有多个相关条目的条目:关联项的CakePHP find()条件,php,cakephp,associations,has-many,Php,Cakephp,Associations,Has Many,我有以下命令,从我的数据库中获取带有多个相关条目的条目: $teasers = $this->Teaser->find('all', array( 'conditions' => array( 'Teaser.published' => 1 ), )); 现在,由于有许多关系,也将获取帖子条目 输出如下所示: array( 0 => array( 'Teaser' => array(
$teasers = $this->Teaser->find('all', array(
'conditions' => array(
'Teaser.published' => 1
),
));
现在,由于有许多
关系,也将获取帖子
条目
输出如下所示:
array(
0 => array(
'Teaser' => array(
'id' => '1',
'user_id' => '63',
'token' => '56d455bc20cfb56d455bc20d08',
// this goes on
),
'Post' => array(
0 => array(
'id' => '1',
'teaser_id' => '1',
'title' => 'blabla',
'text' => 'blabla',
'published' => 1,
// this goes on
)
)
)
)
现在我的问题是,如何在条件
中包含一些内容,以便同时过滤Post
-条目
当我像这样输入它时,我得到一个错误:
$teasers = $this->Teaser->find('all', array(
'conditions' => array(
'Teaser.published' => 1,
'Post.published' => 1
)
));
您可以在模型
striser.php
中编写条件
public $hasMany = array(
'Post' => array(
'className' => 'Post',
'conditions' => array('Post.published' => 1)
)
);
您可以在模型
striser.php
中编写条件
public $hasMany = array(
'Post' => array(
'className' => 'Post',
'conditions' => array('Post.published' => 1)
)
);
您应该阅读和的文档。这些都是基础
$teasers = $this->Teaser->find('all', array(
'contain' => [
'Post' => [
'conditions' => [
'published' => 1
]
]
],
'conditions' => array(
'Teaser.published' => 1,
)
));
您应该阅读和的文档。这些都是基础
$teasers = $this->Teaser->find('all', array(
'contain' => [
'Post' => [
'conditions' => [
'published' => 1
]
]
],
'conditions' => array(
'Teaser.published' => 1,
)
));
出现错误的原因是您的关系是
有很多
,因此当蛋糕包含
时,它实际上是在为您的查找执行多个查询。因此,您不能在条件中指定'Post.published'=>1
,因为Post
别名在主查询(检索摘要的查询)中不存在
相反,您需要将附加条件作为包含的一部分传递:-
$teasers = $this->Teaser->find('all', [
'contain' => [
'Post' => [
'conditions' => [
'Post.published' => 1
]
]
],
'conditions' => [
'Teaser.published' => 1,
]
]);
这将让Cake知道在构建帖子查询时要使用的条件。出现错误的原因是您的关系是有许多
,因此当Cake包含时,它实际上是在为您的查找执行多个查询。因此,您不能在条件中指定'Post.published'=>1
,因为Post
别名在主查询(检索摘要的查询)中不存在
相反,您需要将附加条件作为包含的一部分传递:-
$teasers = $this->Teaser->find('all', [
'contain' => [
'Post' => [
'conditions' => [
'Post.published' => 1
]
]
],
'conditions' => [
'Teaser.published' => 1,
]
]);
这将让Cake知道在构建帖子查询时要使用的条件。您会遇到什么错误?您会遇到什么错误?