How do I pass model data to view instance?

Ok, I’ve read through the book, tutorials and forum posts, but I can’t seem to get this right. I’m making a chat room application where Comment is the main model. I have it working, except that now I’m trying to make a “liveindex” view for the comments and I want to pass the comment model data to a foreach similar to how its done in the _comment partial view in the blog tutorial. Here are the parts that I have so far relevant to this problem:

model/Comment.php relations function




    const STATUS_ENABLED=1;

    const STATUS_DISABLED=0;


[..snipped code here..]


    public function relations()

    {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'createUser' => array(self::BELONGS_TO, 'User', 'create_user_id'),

            'updateUser' => array(self::BELONGS_TO, 'User', 'update_user_id'),

            'comments' => array(self::HAS_MANY, 'Comment', 'id', 'condition'=>'comments.status='.Comment::STATUS_ENABLED, 'order'=>'comments.create_time DESC'),


        );

    }



CommentController.php actionLiveindex function


    public function actionLiveindex()

    {

        $dataProvider=new CActiveDataProvider('Comment', array(

            'criteria'=>array(

                'condition'=>'status=1',

                'order'=>'create_time DESC',

            ),

        ));

        $model=new Comment;

        if(isset($_POST['Comment']))

        {

            $model->attributes=$_POST['Comment'];

            $model->status = 1; # Default the comment to "Enabled"

            $model->save();

        }


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

            'dataProvider'=>$dataProvider,

            'model'=>$model,

        ));

    }



views/comment/liveindex.php


<?php

$this->breadcrumbs=array(

    'Comments',

);


$this->menu=array(

    array('label'=>'Create Comment', 'url'=>array('create')),

    array('label'=>'Manage Comment', 'url'=>array('admin')),

);

?>


<?php echo $this->renderPartial('_form',array( 'model'=>$model, 'rows'=>2)); ?>


<h2>Comments</h2>


<div id="comments">

    <?php if($model->count()>=1): ?>

        <h3>

            <?php echo $model->count() . ' comment(s)'; ?>

        </h3>


        <?php $this->renderPartial('_comments',array(

            'comments'=>$dataProvider,

        )); ?>

    <?php endif; ?>

</div>

views/comment/_comments.php


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

<div class="comment" id="c<?php echo $comment->id; ?>">


    <?php echo "#{$comment->id}"; ?>


    <div class="author">

        <?php echo $comment->createUser; ?> says:

    </div>


    <div class="time">

        <?php echo date('F j, Y \a\t h:i a',$comment->create_time); ?>

    </div>


    <div class="content">

        <?php echo nl2br(CHtml::encode($comment->content)); ?>

    </div>


</div><!-- comment -->

<?php endforeach; ?>



So when I try to go to I get the following error:

If I remove the $comment->createUser line from the _comments.php view, then it just says that create_time is not defined. So $comment is not getting set to a model data instance. Any ideas?

In views/comment/liveindex.php file i think


<?php $this->renderPartial('_comments',array(

            'comments'=>$dataProvider,

        )); ?>



because $dataProvider is object of CActiveDataProvider class, not object of Comment class. so you should write


<?php $this->renderPartial('_comments',array(

            'comments'=>$model,

        )); ?>

because $model is object of Comment class. Hope it helps

Thanks for replying. I must apologize because I accidentally left my code in the _comments view with $comments->id instead of $comment->id. I fixed that in my post and in my code. And I changed comments to be set from $model, but this doesn’t fix it, now its giving the error:

Referring to the first line where it calls $comment->id in the view.

Expanding on the problem a bit, if I do this in the _comment.php view it works, but its not really the way I think it should be done and it doesn’t give me the advantages of the model framework:




$comments = Comment::model()->findAll();

$arr = array();

foreach($comments as $t) {

    $arr[$t->id] = $t->attributes;

}


foreach($arr as $comment): ?>

<div class="comment" id="c<?php echo $comment['id']; ?>">


    <?php echo "#{$comment['id']}"; ?>


    <div class="author">

        <?php echo $comment['create_user_id']; ?> says:

    </div>


    <div class="time">

        <?php echo $comment['create_time']; ?>

    </div>


    <div class="content">

        <?php echo nl2br(CHtml::encode($comment['content'])); ?>

    </div>

</div><!-- comment -->

<?php endforeach; ?>



Bump. I’m still looking for a more Yii/MVC like solution to this. Thanks.

Is this really that hard of a problem to solve? Here I thought I was asking a beginner’s question.

Do it like in post #3:




<?php foreach(Comment::model()->findAll() as $comment): ?>

<div class="comment" id="c<?php echo $comment->id; ?>">


    <?php echo "#{$comment->id}"; ?>


    <div class="author">

        <?php echo $comment->createUser->id; // or ->name <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' /> ?> says:

    </div>


    <div class="time">

        <?php echo date('F j, Y \a\t h:i a',$comment->create_time); ?>

    </div>


    <div class="content">

        <?php echo nl2br(CHtml::encode($comment->content)); ?>

    </div>


</div><!-- comment -->

<?php endforeach; ?>



If createUser doesn’t work, then you have no relation setup for the Comment AR class.

Oh I see, I wasn’t including the whole $comment->createUser->username part (was missing ->username). That fixed it. Thank you very much. I moved the Comment::model()->findAll() part back to the controller as that seems to be the right place for it.