What is $data in _view.php of blog (demos folder) of yii framework is?

Go To StackoverFlow.com

1

I confuse about $data in blog/views/post/_view.php of blog demo of yii framework How I get this attribute of $data? Where I can find the define of $data? I find in source but doesn't see any line about author->username ,url commentCount?

<div class="post">
    <div class="title">
        <?php echo CHtml::link(CHtml::encode($data->title), $data->url); ?>
    </div>
    <div class="author">
        posted by <?php echo $data->author->username . ' on ' . date('F j, Y',$data->create_time); ?>
    </div>
    <div class="content">
        <?php
            $this->beginWidget('CMarkdown', array('purifyOutput'=>true));
            echo $data->content;
            $this->endWidget();
        ?>
    </div>
    <div class="nav">
        <b>Tags:</b>
        <?php echo implode(', ', $data->tagLinks); ?>
        <br/>
        <?php echo CHtml::link('Permalink', $data->url); ?> |
        <?php echo CHtml::link("Comments ({$data->commentCount})",$data->url.'#comments'); ?> |
        Last updated on <?php echo date('F j, Y',$data->update_time); ?>
    </div>
</div>

Can you help me to explain or give me some link or some keywork. Thank for everything!

2012-04-04 19:17
by chanhle


3

See documentation for CListView:

The above code first creates a data provider for the Post ActiveRecord class. It then uses CListView to display every data item as returned by the data provider. The display is done via the partial view named '_post'. This partial view will be rendered once for every data item. In the view, one can access the current data item via variable $data. For more details, see itemView.

So in your context, $data is the Post being rendered.

2012-04-04 19:29
by DCoder
but in the model of Post or PostController I dont see attributes: author->username, url ... - chanhle 2012-04-05 07:15
@chanhle: Yii automatically reads the DB schema associated with each Active Record class and creates magic attributes for every column. Table relations specified in the relations() function will also be available as magic attributes, but be careful about lazy loading. So $data->url refers to the value of that post's url field in the database, and $data->author->username will fetch the author of the post and return its username field. Check out http://www.yiiframework.com/doc/guide/1.1/en/database.ar and http://www.yiiframework.com/doc/guide/1.1/en/database.arr - DCoder 2012-04-05 07:24
What about $data->tagLinks ? where does that come from - Samson 2012-11-21 14:44
@Samson: that involves a little bit of magic. In Yii, when you say $data->tagLinks and $data does not have a property named tagLinks, it will try to call $data->getTagLinks() instead. That is exactly what happens here, the Post model contains a function getTagLinks() that gets called - DCoder 2012-11-21 15:45
nice one..I wondered who calls that method. - Samson 2012-11-21 15:47


2

$data is an object of model class having single row of data. author is a relation to another model of the model whose instance is $data. $data->author->username Here username is a variable of the model whom author points. $data->author also treated as an object. It will just execute a relational query to the model whom author points.

You can see the relations of the model in relations() function of the model. Try out guide of yii you will find your answer.

2012-04-05 10:00
by Onkar Janwa
relation is this 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( 'author' => array(self::BELONGSTO, 'User', 'authorid'), 'comments' => array(self::HASMANY, 'Comment', 'postid', 'condition'=>'comments.status='.Comment::STATUSAPPROVED, 'order'=>'comments.createtime DESC'), 'commentCount' => array(self::STAT, 'Comment', 'postid', 'condition'=>'status='.Comment::STATUSAPPROVED), ); - chanhle 2012-04-05 14:54
Ads