Question

Im having trouble with Yii conditions.

My code:

$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']);

In this case it works the way i want (i get list of items and each item has a list of farmers that have them, and 1 category to with this item is relative to.

But the problem is that I need to sort the list by price of a item that is stored in a farmers table.

so, if I uncomment this line:

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

I get:

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

You can see that there is no join with farmers:

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

If I uncomment:

//$criteria->together = true;

it solves this problem but in response i get list of 1 farmer (and that is not what i want)

I guess I have to do some kind of criteria in criteria but how?

my var_dump(); with 2 comment lines

    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
      }
    }
  }
}

my relations TradeGoods

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

edit

manually posting SQL query via phpmyadmin gives fine results

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`

but when Yii sort is launched like this

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

it "looses":

LEFT OUTER JOIN 
    `farmers_goods` `farmer_goods` 
    ON 
    (`farmer_goods`.`articul`=`t`.`articul`) 
Was it helpful?

Solution

I found a problem I really hope that helps someone like me and spare their time so the point is that when you are sorting a BELONGS_TO or HAS_ONE there's no problem jast use

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

and sort like

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

but when you need to sort a HAS_MANY (and possibly MANY_MANY, not tested that) you need to write your criteria like this

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

that's not obvious but a vary important thing that lets you sort by a related HAS_MANY table atribute

btw - sorry for my bad English

bast of luck from Ukraine

Ga1der

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top