php - 在 Yii2 中输出关系数据

标签 php yii model relationship yii2

我试图在Yii2中输出相关数据。我的输出代码如下所示(就像我在 Yii 1 中所做的那样):

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

但是我收到错误tying to get property on a non-object$model->comments的var_dump显示数组,看起来像:

    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

$model->getComments() 的 var_dump 返回 ActiveQuery 对象:

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

我在这两种情况下都看到了我的数据,但是我如何从这个构造中接收它们? ($model->getComments() 或 $model->comments 哪种方式是正确的?)

最佳答案

希望你一切都好! 您只需要对下面给出的代码进行小修改

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

希望这会让您开心。

关于php - 在 Yii2 中输出关系数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26452071/

相关文章:

php - 如何在 Yii 中使用 CListView 自定义 dataProvider

javascript - Three.js无法加载DAE模型

asp.net-mvc - MVC3 - 模型设置为空但仍在输入 IF 语句

php - 使用 ", "插入一个数组,并在最后一项之前添加 "and "

php - 使用 php 和 redis

php - 如何在 PHP 中规范化电子邮件地址?

php - 在 yii 1.1 中使用 bootstrap 3 (grid)) 的最佳方式是什么?

yii - 引用 yii 中的关系应用默认范围

ruby-on-rails - Rails 3 查找所有关联记录 has_many :through

php - 使用 system( ) 甚至 passthru( ) 从 php 运行 python 脚本不会产生任何输出。为什么?