Lewis
Lewis

Reputation: 14876

Yii - find latest user's comment

I have a table name Comment containing fields id, userid, content, posteddate.

How could I find the most recent comment of user (i.e. id = 10) in this table?

Upvotes: 0

Views: 39

Answers (2)

Jon
Jon

Reputation: 437386

$model = Comment::model();
$attributes = ['userid' => 10];
$condition = ['order' => 'posteddate DESC', 'limit' => 1];

$result = $model->findByAttributes($attributes, $condition);

Upvotes: 1

Asped
Asped

Reputation: 3103

$Criteria = new CDbCriteria();
$Criteria->condition = "id = 10";
$Criteria->order = "posteddate desc";

$Comment = Comment::model()->find($Criteria);

Upvotes: 2

Related Questions