Cakephp属于订单

时间:2012-11-03 06:44:07

标签: cakephp

所以我有一个像这样的自引用模型。

class Category extends AppModel {
  public $order = "Category.name";
  public $belongsTo = array(
      'ParentCategory' => array(
          'className'    => 'Category',
          'foreignKey'   => 'parent_id',
          'order'        => 'ParentCategory.name'
      )
  );
}

它产生的SQL查询是:

SQL Query: SELECT `ParentCategory`.`id`, `ParentCategory`.`name` FROM `cakephp`.`categories` AS `ParentCategory` WHERE 1 = 1 ORDER BY `Category`.`name` ASC

这不起作用,因为此处"Category"不是表名。

我在这里做错了什么。为什么不尊重我的“订单”规则?

1 个答案:

答案 0 :(得分:0)

我在CakePHP文档中找到了一个simliar问题的解决方案:

http://book.cakephp.org/2.0/en/models/virtual-fields.html#virtual-fields-and-model-aliases

所以基本上,只要你想使用模型别名,最好在构造函数中定义这样的值:

  public function __construct($id = false, $table = null, $ds = null) {
      parent::__construct($id, $table, $ds);
      $this->order = $this->alias.".whatever";
  }