2

我在 Yii 中定义了以下标准,并尝试使用它来获取一组客户。

$criteria = new CDbCriteria(array(
        "condition"=>"hidden = 0".(Yii::app()->user->GetState('is_admin') ? "" : " AND franchisesMunicipalities.franchise_id=".Yii::app()->user->getState('fid')),
        "with" => array('municipality','municipality.franchisesMunicipalities')
));
$customers = Customers::model()->findAll($criteria);

这(我认为)应该导致 Yii 将表 Customer 与表 Municipalities 连接起来,然后在同一个查询中将表 Municipalities 与表 Franchises_Municipalities 连接起来。但是它不起作用,因为它不加入特许经营市。

结果查询是这样的

SELECT `t`.`id`                   AS `t0_c0`, 
   `t`.`municipality_id`      AS `t0_c1`, 
   `t`.`personal_code_number` AS `t0_c2`, 
   `t`.`name`                 AS `t0_c3`, 
   `t`.`adress`               AS `t0_c4`, 
   `t`.`zip`                  AS `t0_c5`, 
   `t`.`phone`                AS `t0_c6`, 
   `t`.`mobile`               AS `t0_c7`, 
   `t`.`email`                AS `t0_c8`, 
   `t`.`hidden`               AS `t0_c9`, 
   `municipality`.`id`        AS `t1_c0`, 
   `municipality`.`county_id` AS `t1_c1`, 
   `municipality`.`name`      AS `t1_c2` 
FROM   `customers` `t` 
   LEFT OUTER JOIN `municipalities` `municipality` 
                ON ( `t`.`municipality_id` = `municipality`.`id` ) 
WHERE  ( hidden = 0 
     AND municipality.franchisesmunicipalities.franchise_id = 7 ) 
LIMIT  30 

如您所见,它仅加入一个关系。应该正确定义模型中的关系,因为我可以在其他上下文中使用它们。

为什么这不起作用?

4

1 回答 1

1

According to http://www.yiiframework.com/doc/api/1.1/CActiveRecord#with-detail I believe you should be doing it this way:

$criteria = new CDbCriteria(array(
        "condition"=>"hidden = 0".(Yii::app()->user->GetState('is_admin') ? "" : " AND franchisesMunicipalities.franchise_id=".Yii::app()->user->getState('fid'))
));
$customers = Customers::model()->with('municipality','municipality.franchisesMunicipalities')->findAll($criteria);

Hopefully that works for you.

于 2013-03-13T20:43:49.947 回答