CDbCriteria

Is it possible to retrieve a particular column of last inserted row of a table in CDbCriteria?

Thanks.

Yes ,Its Possible to do.

just Create Function in Model that get Selected column only and then call that function in Controller when Action Call

Can i pass that field to $criteria->condition in public function search() in model…

Coz based on that field, i need to retrieve the datas from database

Thanks in advance…

This is easy if you have a primary key for your table that auto increments.

Let’s say I have an Active Record called Upload that corresponds with a table called upload.

I can order DESC by uploadId if my primary key is uploadId.




$criteria = new CDbCriteria(array('select'=>'name','order'=>'uploadId DESC'));

$lastRow = Upload::model()->find($criteria);

/* Generated SQL: SELECT name FROM upload ORDER BY uploadId DESC LIMIT 1 */

echo "The name of my most recent upload is: " . $lastRow->name;



Note: You can order by a timestamp column if you don’t want to use an auto incremented primary key.

Thanks Steven89

but still required to Elborate for Condition Specification

criteria = new CDbCriteria(array(‘select’=>‘name’,‘order’=>‘uploadId DESC’));

$lastRow = Upload::model()->find($criteria);

if want to more spesific condition

then

criteria = new CDbCriteria(array(‘select’=>‘name’,‘order’=>‘uploadId DESC’,‘Condition’=>’<Your Condition here>’));

i should give this code inside controller or model?

Thanks

There are a couple of ways to do this. One way: Make a method in your model. Call that method from your controller.

Here’s an example:




/* Lets pretend this is my model */

public Upload

{

    public function GetLastRow()

    {

        $criteria = new CDbCriteria(array('select'=>'name','order'=>'uploadId DESC'));

        return($this->find($criteria));

    }

}


/* Lets pretend this is my controller */

public UploadController

{

    public function actionUpload()

    {

        $model = new Upload();

        $lastRow = $model->GetLastRow();

    }

}



Thanks Steven…