Yii条件关系问题

时间:2013-08-21 12:19:59

标签: yii yii-relations

我遇到了Yii条件的问题。

我的代码:

$criteria=new CDbCriteria;
//$criteria->together = true;
$criteria->group='t.id';
$criteria->with = array('farmer_goods','goods_catalog');
//$criteria->compare('farmer_goods.price', '<>0');
$criteria->compare('goods_catalog.name', $this->_params['category']);

在这种情况下,它按照我想要的方式工作(我得到了物品清单,每个物品都有一个有农民的清单,这个物品的1个类别是相对的。

但问题是我需要按照农民表中存储的项目的价格对列表进行排序。

所以,如果我取消注释这一行:

//$criteria->compare('farmer_goods.price', '<>0');

我明白了:

SELECT `t`.`id` AS `t0_c0`, `t`.`catalog_id` AS `t0_c1`, `t`.`articul` AS `t0_c2`, `t`.`###name` AS `t0_c3`, `t`.`name_rus` AS `t0_c4`, `t`.`ed_izm` AS `t0_c5`, `t`.`srok_godnosti` AS `t0_c6`, `t`.`temp_hraneniya` AS `t0_c7`, `t`.`belki` AS `t0_c8`, `t`.`jiri` AS `t0_c9`, `t`.`uglevodi` AS `t0_c10`, `t`.`kalorii` AS `t0_c11`, `t`.`gost` AS `t0_c12`, `t`.`photo` AS `t0_c13`, `t`.`opisanie` AS `t0_c14`, `goods_catalog`.`id` AS `t2_c0`, `goods_catalog`.`parent_id` AS `t2_c1`, `goods_catalog`.`unique_name` AS `t2_c2`, `goods_catalog`.`name` AS `t2_c3`, `goods_catalog`.`name_rus` AS `t2_c4`, `goods_catalog`.`rout_url` AS `t2_c5`, `goods_catalog`.`launch_model` AS `t2_c6`, `goods_catalog`.`photo` AS `t2_c7` FROM `goods_details` `t` LEFT OUTER JOIN `goods_catalog` `goods_catalog` ON (`t`.`catalog_id`=`goods_catalog`.`id`) WHERE (((farmer_goods.price LIKE :ycp0) AND (farmer_goods.price<>:ycp1)) AND (goods_catalog.name=:ycp2)) GROUP BY t.id LIMIT 50

你可以看到没有与农民的联系:

FROM `goods_details` `t` LEFT OUTER JOIN `goods_catalog` `goods_catalog` ON (`t`.`catalog_id`=`goods_catalog`.`id`)

如果我取消注释:

//$criteria->together = true;

它解决了这个问题,但作为回应我得到了1名农民的名单(这不是我想要的)

我想我必须在标准中做一些标准,但是如何?

我的var_dump();有两条评论专栏

    object(GoodsDetails)#88 (11) {
  ["_attributes":"CActiveRecord":private]=>
  array(15) {
    ["id"]=>
    string(1) "1"
    ["catalog_id"]=>
    string(1) "1"
    ["articul"]=>
    string(6) "myaso1"
    ["###name"]=>
    string(4) "meet"
    ["name_rus"]=>
    string(29) "Мяско вкуснящее"
    ["ed_izm"]=>
    string(1) "1"
    ["srok_godnosti"]=>
    string(2) "14"
    ["temp_hraneniya"]=>
    string(4) "0±2"
    ["belki"]=>
    string(4) "13.7"
    ["jiri"]=>
    string(4) "36,5"
    ["uglevodi"]=>
    string(8) "uglevodi"
    ["kalorii"]=>
    string(4) "384"
    ["gost"]=>
    string(13) "Р 53221-2008"
    ["photo"]=>
    string(371) "{"main":0,"images":[{"title":"pic","url":["images\/title_01.jpg","images\/title_02.jpg","images\/title_03.jpg","images\/title_04.jpg"]},{"title":"pic2","url":["images\/title_11.jpg","images\/title_12.jpg","images\/title_13.jpg","images\/title_14.jpg"]},{"title":"pic2","url":["images\/title_21.jpg","images\/title_22.jpg","images\/title_23.jpg","images\/title_24.jpg"]}]}"
    ["opisanie"]=>
    string(98) "zxc"
  }
  ["_related":"CActiveRecord":private]=>
  array(2) {
    ["farmer_goods"]=>
    array(5) {
      [0]=>
      object(FarmersGoods)#93 (11) {
        ["_attributes":"CActiveRecord":private]=>
        array(7) {
          ["id"]=>
          string(1) "1"
          ["farmer_id"]=>
          string(1) "1"
          ["articul"]=>
          string(6) "myaso1"
          ["price"]=>
          string(1) "0"
          ["fasovka"]=>
          string(9) "[1,3,4,5]"
          ["fasovka_options"]=>
          NULL
          ["count_left"]=>
          NULL
        }
      }
      [1]=>
      object(FarmersGoods)#94 (11) {
        ["_attributes":"CActiveRecord":private]=>
        array(7) {
          ["id"]=>
          string(1) "2"
          ["farmer_id"]=>
          string(1) "3"
          ["articul"]=>
          string(6) "myaso1"
          ["price"]=>
          string(5) "20.99"
          ["fasovka"]=>
          string(4) "3543"
          ["fasovka_options"]=>
          NULL
          ["count_left"]=>
          NULL
        }
      }
      [2]=>
      object(FarmersGoods)#95 (11) {
        ["_attributes":"CActiveRecord":private]=>
        array(7) {
          ["id"]=>
          string(1) "3"
          ["farmer_id"]=>
          string(1) "4"
          ["articul"]=>
          string(6) "myaso1"
          ["price"]=>
          string(5) "35.60"
          ["fasovka"]=>
          string(4) "3543"
          ["fasovka_options"]=>
          NULL
          ["count_left"]=>
          NULL
        }
      }
      [3]=>
      object(FarmersGoods)#96 (11) {
        ["_attributes":"CActiveRecord":private]=>
        array(7) {
          ["id"]=>
          string(1) "4"
          ["farmer_id"]=>
          string(1) "5"
          ["articul"]=>
          string(6) "myaso1"
          ["price"]=>
          string(2) "50"
          ["fasovka"]=>
          string(11) "[1,2,2.5,5]"
          ["fasovka_options"]=>
          NULL
          ["count_left"]=>
          NULL
        }
      }
      [4]=>
      object(FarmersGoods)#97 (11) {
        ["_attributes":"CActiveRecord":private]=>
        array(7) {
          ["id"]=>
          string(1) "5"
          ["farmer_id"]=>
          string(1) "6"
          ["articul"]=>
          string(6) "myaso1"
          ["price"]=>
          string(5) "50.10"
          ["fasovka"]=>
          string(3) "123"
          ["fasovka_options"]=>
          NULL
          ["count_left"]=>
          NULL
        }
      }
    }
    ["goods_catalog"]=>
    object(GoodsCatalog)#89 (11) {
      ["_new":"CActiveRecord":private]=>
      bool(false)
      ["_attributes":"CActiveRecord":private]=>
      array(8) {
        ["id"]=>
        string(1) "1"
        ["parent_id"]=>
        string(1) "6"
        ["unique_name"]=>
        NULL
        ["name"]=>
        string(4) "meet"
        ["name_rus"]=>
        string(8) "Мясо"
        ["rout_url"]=>
        NULL
        ["launch_model"]=>
        NULL
        ["photo"]=>
        NULL
      }
    }
  }
}
我的关系 TradeGoods

            'farmer_goods'=>array(self::HAS_MANY, 'FarmersGoods', array('articul'=>'articul')),
            'goods_catalog'=>array(self::BELONGS_TO, 'GoodsCatalog', array('catalog_id'=>'id')),

修改

通过phpmyadmin手动发布SQL查询可以得到很好的结果

SELECT 
`t`.`id` AS `t0_c0`, 
`t`.`catalog_id` AS `t0_c1`, 
`t`.`articul` AS `t0_c2`, 
`t`.`###name` AS `t0_c3`, 
`t`.`name_rus` AS `t0_c4`, 
`t`.`ed_izm` AS `t0_c5`, 
`t`.`srok_godnosti` AS `t0_c6`, 
`t`.`temp_hraneniya` AS `t0_c7`, 
`t`.`belki` AS `t0_c8`, 
`t`.`jiri` AS `t0_c9`, 
`t`.`uglevodi` AS `t0_c10`, 
`t`.`kalorii` AS `t0_c11`, 
`t`.`gost` AS `t0_c12`, 
`t`.`photo` AS `t0_c13`, 
`t`.`opisanie` AS `t0_c14`, 
`farmer_goods`.`id` AS `t1_c0`, 
`farmer_goods`.`farmer_id` AS `t1_c1`, 
`farmer_goods`.`articul` AS `t1_c2`, 
`farmer_goods`.`price` AS `t1_c3`, 
`farmer_goods`.`fasovka` AS `t1_c4`, 
`farmer_goods`.`fasovka_options` AS `t1_c5`, 
`farmer_goods`.`count_left` AS `t1_c6`, 
`goods_catalog`.`id` AS `t2_c0`, 
`goods_catalog`.`parent_id` AS `t2_c1`, 
`goods_catalog`.`unique_name` AS `t2_c2`, 
`goods_catalog`.`name` AS `t2_c3`, 
`goods_catalog`.`name_rus` AS `t2_c4`, 
`goods_catalog`.`rout_url` AS `t2_c5`, 
`goods_catalog`.`launch_model` AS `t2_c6`, 
`goods_catalog`.`photo` AS `t2_c7` 
FROM 
`goods_details` `t` 
LEFT OUTER JOIN 
`farmers_goods` `farmer_goods` 
ON 
(`farmer_goods`.`articul`=`t`.`articul`) 
LEFT OUTER JOIN 
`goods_catalog` `goods_catalog` 
ON 
(`t`.`catalog_id`=`goods_catalog`.`id`) 
WHERE 
(`goods_catalog`.`name`='meet')

GROUP BY `t`.`id`
ORDER BY `farmer_goods`.`price`

但是当像这样推出Yii排序时

$sort = new CSort('FarmersGoods');
$sort->attributes = array(
    'price'=>array(
        'asc'=>'farmer_goods.price ASC',
        'desc'=>'farmer_goods.price DESC',
    ),
    '*',
);

它“失败”:

LEFT OUTER JOIN 
    `farmers_goods` `farmer_goods` 
    ON 
    (`farmer_goods`.`articul`=`t`.`articul`) 

1 个答案:

答案 0 :(得分:0)

我发现了一个问题 我真的希望帮助像我这样的人,节省时间 所以重点在于,当您对BELONGS_TOHAS_ONE进行排序时,没有问题可以使用

$criteria=new CDbCriteria;
$criteria->group='t.id';
$criteria->with = array('farmer_goods');

并按

排序
    $sort = new CSort('FarmersGoods');
    $sort->attributes = array(
        'price'=>array(
            'asc'=>'farmer_goods.price ASC',
            'desc'=>'farmer_goods.price DESC',
        ),
    );

但是当你需要对HAS_MANY进行排序(可能还有MANY_MANY,未经过测试)时,你需要编写这样的标准

$criteria=new CDbCriteria;
$criteria->group='t.id';
$criteria->with = array('farmer_goods'=>array('together'=>true),'goods_catalog');

这不是很明显,但是一个不同的重要事项,可以让您按相关的HAS_MANY表属性进行排序

顺便说一句 - 抱歉我的英文不好

来自乌克兰的好运

Ga1der