Yii Association query uses with failed to generate correct SQL problem

Source: Internet
Author: User
Tags aliases sql error
The problem of not being able to surf the internet recently has not been solved.

The AR query in the default Yii can be used without using with, so it is convenient to use the lazy load function for object Association query, but it brings the efficiency problem.

So consider using with to preload, but every time I use with a SQL error, and when I look at the error, I find that the relations added in the Order,condition,on will not automatically add table aliases when generating the final SQL.

Hope to have a better understanding of the students can provide a solution.
--------------------------------------
So maybe we don't quite understand, I'll explain in detail.

The hypothetical 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{}

So when using with, created Desc will appear ambiguous, for example.

Site::model ()->count (); Site::model ()->with (' description ')->findall (); Site::model ()->with (' Description ', ' Category ', ' category.description ');

The created DESC in the latter two statements will become a syntax error, because Yii will add alias to the table after the join, so no alias field name is recognized.

I then try and define alias for all the model, and then change the order in scope to alias.created desc. But another problem is that there is no join operation at COUNT (), so alias.created Desc also did not recognize the error.

The official document says that the fields in scope want to disambiguate, but I didn't find a perfect solution, besides defining alias, what else?

Reply content:

The problem of not being able to surf the internet recently has not been solved.

The AR query in the default Yii can be used without using with, so it is convenient to use the lazy load function for object Association query, but it brings the efficiency problem.

So consider using with to preload, but every time I use with a SQL error, and when I look at the error, I find that the relations added in the Order,condition,on will not automatically add table aliases when generating the final SQL.

Hope to have a better understanding of the students can provide a solution.
--------------------------------------
So maybe we don't quite understand, I'll explain in detail.

The hypothetical 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{}

So when using with, created Desc will appear ambiguous, for example.

Site::model ()->count (); Site::model ()->with (' description ')->findall (); Site::model ()->with (' Description ', ' Category ', ' category.description ');

The created DESC in the latter two statements will become a syntax error, because Yii will add alias to the table after the join, so no alias field name is recognized.

I then try and define alias for all the model, and then change the order in scope to alias.created desc. But another problem is that there is no join operation at COUNT (), so alias.created Desc also did not recognize the error.

The official document says that the fields in scope want to disambiguate, but I didn't find a perfect solution, besides defining alias, what else?

To use $criteria = new Cdbcriteria (); Before it can be solved perfectly.

  • 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.