Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.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 3.0中的可包含行为在多级关联中不起作用_Php_Cakephp_Cakephp 3.0 - Fatal编程技术网

cakephp 3.0中的可包含行为在多级关联中不起作用

cakephp 3.0中的可包含行为在多级关联中不起作用,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,我在cakephp2.x工作了大约1.5年,这很好,现在我已经将自己转移到Cakephp3.x,但是我在使用多级关联获取数据时遇到了问题,请告诉我我正在编写中。 现在我的问题是!! 在CakePHP2.x中:我在控制器中使用了多级关联,如下所示 在注释控制器中: 型号:Post 型号:评论 Posts有许多评论(我在模型中定义的关系) 评论属于帖子 $this->Post->Comment->find('all'); 这个查询也会返回评论及其相关帖子,但现在是在cakephp

我在cakephp2.x工作了大约1.5年,这很好,现在我已经将自己转移到Cakephp3.x,但是我在使用多级关联获取数据时遇到了问题,请告诉我我正在编写中。 现在我的问题是!! 在CakePHP2.x中:我在控制器中使用了多级关联,如下所示

在注释控制器中: 型号:Post 型号:评论

Posts有许多评论(我在模型中定义的关系) 评论属于帖子

$this->Post->Comment->find('all');
这个查询也会返回评论及其相关帖子,但现在是在cakephp 3.x中

$this->Posts->Comments->find();
它只返回评论,不返回相关帖子,但当我使用像这样的可包含行为时

$this->Post->Comments->find('all',['contain'=>['Posts'] ] )
这一次,它显示错误帖子与评论无关 现在我想知道我做错了吗

表(模型)在蛋糕3中是复数形式。听起来你想做这样的事情:

$this->Posts->find()->contain(['Comments']);
// or
$this->Posts->find('all', ['contain' => ['Comments']]);
如果仍然出现错误,请确保已设置关联

// src/Model/Table/PostsTable.php
public function initialize(array $config)
{
    parent::initialize($config);
    $this->hasMany('Comments');
}

试试这个,我想这会有帮助的

$this->Posts->find('all')->contain(['Comments']);

在cakephp3.x上试试这个

$this->Posts->Comments->find()->contain(['Posts'])->all();


在尝试此操作之前,请确保您的模型关系中有许多项必须正确。

是的,伙计,您是对的,但我说的是cakephp 3.0中的多级关联。您尝试过此操作吗$这->帖子->评论->查找('all')->包含('Posts');在评论控制器中!!!it show的帖子与评论没有关联。但是它在cakephp 2.xCan上运行得很好,你可以使用你的PostsTable.php类吗?你是对的,但我使用的是插件,它不允许在一个插件到另一个插件之间使用可包含的行为,并显示与帖子无关的错误注释。。谢谢
$this->Posts->Comments->find()->contain(['Posts'])->toArray();