So I have a self-referencing model like so.
class Category extends AppModel {
public $order = "Category.name";
public $belongsTo = array(
'ParentCategory' => array(
'className' => 'Category',
'foreignKey' => 'parent_id',
'order' => 'ParentCategory.name'
)
);
}
The sql query it produces is this:
SQL Query: SELECT `ParentCategory`.`id`, `ParentCategory`.`name` FROM `cakephp`.`categories` AS `ParentCategory` WHERE 1 = 1 ORDER BY `Category`.`name` ASC
Which will not work because "Category" is not a table name here.
What am I doing wrong here. Why is it not respecting my “order” rule?
I found the solution to a simliar issue in the CakePHP docs:
http://book.cakephp.org/2.0/en/models/virtual-fields.html#virtual-fields-and-model-aliases
So basically, anytime you want to use Model Aliasing, it is best to define the values in the constructor like this: