Yii Framework Forum: CDbCriteria - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

CDbCriteria Retrieve column from last inserted row Rate Topic: -----

#1 User is offline   nivedha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 19-January 12

Posted 23 January 2012 - 10:51 PM

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

Thanks.

This post has been edited by tri: 25 January 2012 - 06:55 AM
Reason for edit: Topic description added

0

#2 User is offline   shailesh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 14-May 11
  • Location:Ahmedabad(India)

Posted 24 January 2012 - 12:40 AM

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
0

#3 User is offline   nivedha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 19-January 12

Posted 24 January 2012 - 01:10 AM

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..
0

#4 User is offline   Steven89 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 13-February 11

Posted 24 January 2012 - 01:32 AM

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.
0

#5 User is offline   shailesh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 14-May 11
  • Location:Ahmedabad(India)

Posted 24 January 2012 - 01:42 AM

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

#6 User is offline   nivedha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 19-January 12

Posted 24 January 2012 - 05:20 AM

i should give this code inside controller or model?

Thanks
0

#7 User is offline   Steven89 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 13-February 11

Posted 24 January 2012 - 10:29 AM

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

0

#8 User is offline   nivedha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 19-January 12

Posted 24 January 2012 - 10:29 PM

Thanks Steven..
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users