Yii Framework Forum: Model Bug,relation With - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Model Bug,relation With Rate Topic: -----

#1 User is offline   new_yii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 16-August 13

Posted 16 August 2013 - 02:00 AM

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

0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 27 September 2013 - 09:36 PM

/* Moved from "Bug Discussions" to "General Discussion for Yii 1.1.x" */
0

#3 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 28 September 2013 - 08:25 AM

Hi new_yii, welcome to the forum.

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users