Ahsan Mohyuddin
Ahsan Mohyuddin

Reputation: 181

How to add alias with model in CActiveDataProivder yii

I have User model with "type" attribute. and Product model with "title". Here is my code.

$criteria = new  CDbCriteria;
$criteria->addCondition('type = "ABC"');    
$criteria->mergeWith ( array (
            'with' => 'products'
        ) );

$criteria->mergeWith ( array (
            'condition' => "`products`.title LIKE '". $model->title ."'",
            'together' => true
        ) );

$dataProvider = new CActiveDataProvider('User', array(
        'criteria' => $criteria,
    ));

It is giving following error:-

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: 
Integrity constraint violation: 1052 Column 'type' in where clause is ambiguous

My question is, how to add alias for User model so the User.type = "ABC" using above approch.

Upvotes: 0

Views: 90

Answers (2)

anwerj
anwerj

Reputation: 2478

$criteria = new  CDbCriteria;
$criteria->compare('t.type' => "ABC");    
$criteria->with= array('products'=>array('on'=>"title LIKE '$model->title'"));

$criteria->together=true;

$dataProvider = new CActiveDataProvider('User', array(
        'criteria' => $criteria,
    ));

if does not work please paste full error with whole query

Upvotes: 1

user2883814
user2883814

Reputation: 365

try like this:

$criteria = new  CDbCriteria;
$criteria->addCondition('table_name.type = "ABC"');    
$criteria->mergeWith ( array (
            'with' => 'products'
        ) );

$criteria->mergeWith ( array (
            'condition' => "`products`.title LIKE '". $model->title ."'",
            'together' => true
        ) );

$dataProvider = new CActiveDataProvider('User', array(
        'criteria' => $criteria,
    ));

Upvotes: 0

Related Questions