[英]CakePHP - Sorting using HABTM Join Table Field
here's my problem: 这是我的问题:
Table1: Posts 表1:帖子
id - int
title - varchar
Table2: Categories 表2:类别
id - int
name - varchar
HABTM JoinTable: categories_posts HABTM JoinTable:categories_posts
id - int
post_id - int
category_id - int
postorder - int
As you can see, the join table contains a field called 'postorder' - This is for ordering the posts in a particular category. 如您所见,连接表包含一个名为“postorder”的字段 - 这用于对特定类别中的帖子进行排序。 For example, 例如,
Posts: Post1, Post2, Post3, Post4
Categories: Cat1, Cat2
Ordering:
Cat1 - Post1, Post3, Post2
Cat2 - Post3, Post1, Post4
Now in CakePHP, 现在在CakePHP中,
$postpages = $this->Post->Category->find('all');
gives me a array like 给我一个类似的数组
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 13
[name] => Cat1
)
[Post] => Array
(
[0] => Array
(
[id] => 1
[title] => Post2
[CategoriesPost] => Array
(
[id] => 17
[post_id] => 1
[category_id] => 13
[postorder] => 3
)
)
[1] => Array
(
[id] => 4
[title] => Post1
[CategoriesPost] => Array
(
[id] => 21
[post_id] => 4
[category_id] => 13
[postorder] => 1
)
)
)
)
)
As you can see [Post], they are not ordered according to [CategoriesPost].postorder but are ordered according to [CategoriesPost].id. 如您所见[Post],它们不是根据[CategoriesPost] .postorder排序的,而是根据[CategoriesPost] .id排序。 How can I get the array ordered according to [CategoriesPost].postorder? 如何根据[CategoriesPost] .postorder获取数组?
Thanks in advance for your time :) 在此先感谢您的时间 :)
Update 1: The Queries from Cake's SQL Log are: 更新1:Cake的SQL日志中的查询是:
SELECT `Category`.`id`, `Category`.`name` FROM `categories` AS `Category` WHERE 1 = 1
SELECT `Post`.`id`, `Post`.`title`, `CategoriesPost`.`id`, `CategoriesPost`.`post_id`, `CategoriesPost`.`category_id`, `CategoriesPost`.`postorder` FROM `posts` AS `Post` JOIN `categories_posts` AS `CategoriesPost` ON (`CategoriesPost`.`category_id` IN (13, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52) AND `CategoriesPost`.`post_id` = `Post`.`id`)
What I am looking for is how to make cake put a Order By CategoriesPost.postorder in that second SELECT SQL Query. 我正在寻找的是如何在第二个SELECT SQL查询中将蛋糕放入Order By CategoriesPost.postorder。
Update 2: Trying to use order as following 更新2:尝试使用如下命令
$this->Post->Category->find('all',array('order'=>array('postorder'=>'ASC')));
throws an SQL error 抛出SQL错误
SQL Error: 1054: Unknown column 'PostsCategory.postorder' in 'order clause'
The SQL Query is SQL查询是
SELECT `Category`.`id`, `Category`.`name` FROM `categories` AS `Category` WHERE 1 = 1 ORDER BY `CategoriesPost`.`postorder` ASC
Instead of an ORDERBY in the second SQL query (in my update1), its getting executed in the first SQL query as shown above. 而不是第二个SQL查询中的ORDERBY(在我的update1中),它在第一个SQL查询中执行,如上所示。
Try to make it part of the association: 尝试将其作为协会的一部分:
var $hasAndBelongsToMany = array(
'Post' => array(
...
'order' => 'CategoriesPost.postorder DESC',
)
)
This may or may not work, haven't tested it. 这可能有效,也可能无效,尚未测试过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.