php - Trying to get property of non-object in Comments model -
i make example comment model page model. yesterday fine, when today go check comments have error http://pastebin.com/ve1g5q6n.
comment.php
<?php /** * model class table "{{comment}}". * * followings available columns in table '{{comment}}': * @property integer $id * @property string $content * @property integer $page_id * @property integer $created * @property integer $user_id * @property string $guest */ class comment extends cactiverecord { public $verifycode; /** * 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__) { return parent::model($classname); } /** * @return string associated database table name */ public function tablename() { return '{{comment}}'; } /** * @return array validation rules model attributes. */ public function rules() { // note: should define rules attributes // receive user inputs. return array( array('content', 'required'), array('content, guest', 'required', 'on'=>'guest'), array('page_id, created, user_id', 'numerical', 'integeronly'=>true), array('guest', 'length', 'max'=>255), array('content', 'safe'), // following rule used search(). // please remove attributes should not searched. array('verifycode', 'captcha', 'allowempty'=>!ccaptcha::checkrequirements(), 'on'=>'guest'), array('id, content, page_id, created, user_id, guest, status', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { // note: may need adjust relation name , related // class name relations automatically generated below. return array( 'user'=>array(self::belongs_to, 'user', 'user_id'), 'page'=>array(self::belongs_to, 'page', 'page_id'), ); } /** * @return array customized attribute labels (name=>label) */ public function attributelabels() { return array( 'id' => 'id', 'content' => 'Текст', 'page_id' => 'Страница', 'created' => 'Дата', 'user_id' => 'Пользователь', 'guest' => 'Имя (гость)', 'status' => 'Статус', ); } /** * retrieves list of models based on current search/filter conditions. * @return cactivedataprovider data provider can return models based on search/filter conditions. */ public function search() { // warning: please modify following code remove attributes // should not searched. $criteria=new cdbcriteria; $criteria->compare('id',$this->id); $criteria->compare('content',$this->content,true); $criteria->compare('page_id',$this->page_id); $criteria->compare('created',$this->created); $criteria->compare('user_id',$this->user_id); $criteria->compare('guest',$this->guest,true); $criteria->compare('status',$this->status,true); return new cactivedataprovider($this, array( 'criteria'=>$criteria, )); } public function beforesave() { if ($this->isnewrecord){ $this->created = time(); } if (!yii::app()->user->isguest) { $this->user_id = yii::app()->user->id; } return parent::beforesave(); } public static function getcomment($page_id) { $criteria = new cdbcriteria; $criteria->compare('page_id', $page_id); $criteria->compare('status', 1); $criteria->order = 'created desc'; return new cactivedataprovider('comment', array( 'criteria'=>$criteria, )); } } commentcontroller.php
<?php class commentcontroller extends controller { /** * @return array action filters */ public function filters() { return array( 'accesscontrol', // perform access control crud operations 'postonly + delete', // allow deletion via post request ); } /** * specifies access control rules. * method used 'accesscontrol' filter. * @return array access control rules */ public function accessrules() { return array( array('allow', // allow users perform 'index' , 'view' actions 'actions'=>array('index','view','create','update','delete'), 'roles'=>array('2'), ), array('deny', // deny users 'users'=>array('*'), ), ); } /** * displays particular model. * @param integer $id id of model displayed */ public function actionview($id) { $this->render('view',array( 'model'=>$this->loadmodel($id), )); } /** * creates new model. * if creation successful, browser redirected 'view' page. */ public function actioncreate() { $model=new comment; // uncomment following line if ajax validation needed // $this->performajaxvalidation($model); if(isset($_post['comment'])) { $model->attributes=$_post['comment']; if($model->save()) $this->redirect(array('view','id'=>$model->id)); } $this->render('create',array( 'model'=>$model, )); } /** * updates particular model. * if update successful, browser redirected 'view' page. * @param integer $id id of model updated */ public function actionupdate($id) { $model=$this->loadmodel($id); // uncomment following line if ajax validation needed // $this->performajaxvalidation($model); if(isset($_post['comment'])) { $model->attributes=$_post['comment']; if($model->save()) $this->redirect(array('view','id'=>$model->id)); } $this->render('update',array( 'model'=>$model, )); } /** * deletes particular model. * if deletion successful, browser redirected 'admin' page. * @param integer $id id of model deleted */ public function actiondelete($id) { $this->loadmodel($id)->delete(); // if ajax request (triggered deletion via admin grid view), should not redirect browser if(!isset($_get['ajax'])) $this->redirect(isset($_post['returnurl']) ? $_post['returnurl'] : array('index')); } /** * manages models. */ public function actionindex() { $model=new comment('search'); $model->unsetattributes(); // clear default values if(isset($_get['comment'])) $model->attributes=$_get['comment']; $this->render('index',array( 'model'=>$model, )); } /** * returns data model based on primary key given in variable. * if data model not found, http exception raised. * @param integer $id id of model loaded * @return comment loaded model * @throws chttpexception */ public function loadmodel($id) { $model=comment::model()->findbypk($id); if($model===null) throw new chttpexception(404,'the requested page not exist.'); return $model; } /** * performs ajax validation. * @param comment $model model validated */ protected function performajaxvalidation($model) { if(isset($_post['ajax']) && $_post['ajax']==='comment-form') { echo cactiveform::validate($model); yii::app()->end(); } } } view/index.php
<?php /* @var $this commentcontroller */ /* @var $model comment */ yii::app()->clientscript->registerscript('search', " $('.search-button').click(function(){ $('.search-form').toggle(); return false; }); $('.search-form form').submit(function(){ $('#comment-grid').yiigridview('update', { data: $(this).serialize() }); return false; }); "); ?> <h1>Журнал комментариев</h1> <?php echo chtml::link('Расширенный поиск','#',array('class'=>'search-button')); ?> <div class="search-form" style="display:none"> <?php $this->renderpartial('_search',array( 'model'=>$model, )); ?> </div><!-- search-form --> <?php $this->widget('zii.widgets.grid.cgridview', array( 'id'=>'comment-grid', 'dataprovider'=>$model->search(), 'filter'=>$model, 'columns'=>array( 'id'=>array( 'name'=>'id', 'headerhtmloptions'=>array('width'=>30), ), 'status'=>array( 'name'=>'status', 'value'=>'($data->status==1)?"Доступно":"Скрыто"', 'filter'=>array(0=>"Скрыто",1=>"Доступно"), ), 'content', 'page_id'=>array( 'name'=>'page_id', 'value'=>'$data->page->title', 'filter'=>page::getpage(), ), 'created'=>array( 'name'=>'created', 'value'=>'date("j.m.y. h:i", $data->created)', 'filter'=>false, ), 'user_id'=>array( 'name'=>'user_id', 'value'=>'$data->user->username', 'filter'=>user::getusername(), ), 'guest', array( 'class'=>'cbuttoncolumn', 'updatebuttonoptions'=>array('style'=>'display:none'), ), ), )); ?> view/view.php
<?php /* @var $this commentcontroller */ /* @var $model comment */ $this->menu=array( array('label'=>'Журнал комментариев', 'url'=>array('index')), array('label'=>'Удалить комментарий', 'url'=>'#', 'linkoptions'=>array('submit'=>array('delete','id'=>$model->id), 'confirm'=>'Вы уверены что хотите удалить этот комментарий?')), ); ?> <h1>Просмотр комментария #<?php echo $model->id; ?></h1> <?php $this->widget('zii.widgets.cdetailview', array( 'data'=>$model, 'attributes'=>array( 'id', 'content', 'page_id', 'created', 'user_id', 'guest', ), )); ?> view/_search.php
<?php /* @var $this commentcontroller */ /* @var $model comment */ /* @var $form cactiveform */ ?> <div class="wide form"> <?php $form=$this->beginwidget('cactiveform', array( 'action'=>yii::app()->createurl($this->route), 'method'=>'get', )); ?> <div class="row"> <?php echo $form->label($model,'id'); ?> <?php echo $form->textfield($model,'id'); ?> </div> <div class="row"> <?php echo $form->label($model,'status'); ?> <?php echo $form->dropdownlist($model,'status', array(''=>'',0=>"Скрыто",1=>"Доступно")); ?> </div> <div class="row"> <?php echo $form->label($model,'content'); ?> <?php echo $form->textarea($model,'content',array('rows'=>6, 'cols'=>50)); ?> </div> <div class="row"> <?php echo $form->label($model,'page_id'); ?> <?php echo $form->dropdownlist($model,'page_id',page::getpage(),array('empty'=>'')); ?> </div> <div class="row"> <?php echo $form->label($model,'user_id'); ?> <?php echo $form->dropdownlist($model,'user_id',user::getusername(),array('empty'=>'')); ?> </div> <div class="row"> <?php echo $form->label($model,'guest'); ?> <?php echo $form->textfield($model,'guest',array('size'=>60,'maxlength'=>255)); ?> </div> <div class="row buttons"> <?php echo chtml::submitbutton('Поиск'); ?> </div> <?php $this->endwidget(); ?> </div><!-- search-form -->
this issue being generated when yii tries evaluate code have inside gridview column values. make sure each property you're referencing exists on $data.
since issue started happening, guess culprit "value" => "$data->page->title". there's new record doesn't have page relationship, so$data->page null.
edit
actually, didn't notice before, error output highlights property reference ccomponent->evaluateexpression("$data->user->username", array("data" => comment, "row" => 0)). same reasons described above, $data->user null.
Comments
Post a Comment