dqp4933 2013-11-05 09:52
浏览 126
已采纳

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?

  • 写回答

1条回答 默认 最新

  • drbouzlxb92333332 2013-11-05 10:12
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题