我有POST表格 id,title,content,tags,status,create_time,update_time,author_id,image
我在Post模型中创建了模块和模型关系:
return array(
'author' => array(self::BELONGS_TO, 'User', 'author_id'),
'comments' => array(self::HAS_MANY, 'Comment', 'post_id', 'condition'=>'comments.status='.Comment::STATUS_APPROVED, 'order'=>'comments.create_time DESC'),
'commentCount' => array(self::STAT, 'Comment', 'post_id', 'condition'=>'status='.Comment::STATUS_APPROVED),
);
控制器操作:
public function actionHome()
{
$criteria=new CDbCriteria(array(
'condition'=>'status='.Post::STATUS_PUBLISHED,
'order'=>'update_time DESC',
'with'=>'commentCount',
));
$criteria->addSearchCondition('tags','home'); // $_GET['tag']);
$dataProvider=new CActiveDataProvider('Post', array(
'pagination'=>array(
'pageSize'=>Yii::app()->params['postsPerPage'],
),
'criteria'=>$criteria,
));
$this->render('home',array(
'dataProvider'=>$dataProvider
));
}
主页视图:
<?php foreach($dataProvider as $post) { ?>
<div class="blog_wrap">
<h2> <?php echo CHtml::link(CHtml::encode($post->title), $post->url); ?></h2>
<?php if($post->image != '') { ?>
<div class="img-box"><img src="<?php echo Yii::app()->request->baseUrl.'/images/post/'.$post->image; ?>"> </div>
<?php } ?>
<p><?php echo $post->content; ?></p>
<a href="more.php">Read More [+]</a>
</div>
<?php } ?>
错误:
Trying to get property of non-object
在
<?php echo CHtml::link(CHtml::encode($data->title), $data->url); ?>
如何使用yii Active记录查询关系表。我在控制器的家庭行动中没有工作。