Monday, 15 July 2013

php - What is type of Yii's relations in AR models -



php - What is type of Yii's relations in AR models -

i believe relations in ar models of yii returned array. book "web application development yii , php" seek utilize theme object instances , makes error. missing or understanding wrong or it's book's mistake?

for illustration in 'comment' ar model class have:

public function relations() { homecoming array( 'author' => array(self::belongs_to, 'user', 'create_user_id'), ); }

book refers "username" :

$comment->author->username

and utilize :

$comment->author['username']

which 1 correct?

update-> i'm gonna set related code here: ar model: /** * model class table "tbl_comment". * * followings available columns in table 'tbl_comment': * @property integer $id * @property string $content * @property integer $issue_id * @property string $create_time * @property integer $create_user_id * @property string $update_time * @property integer $update_user_id * * followings available model relations: * @property user $updateuser * @property issue $issue * @property user $createuser */ class comment extends trackstaractiverecord { /**book */ public function recent($limit=5) { $this->getdbcriteria()->mergewith(array( 'order'=>'t.create_time desc', 'limit'=>$limit,) ); homecoming $this; } /** * returns static model of specified ar class. * @param string $classname active record class name. * @return comment static model class */ public static function model($classname=__class__) { homecoming parent::model($classname); } /** * @return string associated database table name */ public function tablename() { homecoming 'tbl_comment'; } /** * @return array validation rules model attributes. */ public function rules() { // note: should define rules attributes // receive user inputs. homecoming array( array('content, issue_id', 'required'), array('issue_id, create_user_id, update_user_id', 'numerical', 'integeronly'=>true), array('create_time, update_time', 'safe'), // next rule used search(). // please remove attributes should not searched. array('id, content, issue_id, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { // note: may need adjust relation name , related // class name relations automatically generated below. homecoming array( 'updateuser' => array(self::belongs_to, 'user', 'update_user_id'), 'issue' => array(self::belongs_to, 'issue', 'issue_id'), 'author' => array(self::belongs_to, 'user', 'create_user_id'), ); } /** * @return array customized attribute labels (name=>label) */ public function attributelabels() { homecoming array( 'id' => 'id', 'content' => 'content', 'issue_id' => 'issue', 'create_time' => 'create time', 'create_user_id' => 'create user', 'update_time' => 'update time', 'update_user_id' => 'update user', ); } /** * retrieves list of models based on current search/filter conditions. * @return cactivedataprovider info provider can homecoming models based on search/filter conditions. */ public function search() { // warning: please modify next code remove attributes // should not searched. $criteria=new cdbcriteria; $criteria->compare('id',$this->id); $criteria->compare('content',$this->content,true); $criteria->compare('issue_id',$this->issue_id); $criteria->compare('create_time',$this->create_time,true); $criteria->compare('create_user_id',$this->create_user_id); $criteria->compare('update_time',$this->update_time,true); $criteria->compare('update_user_id',$this->update_user_id); homecoming new cactivedataprovider($this, array( 'criteria'=>$criteria, )); } }

widget component:

<?php /** * recentcommentswidget yii widget used display list of recent comments */ class recentcommentswidget extends cwidget { private $_comments; public $displaylimit = 5; public $projectid = null; public function init() { if(null !== $this->projectid) $this->_comments = comment::model()- >with(array('issue'=>array('condition'=>'project_id='.$this->projectid)))->recent($this->displaylimit)->findall(); else $this->_comments = comment::model()->recent($this->displaylimit)->findall(); } public function getdata() { homecoming $this->_comments; } public function run() { // method called ccontroller::endwidget() $this->render('recentcommentswidget'); } }

widget view:

<ul> <?php foreach($this->getdata() $comment): ?> <div class="author"> <?php echo $comment->author->username; ?> added comment. </div> <div class="issue"> <?php echo chtml::link(chtml::encode($comment->issue->name), array('issue/view', 'id'=>$comment->issue->id)); ?> </div> <?php endforeach; ?> </ul>

this code makes non-object error when change

$comment->author->username

to $comment->author['username'] works fine. wonder how it's work $issue object access method.

the recommended form is:

$comment->author->username

you can utilize code below...

$comment->author['username']

...because php handles object array.

php model yii

No comments:

Post a Comment