php - Yii2 Call to a member function someFunction() on Integer and a non-object -


i wanna show usernames in timeline index. show error

call member function getstatuseslabel() on integer

if use code :

'status' =>$model->data['status']->getstatuseslabel(), 

and

'author' => $model->data['author']->getauthor(), 

and error

trying property of non-object

if use code :

'author' => arrayhelper::map($model->data['author']->getauthor, 'id','username'), 

my model

namespace common\models; .....   class article extends activerecord {    .....     public function getauthor()     {         return $this->hasone(user::classname(), ['id' => 'author_id']);     }       public function getupdater()     {         return $this->hasone(user::classname(), ['id' => 'updater_id']);     }       public function getcategory()     {         return $this->hasone(articlecategory::classname(), ['id' => 'category_id']);     }      public function getarticleattachments()     {         return $this->hasmany(articleattachment::classname(), ['article_id' => 'id']);     }      public static function statuses()     {         return [             self::status_published => yii::t('common', 'published'),             self::status_draft => yii::t('common', 'draft'),         ];     }      public function aftercreate()     {       $this->refresh();       // use common\commands\addtotimelinecommand;           yii::$app->commandbus->handle(new addtotimelinecommand([             'category' => 'articles',             'event' => '_item',             'data' => [               'title' => $this->title,               'published_at' => $this->published_at,               'created_at' => $this->created_at,               'slug' => $this->slug,               'author' => $this->author_id,               'category' => $this->category,               'status' => $this->status,             ]           ]));     }       public function afterupdate()     {       $this->refresh();       // use common\commands\addtotimelinecommand;           yii::$app->commandbus->handle(new addtotimelinecommand([             'category' => 'articles',             'event' => '_itemu',             'data' => [               'title' => $this->title,               'published_at' => $this->published_at,               'updated_at' => $this->updated_at,               'slug' => $this->slug,               'author' => $this->author_id,               'category' => $this->category,               'status' => $this->status,             ]           ]));     } } 

my controller article:

public function actioncreate() {     $model = new article();     $transaction = yii::$app->db->begintransaction();      try{     if ($model->load(yii::$app->request->post()) && $model->save()) {         $model->aftercreate();         $transaction->commit();         return $this->redirect(['index']);     } else {         return $this->render('create', [             'model' => $model,             'categories' => articlecategory::find()->active()->all(),         ]);     }   } catch (exception $e) {               $transaction->rollback();             } } 

controller timeline

public function actionindex()     {         $searchmodel = new timelineeventsearch();         $dataprovider = $searchmodel->search(yii::$app->request->queryparams);         $dataprovider->sort = [             'defaultorder'=>['created_at'=>sort_desc]         ];          return $this->render('index', [             'searchmodel' => $searchmodel,             'dataprovider' => $dataprovider,         ]); 

command timeline :

class addtotimelinecommand extends object implements selfhandlingcommand {     public $category;     public $event;     public $data;     public function handle($command)     {         $model = new timelineevent();         $model->application = yii::$app->id;         $model->category = $command->category;         $model->event = $command->event;         $model->data = json_encode($command->data, json_unescaped_unicode);         return $model->save(false);     } } 

index timeline:

<ul class="timeline">                 <?php foreach($dataprovider->getmodels() $model): ?>                     <?php if(!isset($date) || $date != yii::$app->formatter->asdate($model->created_at)): ?>                         <!-- timeline time label -->                         <li class="time-label">                             <span class="bg-blue">                                 <?php echo yii::$app->formatter->asdate($model->created_at) ?>                             </span>                         </li>                         <?php $date = yii::$app->formatter->asdate($model->created_at) ?>                     <?php endif; ?>                     <li>                         <?php                             try {                                 $viewfile = sprintf('%s/%s', $model->category, $model->event);                                 echo $this->render($viewfile, ['model' => $model]);                             } catch (\yii\base\invalidparamexception $e) {                                 echo $this->render('_item', ['model' => $model]);                             }                         ?>                     </li> 

view _item index:

<div class="timeline-body">         <?php echo yii::t('backend', 'updated post <b>({title})</b>, published date : {published_at} ', [             'title' => html::a($model->data['title'],             url::to(yii::$app->urlmanager->hostinfo.'/article'.'/')  .$model->data['slug'],             ['target' => '_blank']),             //'author' => arrayhelper::map($model->data['author']->getauthor, 'id','username'),             //'author' => $model->data['author']->getauthor(),             'author' => $model->data['author'],             'category' => $model->data['category'],             //'status' =>$model->data['status']->getstatuseslabel(),             'published_at' => yii::$app->formatter->asdatetime($model->data['published_at']),             'updated_at' => yii::$app->formatter->asdatetime($model->data['updated_at'])         ])//.html::a($model->data['title'], ["/article/$model->data['title']"]) ?>     </div> 

what wrong above code? use in gridview / detailview, there no errors how fix it?

in simple view (not based on gridview or detailview widget) render using eg:

  return $this->render('your_view', [         'model' => $model,     ]);  can use directly model , attribute (or depending function) eg:   'author' => $model->author 

if instead use

  return $this->render('your_view', [         'dataprovider' => $dataprovider,     ]); 

you can refer model instance

  'author' => $dataprovider->models[i]['author']  

where index specific instance


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -