dorflv5944 2014-05-17 05:34
浏览 10
已采纳

yii关系HAS_MANY添加条件

I write this line at my controller

<i>
    $model_town=ResTown::model()->with(array('get_by_town'))->findAll(array('limit'=>'3'));
</i>

I need to make get_by_town with limit=3 and user_active=1

I got all records but conditions don't work

  • 写回答

1条回答 默认 最新

  • dplo59755 2014-05-17 08:18
    关注

    If you would like to add condition on related table you should write it inside with() method.

    So, you should get something similar `

    ->with('get_by_town'=>array('limit'=>3, 'user_active'=>1))
    

    But in some cases it can not helps you, because it depends on many things: for example, relation type, join type and so on.

    The easiest way to work with ActiveRecord for me, first of all write down pure sql, and then convert to ActiveRecord representation. Thus, I get optimized query and exactly knowing of what I'm doing %)

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部