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!
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.
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
$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
$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.