Yii关联查询使用with无法生成正确sql的问题

WBOY
Release: 2016-06-06 20:52:17
Original
1032 people have browsed it

最近在老家无法上网,这个问题一直都没有解决。

默认yii中ar查询是可以不使用with的,这样会使用延迟加载功能进行对象关联查询,很方便,但是带来了效率问题。

所以考虑使用with进行预加载,但是每当我使用with时就会出现sql错误,查看了error之后发现是因为relations里添加的order,condition,on等条件在生成最终的sql时不会自动加上表别名。

希望有了解的同学能提供一下解决办法。
--------------------------------------
这么说可能大家不太明白,我详细说明一下.

假设结构如下:

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{
}
Copy after login
Copy after login

那么当使用with的时候,created desc会出现歧义,比如.

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

上面后两个语句中的created desc就会成为语法错误,因为Yii进行了join之后会给表增加alias,所以没有alias的字段名无法识别.

之后我尝试和给所有的model定义alias,然后将scope中的order改为alias.created desc.但是产生了另一个问题,就是在count()的时候是没有Join操作的,所以alias.created desc也不识别而报错了.

官方文档中说scope中的字段要消除歧义,但是我没有找到一个完美的解决办法,除了定义alias之外还有别的么?

回复内容:

最近在老家无法上网,这个问题一直都没有解决。

默认yii中ar查询是可以不使用with的,这样会使用延迟加载功能进行对象关联查询,很方便,但是带来了效率问题。

所以考虑使用with进行预加载,但是每当我使用with时就会出现sql错误,查看了error之后发现是因为relations里添加的order,condition,on等条件在生成最终的sql时不会自动加上表别名。

希望有了解的同学能提供一下解决办法。
--------------------------------------
这么说可能大家不太明白,我详细说明一下.

假设结构如下:

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{
}
Copy after login
Copy after login

那么当使用with的时候,created desc会出现歧义,比如.

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

上面后两个语句中的created desc就会成为语法错误,因为Yii进行了join之后会给表增加alias,所以没有alias的字段名无法识别.

之后我尝试和给所有的model定义alias,然后将scope中的order改为alias.created desc.但是产生了另一个问题,就是在count()的时候是没有Join操作的,所以alias.created desc也不识别而报错了.

官方文档中说scope中的字段要消除歧义,但是我没有找到一个完美的解决办法,除了定义alias之外还有别的么?

要用$criteria = new CDbCriteria(); 才可以完美解决

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!