duanbu1421 2013-02-08 11:15
浏览 56
已采纳

Yii CActiveRecord类是否有first()方法?

I need to just get the first record from a Yii CActiveRecord derived class. In Rails I would just be able to do this:

post = Post.first

I thought I could do the same thing with Yii like this:

$post = Post::model()->first();

But that method doesn't exist. Do I have to just do find with a condition to get the first record?

I don't see first() in the docs for CActiveRecord so I assume the answer is no, it doesn't have a first method. So how would one go about querying just the first record?

This works but sure is an ugly hack. Surely there's a better way.

$first = Post::model()->findAll(array('order'=>id, 'limit'=>1));
  • 写回答

3条回答 默认 最新

  • dpxnrx11199 2013-02-08 12:29
    关注

    CActiveRecord::find() returns only one model.

    $first=Post::model()->find();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部