Yii associated query using with cannot generate correct SQL

Source: Internet
Author: User
Tags sql error
The problem has not been solved since I was unable to access the Internet in my hometown. By default, ar queries in yii do not use with. In this way, the delayed loading function is used for object Association queries, which is convenient but brings efficiency problems. So we want to use with for pre-loading, but every time I use with, I will not be able to access the Internet in my hometown recently. This problem has never been solved.

By default, ar queries in yii do not use with. In this way, the delayed loading function is used for object Association queries, which is convenient but brings efficiency problems.

Therefore, we consider using with for pre-loading, but every time I use with, an SQL error occurs. After checking the error, we find that the order, condition, on and other conditions are not automatically added when the final SQL statement is generated.

I hope you can provide some solutions.
--------------------------------------
You may not understand this. I will explain it in detail.

Assume that the structure is as follows:

class CategoryModel{  function relations() {    return array(        'description' => array(self::HAS_ONE,'CategoryDescription','category_id'),    );  }  function defaultScope() {    return array(      'condition' => 'language=1',      'order'     => 'created desc',    );  }}class CategoryDescriptionModel{}class SiteModel{  function relations() {    return array(        'description' => array(self::HAS_ONE,'SiteDescription','site_id'),        'category' => array(self::BELONGS_TO,'Category','category_id'),    );  }  function defaultScope() {    return array(      'condition' => 'language=1',      'order'     => 'created desc',    );  }}class SiteDescriptionModel{}

When using with, the created desc will be ambiguous, for example.

Site::model()->count();Site::model()->with('description')->findAll();Site::model()->with('description','category','category.description');

The created desc in the preceding two statements becomes a syntax error. Because alias is added to the table after Yii is joined, the Field Names without alias cannot be identified.

Then I try to define alias for all models and change the order in scope to alias. created desc. however, another problem is that there is no Join Operation in count (), so alias. the created desc does not recognize it and an error is returned.

The official document says that the field in scope should eliminate ambiguity, but I have not found a perfect solution. Is there anything besides defining alias?

Reply content:

The problem has not been solved since I was unable to access the Internet in my hometown.

By default, ar queries in yii do not use with. In this way, the delayed loading function is used for object Association queries, which is convenient but brings efficiency problems.

Therefore, we consider using with for pre-loading, but every time I use with, an SQL error occurs. After checking the error, we find that the order, condition, on and other conditions are not automatically added when the final SQL statement is generated.

I hope you can provide some solutions.
--------------------------------------
You may not understand this. I will explain it in detail.

Assume that the structure is as follows:

class CategoryModel{  function relations() {    return array(        'description' => array(self::HAS_ONE,'CategoryDescription','category_id'),    );  }  function defaultScope() {    return array(      'condition' => 'language=1',      'order'     => 'created desc',    );  }}class CategoryDescriptionModel{}class SiteModel{  function relations() {    return array(        'description' => array(self::HAS_ONE,'SiteDescription','site_id'),        'category' => array(self::BELONGS_TO,'Category','category_id'),    );  }  function defaultScope() {    return array(      'condition' => 'language=1',      'order'     => 'created desc',    );  }}class SiteDescriptionModel{}

When using with, the created desc will be ambiguous, for example.

Site::model()->count();Site::model()->with('description')->findAll();Site::model()->with('description','category','category.description');

The created desc in the preceding two statements becomes a syntax error. Because alias is added to the table after Yii is joined, the Field Names without alias cannot be identified.

Then I try to define alias for all models and change the order in scope to alias. created desc. however, another problem is that there is no Join Operation in count (), so alias. the created desc does not recognize it and an error is returned.

The official document says that the field in scope should eliminate ambiguity, but I have not found a perfect solution. Is there anything besides defining alias?

Use $ criteria = new CDbCriteria ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.