Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关联项的CakePHP find()条件_Php_Cakephp_Associations_Has Many - Fatal编程技术网

关联项的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知道在构建帖子查询时要使用的条件。

您会遇到什么错误?您会遇到什么错误?