使用Paginate with conditions和contain with CakePHP
所以,我想把我的一个请求分页。“正常方式”似乎效果不太好,因此,我做了一些研究,所有的答案都告诉我们要这样做:使用Paginate with conditions和contain with CakePHP,cakephp,pagination,conditional-statements,contain,Cakephp,Pagination,Conditional Statements,Contain,所以,我想把我的一个请求分页。“正常方式”似乎效果不太好,因此,我做了一些研究,所有的答案都告诉我们要这样做: $this->paginate("Like", array("Like.user_id" => $id), array( "order" => "Like.created desc", "contain" => array( "User", "Post",
$this->paginate("Like",
array("Like.user_id" => $id),
array(
"order" => "Like.created desc",
"contain" => array(
"User",
"Post",
"Post.User",
"Post.Like"
)
)
);
问题是。。。它不起作用。debug
给了我类似的东西:
array(
(int) 0 => array(
'Like' => array(
'id' => '38',
'created' => '2012-10-03 21:21:27',
'post_id' => '29',
'user_id' => '19'
),
'Post' => array(
'id' => '29',
'title' => 'Don't Panic',
'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam elementum sem ac sem imperdiet cursus. Quisque venenatis pulvinar ornare. Donec rutrum, lacus vel imperdiet sagittis, metus risus interdum ante, iaculis venenatis arcu nibh et odio.',
'image' => '29-c49cb96c.jpg',
'model' => '',
'created' => '2012-09-07 01:46:49',
'user_id' => '19',
'project_id' => '1',
'like_count' => '1'
),
'User' => array(
'password' => '*****',
'id' => '19',
'username' => 'Axiol'
)
)
)
没有Post.User
和Post.Like
的痕迹
有什么想法吗
编辑:
以下是模型关联:
职位
像
使用者
删除
Post.User
和Post.Like
-这些不是型号,不应列在包含
数组中
如果它们是模型名称-即,您已经在模型中指定了
$name
(即使它违反了命名约定),然后,请发布您的模型及其关联,我们可以进一步查看。删除post.User
和post.Like
-这些不是模型,不应该列在包含的数组中
如果它们是模型名称-即,您已经在模型中指定了$name
(即使它违反了命名约定),那么请发布您的模型及其关联,我们可以进一步查看。正如Dave已经提到的,您的模型名称已关闭。您不应该使用这种表示法,而是将它们作为“嵌套”模型包含在您正在加载的模型下面。像这样:
"contain" => array(
"User",
"Post" => array(
"User", // Get post related user data
"Like" // Get post related like data
)
)
正如戴夫已经提到的那样,你的模特名字不合适。您不应该使用这种表示法,而是将它们作为“嵌套”模型包含在您正在加载的模型下面。像这样:
"contain" => array(
"User",
"Post" => array(
"User", // Get post related user data
"Like" // Get post related like data
)
)
首先,您确实应该按照Oldskool的回答更正contain
数组
这应该是非常明显的,因为蛋糕是这样的:
Warning (512): Model "Post.Like" is not associated with model "Like"
[CORE\Cake\Model\Behavior\ContainableBehavior.php, line 342]
你可能没有看到,因为你的实际问题
您没有提到是否以任何方式加载了可包含的
行为。
我想你不是,你就知道了。contain
数组将被完全忽略
是可包含的
行为的文档。
我建议您将其添加到AppModel::actsAs
属性中,如下所示:
class AppModel extends Model {
public $actsAs = array('Containable');
}
这样,所有模型都可以自动使用它,您也应该停止使用recursive
madness
希望这能有所帮助。首先,您确实应该按照Oldskool的回答更正包含的数组
这应该是非常明显的,因为蛋糕是这样的:
Warning (512): Model "Post.Like" is not associated with model "Like"
[CORE\Cake\Model\Behavior\ContainableBehavior.php, line 342]
你可能没有看到,因为你的实际问题
您没有提到是否以任何方式加载了可包含的
行为。
我想你不是,你就知道了。contain
数组将被完全忽略
是可包含的
行为的文档。
我建议您将其添加到AppModel::actsAs
属性中,如下所示:
class AppModel extends Model {
public $actsAs = array('Containable');
}
这样,所有模型都可以自动使用它,您也应该停止使用recursive
madness
希望这有帮助。“它不起作用”?你是什么意思?正如你在调试中看到的,我没有User
和Like
下的子数组Post
(与其他Like
和User
数组不同)“它不工作”?你是什么意思?正如你在调试中看到的,我没有User
和Like
下的子数组Post
(与其他Like
和User
数组不同)是的,它们是型号名称。我用关联编辑了我的问题。@Axiol-Post.User
和Post.Like
模型的关联在哪里?是的,它们是模型名。我用关联编辑了我的问题。@Axiol-Post.User
和Post.Like
模型的关联在哪里?