Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:50:54+00:00 2026-06-13T20:50:54+00:00

So I have a self-referencing model like so. class Category extends AppModel { public

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:50:55+00:00Added an answer on June 13, 2026 at 8:50 pm

    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:

      public function __construct($id = false, $table = null, $ds = null) {
          parent::__construct($id, $table, $ds);
          $this->order = $this->alias.".whatever";
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model with self-referencing ManyToMany field: class Suite(models.Model): suites = models.ManyToManyField(self, related_name=parents,
Assume the following model. Note the self-referencing relationship parent. class Widget(object): __tablename__ = 'widgets'
I have a standard self referencing table of Categories . In my entity model
I have a self-referencing many-to-many relationship between users and managers that looks like this:
I have a problem in a hierarchical query. I have a self-referencing table like
In my 'Topic' entity, I have a One-To-Many, Self-referencing relationship $parent:$children . class Topic
I have a POCO Menu class with a self reference children property: public class
I would like to make a form I have in my website self referencing.
I'd like to create a self referencing relation in rails. I have a Person
I have a self-referencing table with content like this: Self-referencing parent table ID ParentID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.