2
votes

I trying to output related data in Yii2. My output code looks like this(like i do that in Yii 1):

foreach ($model->comments as $key => $comment) {
    echo $comment->title;
}

But i get the error tying to get property on a non-object, var_dump of $model->comments show the array, what look like:

    array (size=1)
  0 => 
    object(common\models\Comment)[74]
      private '_attributes' (yii\db\BaseActiveRecord) => 
        array (size=5)
          'id' => int 1
          'title' => string 'testComment' (length=11)
          'body' => string 'body' (length=4)
          'post_id' => int 1
          'user_id' => int 1
      private '_oldAttributes' (yii\db\BaseActiveRecord) => 
        array (size=5)
          'id' => int 1
          'title' => string 'testComment' (length=11)
          'body' => string 'body' (length=4)
          'post_id' => int 1
          'user_id' => int 1
      private '_related' (yii\db\BaseActiveRecord) => 
        array (size=0)
          empty

var_dump of $model->getComments() return the ActiveQuery object:

object(yii\db\ActiveQuery)[67]
  public 'sql' => null
  public 'on' => null
  public 'joinWith' => null
  public 'select' => null
  public 'selectOption' => null
  public 'distinct' => null
  public 'from' => null
  public 'groupBy' => null
  public 'join' => null
  public 'having' => null
  public 'union' => null
  public 'params' => 
    array (size=0)
      empty
  private '_events' (yii\base\Component) => 
    array (size=0)
      empty
  private '_behaviors' (yii\base\Component) => 
    array (size=0)
      empty
  public 'where' => null
  public 'limit' => null
  public 'offset' => null
  public 'orderBy' => null
  public 'indexBy' => null
  public 'modelClass' => string 'common\models\Comment' (length=21)
  public 'with' => null
  public 'asArray' => null
  public 'multiple' => boolean true
  public 'primaryModel' => 
    object(common\models\Post)[65]
      private '_attributes' (yii\db\BaseActiveRecord) => 
        array (size=5)
          'id' => int 1
          'header' => string 'ds' (length=2)
          'content' => string 'dsad' (length=4)
          'created' => string '0000-00-00' (length=10)
          'status' => int 1
      private '_oldAttributes' (yii\db\BaseActiveRecord) => 
        array (size=5)
          'id' => int 1
          'header' => string 'ds' (length=2)
          'content' => string 'dsad' (length=4)
          'created' => string '0000-00-00' (length=10)
          'status' => int 1
      private '_related' (yii\db\BaseActiveRecord) => 
        array (size=0)
          empty
      private '_errors' (yii\base\Model) => null
      private '_validators' (yii\base\Model) => null
      private '_scenario' (yii\base\Model) => string 'default' (length=7)
      private '_events' (yii\base\Component) => 
        array (size=0)
          empty
      private '_behaviors' (yii\base\Component) => 
        array (size=0)
          empty
  public 'link' => 
    array (size=1)
      'post_id' => string 'id' (length=2)
  public 'via' => null
  public 'inverseOf' => null

I see my data in both cases, but how i can recieve them from this constructs? (and what way $model->getComments() or $model->comments is wright?)

1
Can you do a var_dump of $comment as well? - Barry
You can use either $model->comments or $model->getComments()->All(). - Barry

1 Answers

4
votes

Hope you are doing well !!! You just required to do the small revision in your code which is given below

foreach ($model[0]->comments as $key => $comment) {
    echo $comment->title;
}

Hope this will make your day.