PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] Does the Yii CActiveRecord class have a first() method?

 March 03, 2022     php, yii     No comments   

Issue

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));

Solution

CActiveRecord::find() returns only one model.

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


Answered By - topher
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing