Model Bug,relation With

[size="5"]controller code[/size]

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,


	


	));


	


}

[size="5"]view code[/size]

<?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;?>

[b]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[/b]

[color="#556B2F"]/* Moved from "Bug Discussions" to "General Discussion for Yii 1.1.x" */[/color]

Hi new_yii, welcome to the forum.

As for the first question …

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.yiiframework.com/doc/api/1.1/CActiveRecord#relations-detail