【问题标题】:CakePHP - Sorting using HABTM Join Table FieldCakePHP - 使用 HABTM 连接表字段进行排序
【发布时间】:2010-05-14 21:35:36
【问题描述】:

这是我的问题:

表 1:帖子

id - int
title - varchar

表 2:类别

id - int
name - varchar

HABTM 连接表:categories_posts

id - int
post_id - int
category_id - int
postorder - int

如您所见,连接表包含一个名为“postorder”的字段 - 用于对特定类别中的帖子进行排序。例如,

Posts: Post1, Post2, Post3, Post4
Categories: Cat1, Cat2
Ordering:
     Cat1 - Post1, Post3, Post2
     Cat2 - Post3, Post1, Post4

现在在 CakePHP 中,

$postpages = $this->Post->Category->find('all');

给我一​​个类似的数组

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
            )
          )

        )
    )
) 

正如你所看到的[Post],它们不是根据[CategoriesPost].postorder 排序的,而是根据[CategoriesPost].id 排序的。如何获取根据 [CategoriesPost].postorder 排序的数组?

提前感谢您的宝贵时间:)

更新 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`) 

我正在寻找的是如何让蛋糕在第二个 SELECT SQL 查询中放入 Order By CategoriesPost.postorder。

更新 2: 尝试使用如下顺序

$this->Post->Category->find('all',array('order'=>array('postorder'=>'ASC')));

引发 SQL 错误

SQL Error: 1054: Unknown column 'PostsCategory.postorder' in 'order clause'

SQL 查询是

SELECT `Category`.`id`, `Category`.`name` FROM `categories` AS `Category` WHERE 1 = 1 ORDER BY `CategoriesPost`.`postorder` ASC

第二个 SQL 查询(在我的 update1 中)中不是 ORDERBY,而是在第一个 SQL 查询中执行,如上所示。

【问题讨论】:

  • 我已经有一段时间没有搞定它了。如果你输入 'order'=>'CategoriesPost.postorder' 会被忽略吗?我想我记得在某个时候遇到过这个问题并且不得不使用 usort。
  • $this->Post->Category->find('all',array('order'=>array('CategoriesPost.postorder'=>'ASC'))) 抛出 SQL 错误: 1054:“订单条款”中的未知列“PostsSitepage.postorder”...

标签: php arrays cakephp


【解决方案1】:

尝试使其成为关联的一部分:

var $hasAndBelongsToMany = array(
    'Post' => array(
        ...
        'order' => 'CategoriesPost.postorder DESC',
    )
)

这可能有效,也可能无效,尚未测试。

【讨论】:

  • @Ashok,我确定这可行。只需检查您的 Category 模型的代码:)
  • 嘿,这行得通!发生的事情是 Categories 和 Posts 模型位于两个不同的插件中,我没有遵循 Plugin.Model 表示法。当我纠正它时,订购开始起作用了!谢谢大家的时间:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
相关资源
最近更新 更多