How can I return the newest Post of the specific Author in Yii Blog Demo ??
in the User model we have
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'posts' => array(self::HAS_MANY, 'Post', 'author_id'),
);
}
I dont want Hard coded m I just need a simple way because I need to access newest record many times
I want to do something like this but I don’t know how do this ?
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'posts' => array(self::HAS_MANY, 'Post', 'author_id'),
'newestPost' => array(self::HAS_One, 'Post', 'author_id',array('order'=>'Post.create_time DESC',)),
);
}
Usage :
<?php
$model = User::model()->findByPk(1);
echo $model->newestPost->title; // Must echo title of Newst post of User 1
?>
Now If I want access to last Status of Post I can do this :
<?php
$post->neweststatus->status;//Status ID
$post->neweststatus->log_time;//When Status changed to this status
?>
Now my problem is ::
Only posts that $post->neweststatus->status = 1 and $post->neweststatus->status=2 can be display for end user and other post must hide for end users so I want to create a Named Scope like this
<?
public function userVisible()//Only Visible Post for end-users
{
$criteria = new CDbCriteria;
$criteria->with = array('neweststatus');
$criteria->addColumnCondition(
array(
'neweststatus.status' => 1,
'neweststatus.status' => 2,
),
'OR'
);
$this->getDbCriteria()->mergeWith($criteria);
return $this;
}
?>
but is dont works properly becacuse in this example
the newest status of Post#1 is 2 but in Tbl_Log post#1 has 0 and 1 as old status and when I use
[color="#006400"]/* Merged 2 threads (“HAS_MANY” and “CDbCriteria”) into this one. Please don’t scatter the same issue accross different threads. */[/color]
So, you say ‘neweststatus’ relation works fine for lazy loading:
$post->neweststatus->status;
but it doesn’t work as expected when you use it for filtering … am I right?
I think it’s the limitation of a HAS_ONE relation that uses ‘order’ to get the first record. Probably we have do ‘select from select …’ in a very low-level fashion … as you are trying to do.
Would you please try this?
<?
public function userVisible()//Only Visible Post for end-users
{
$criteria = new CDbCriteria;
$criteria->select = array(
'*',
'(SELECT status from (
SELECT * from `tbl_log`
WHERE `tbl_log`.`Post_ID` = `t`.`id`
ORDER BY `tbl_log`.`Log_Time` DESC
LIMIT 1)
) as status',
);
$criteria->addColumnCondition(
array(
'status' => 1,
'status' => 2,
),
'OR'
);
$this->getDbCriteria()->mergeWith($criteria);
return $this;
}
?>
As I suggested in the previous post, I have confirmed that A HAS_ONE relation using ORDER has a limitation. You can use it for the lazy loading scenario. It works fine for it. But you can not use it for ‘with’, because ‘ORDER’ clause is not applied to the related table only but to the whole virtual table that is the result of joining.