Select last record of the user ?

Hello for all,


$exemple = exemple::model()->findAll("userid=:uid and statut='1'", array(":userid" => Yii::app()->user->id));

Please how to select just the last record of the user ?

becouse in the code i select all the records of user which is connected and has in statut = 1 . but i want to select just the last record of the user which has in status = 1 .

i don’t know how to use limit and order by in the query

Thanks

so ? :)

I would suggest to use scopes in your model:




public function scopes()

{

    return array(

        'lastRecord'=>array(

            'condition'=>'userid = '.Yii::app()->user->id.' AND status = 1',

            'order'=>'orderField DESC',

            'limit'=>1,

        ),

    );

}



Then you can easily select the last record:




$model = exemple::model()->lastRecord()->find();



But you can also use CDbCriteria + find() method.

I want to use this order by and limit 1 in this query :


$exemple = exemple::model()->findAll("userid=:uid and statut='1'", array(":userid" => Yii::app()->user->id));



can you tell me how do it ?

Thank you

andy_s already told you how to do it, but if you wish to use your statement:




$criteria = new CDbCriteria();

$criteria->condition  = "userid=:uid and statut='1'";

$criteria->order = "orderField DESC";

$criteria->limit = 1;


$exemple = exemple::model()->findAll($criteria, array(":userid" => Yii::app()->user->id));




IMHO I like the use of SCOPES for that type of action.

Thank you Antonio Ramirez and andy_s . it works now

Thanks again

hey I Have a field pid(Portal Id) and uid(User ID) in my tbl_user_account table. uid is the primary key of the table.For the Different Pid I have to make the uid example

For PID 1:

UID 1:,2:,3:etc

and PID 2:

UID 1:,2:,3:,

if someone again enter PID 1 then

UID will Be start from last uid value+1;

Please help me

Hi All

This code have error, so true it’s :




public function scopes()

{

    return array(

        'lastRecord'=>array(

            'condition'=>'userid = '.Yii::app()->user->id.' AND status = 1',

            'order'=>'"orderField" DESC',

            'limit'=>1,

        ),

    );

}



orderField changed to "orderField"

Thanks