Find newest user

Hey Guys,

can anyone explain me how I can get the newest user from Database with ActiveRecord? I have a field creationDate which is an integer, but how can I find the highest value there?


If you use CDbCriteria, set $criteria->order=‘creationDate DESC’, and use find($criteria) in order to receive only the first row.

CDbConnection has lastInsertID also

I’m pretty sure the lastInsertId is lost when the request ends though… or is it not?

Databases return the auto-incremented id in case of insertion, and CDbConnection only stores that for the given request. If you want it in the subsequent requests as well without querying your database, you have to cache it.

that’s what I though, thanks for clearing it up

Ok thats what I wanted to know, thank you very much.

This is the classic problem of retrieving the groupwise maximum row. pestaa’s idea is okay for small datasets, but might be a problem when it’s large. My approach is to consider finding the maximum a business rule, and making it a static function of the model (which you can reuse). You can then use this value to grab an AR.

So, in the model (for a different problem):

static function getHighestAgentCountry() {

    $sql = 'select MAX(agtCountry) from agents';

    return Yii::app()->db->createCommand($sql)->queryScalar();


Then you can simple do:

$agent = Agent::model()->find('agtCountry='. Agent::getHighestAgentCountry());

This may or may not work if you have duplicates for the particular value, but you will know this for your domain.

Or you can wrap the whole thing up in the model, which will make your other code much more readable and maintainable.

$agent = Agent::findHighestAgentCountry());

Should it be:

$agent = Agent::model()->findHighestAgentCountry());

Or is model() not always needed?

$agent = Agent::findHighestAgentCountry());

… is fine. It’s just a regular static call on Agent.

static function findHighestAgentCountry() {

    return Agent::model()->find('agtCountry='. Agent::getHighestAgentCountry());


Ok that looks more beatiful, thanks for the explanation

Would this also work?

$agent = Agent::model()->findHighestAgentCountry());

static function findHighestAgentCountry() {

    return $this->find('agtCountry='. Agent::model()->getHighestAgentCountry());


I had never thought about this funny enough… Notice the differences… I don’t think $this-> would work in the first case

Yes, you can call this on the model, since it is still part of Agent; and an instance of Agent is what Agent::model() is. Clearly, there is no reason to, though, and it is extremely bizarre to me. After all, the original is a static on Agent!

This fails as a static call, obviously, but is fine once you remove that.

If doing this, then why not go the whole hog and remove the static from getHighestAgentCountry() and do:

function findHighestAgentCountry() {

    return $this->find('agtCountry='. $this->getHighestAgentCountry());


Of course, if I were looking at your code I’d think you had no lost your marbles!

This is how one uses models, ime. I only wish it were easier in Yii to do:

$agent = new Agent;

# $agent->findPk($id);


$agent2 = new Agent;


But at least it’s not hard to implement stuff like that myself.


$agent = Agent::getInstance();

This is what does model().

Or am I missing something?

new Agent and Agent::model() are equal. For chainable methods you may want to use the latter though.