newyii 2013-08-16 14:49:16 3457次浏览 1条回复 0 0 0
$posts = Post::model()->with(
	array(
		'author'=>array(
			'select'=>'username',
		),
		'comments'=>array(
			 'condition'=>'comments.status='.Comment::STATUS_APPROVED,
			 'order'=>'comments.create_time  DESC'
		),
	)
	)->findAll("t.status=:status",array(":status"=>"".Post::STATUS_PUBLISHED));

	$this->render('posts',array(

	'posts'=>$posts,
		
));

以上是控制器中内容,要求评论是通过的,则有的日志没有评论,则该日志也不显示了。还有一个问题是 author 关系的select中只查找名字怎么感觉没啥用呢,得到的数据在视图页面如何访问author的内容

以下是我读取的方法在视图

<?php foreach ($posts as $post) :?>

<?php // print_r($post);exit(); ?>
<?php echo $post->author->id.$post['author']['username']."的所有日志如下:"?><br/><br/>
日志标题 :<?php echo $post->title ;?><br/>
日志内容 :<?php echo $post->content ;?><br/><br/><br/>

评论:《<?php echo $post->commentCount ;?><br/><br/>

<?php foreach ($post->comments  as $comment) :?>

内容——》<?php echo $comment->content ;?><br/><br/>
状态——》<?php echo $comment->status ;?><br/><br/>

<?php endforeach;?>
<?php endforeach;?>

感觉不太对,这样访问则所有字段都能读到,感觉是加载的关系,和控制器中写的无关

  • 回复于 2013-08-19 14:48 举报

    你在加上'together' => true试试
    ‘comments’=>array('together' => true)

您需要登录后才可以回复。登录 | 立即注册