controller code
public function actionIndex() {
$posts = Post::model()->with(
array(
'author'=>array(
'select'=>'username',
),
'comments'=>array(
//if the post do not has a comment,then the post can not show ,why? I just want the comment's status is APPROVED.
'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,
));
}
view code
<?php foreach ($posts as $post) :?>
<?php // print_r($post);exit(); ?>
<?php echo $post->author->id.$post['author']['username']."posts:"?><br/><br/>
title:<?php echo $post->title ;?><br/>
content :<?php echo $post->content ;?><br/><br/><br/>
评论:《<?php echo $post->commentCount ;?>》<br/><br/>
<?php foreach ($post->comments as $comment) :?>
content——》<?php echo $comment->content ;?><br/><br/>
status——》<?php echo $comment->status ;?><br/><br/>
<?php endforeach;?>
<?php endforeach;?>
How to write the view to access the data? I use select but do not have a effect.
how to use the join,I use it but do not know how to show the numeric field.
who can help me write the demo code
Page 1 of 1
Model Bug,relation With
#2
Posted 27 September 2013 - 09:36 PM
/* Moved from "Bug Discussions" to "General Discussion for Yii 1.1.x" */
#3
Posted 28 September 2013 - 08:25 AM
Hi new_yii, welcome to the forum.
As for the first question ...
if the post do not has a comment, then the post can not show, why?
I just want the comment's status is APPROVED.
I think it's because your are using the 'condition' option of the relation.
Try 'on' option instead, I hope it will do the job.
http://www.yiiframew...elations-detail
As for the first question ...
Quote
public function actionIndex() { $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, )); }
if the post do not has a comment, then the post can not show, why?
I just want the comment's status is APPROVED.
I think it's because your are using the 'condition' option of the relation.
Try 'on' option instead, I hope it will do the job.
'comments'=>array( 'on'=>'comments.status='.Comment::STATUS_APPROVED, 'order'=>'comments.create_time DESC' ),
http://www.yiiframew...elations-detail
Share this topic:
Page 1 of 1