我有带有列 id、标题、内容、标签、状态、create_time、update_time、author_id、图像的 POST 表
我在 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 活动记录查询关系表。我的不在控制器的家庭操作中工作。