Naga shayan
Naga shayan

Reputation: 37

SubQueries in yii

Am trying to code for a subquery

select * from mob_reg where name in (select name from auth where
parent ="naga");

I followed all these answer's

[Sub-queries ActiveRecord Yii [How to set multiple condition on criteria for CActiveDataProvider?

But am getting invalid arguments for foreach().

   $model=new MobReg('search');
$model->unsetAttributes();


$criteria = new CDbCriteria(array(
    'select' => 't.imei, t.emp_name, t.email, t.name',
    'join' => 'INNER JOIN auth ON (t.name = auth.name AND auth.parent = :parent)',
    'group' => 't.id',
    'params' => array(':parent' => 'naga')
)); 
$dataProvider = new CActiveDataProvider('MobReg', array(
        'criteria'=>$criteria,
));

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'mob-reg-grid',
    'dataProvider'=>$dataProvider,
    'filter'=>$model,
    'columns'=>array(
        'imei',
        'emp_name',
                'email',
               'name',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

What I have to replace? any idea? where am I going wrong?

I guess the problem am facing is in referencing two different tables, one in subquery and another in main query. how to handle this one?

Upvotes: 0

Views: 1479

Answers (1)

Thomas Jensen
Thomas Jensen

Reputation: 2765

What about something along the lines of:

$criteria = new CDbCriteria(array(
    'select' => 't.imei, t.emp_name, t.email, t.name',
    'join' => 'INNER JOIN auth ON (t.name = auth.name AND auth.parent = :parent)',
    'group' => 't.id',
    'params' => array(':parent' => 'naga')
));

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

Would that work for you?

Upvotes: 2

Related Questions