CDbCriteria


(Nivedha Ga) #1

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

Thanks.


(Shailesh Makwana990) #2

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


(Nivedha Ga) #3

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…


(Smw2j) #4

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.


(Shailesh Makwana990) #5

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


(Nivedha Ga) #6

i should give this code inside controller or model?

Thanks


(Smw2j) #7

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

    }

}




(Nivedha Ga) #8

Thanks Steven…