Harsha Tharanga
Harsha Tharanga

Reputation: 94

Yii criteria issue with selecting values from foreign tables

Hi i want to select following underlined values from table.i tried but its not working.... enter image description here

$criteria = new CDbCriteria;       

    $criteria->select = array('description', 'id','rate','item_unit_id');
    $criteria->with=array("item_unit","color");
    $criteria->together = true; 
    $criteria->addSearchCondition("description", $_GET['query']);

    $criteria->limit = $this->limit;

    $items = Item::model()->findAll($criteria);
    $suggestions = array();
    $x=0;
    foreach ($items as $c) {
        $suggestions[$x]['value'] = $c->description;
        $suggestions[$x]['id'] = $c->id;
        $suggestions[$x]['rate'] = $c->rate;
        $suggestions[$x]['unit'] = $c->item_unit->name;
        $x++;
    }

Upvotes: 0

Views: 1227

Answers (1)

Twisted1919
Twisted1919

Reputation: 2499

Well, if you would've read the documentation you would know, but because you didn't, you don't :)

However, this might help you:

$criteria->select = 't.id, t.description, t.rate, t.code, color.name, item_unit.name';
$criteria->with = array(
    "item_unit" => array(
        'together' => true,
        'joinType' => 'INNER JOIN'
    ),
    "color" => array(
        'together' => true,
        'joinType' => 'INNER JOIN',
    )
); 

$criteria->addSearchCondition("t.description", $_GET['query']);
$criteria->limit = $this->limit;

$items = Item::model()->findAll($criteria); 

Upvotes: 3

Related Questions