Yii2获取关系模型的特定属性

Chi*_*may 3 yii2 yii2-advanced-app

我有两个模型Onlinelearning和Onlinelearningreviews与HAS MANY关系.我想获得Onlinelearningreviews模型的特定属性.

在线学习属性: ID, Title, URL, Description, GradeAge

Onlinelearningreviews属性: ID, OnlinelearningID, Rating, Comments

模型在线学习:

public function getOnlinelearningreviews()
{
    return $this->hasMany(Onlinelearningreviews::className(), ['OnlineLearningID' => 'ID']);
}
Run Code Online (Sandbox Code Playgroud)

模型在线学习评论:

 public function getOnlineLearning()
 {
        return $this->hasOne(Onlinelearning::className(), ['ID' => 'OnlineLearningID']);
 }
Run Code Online (Sandbox Code Playgroud)

我想得到Title, URL, Description, GradeAge and Rating属性.

以下作品:

Onlinelearning::find()->select(['Title','URL','Description','GradeAge'])->with('onlinelearningreviews')->asArray()->all();
Run Code Online (Sandbox Code Playgroud)

但是当我指定评级时,它会给我错误

Onlinelearning::find()->select(['Title','URL','Description','GradeAge','onlinelearningreviews.Rating'])->with('onlinelearningreviews')->asArray()->all();
Run Code Online (Sandbox Code Playgroud)

如何从Onlinelearningreviews模型中仅获取Rating属性?我不想要其他属性.

Onlinelearning::find()->with(['onlinelearningreviews'])->asArray()->all() 打印:

 Array
    (
        [0] => Array
            (
                [ID] => 1
                [Title] => Udemy
                [URL] => http://www.google.com
                [Description] => Lorem Ipsum is simply dummy text of the printing and typesetting industry.
                [GradeAge] => 2nd to 5th Grade
                [AddedOn] => 2015-03-20 00:00:00
                [LastModifiedOn] => 2015-03-20 00:00:00
                [onlinelearningreviews] => Array
                    (
                        [0] => Array
                            (
                                [ID] => 1
                                [ParentID] => 1
                                [OnlineLearningID] => 1
                                [Rating] => 3.5
                                [PositiveComments] => It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. .
                                [NegativeComments] => 
                                [AddedOn] => 
                                [LastModifiedOn] => 
                            )

                        [1] => Array
                            (
                                [ID] => 2
                                [ParentID] => 1
                                [OnlineLearningID] => 1
                                [Rating] => 3.5
                                [PositiveComments] => It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. 
                                [NegativeComments] => 
                                [AddedOn] => 
                                [LastModifiedOn] => 
                            )

                    )

            )
)
Run Code Online (Sandbox Code Playgroud)

Onlinelearning::find()
        ->select(['Title','URL','Description','GradeAge'])
        ->with([
            'onlinelearningreviews' => function ($query) {
                /* @var $query yii\db\ActiveQuery */

                $query->select('Rating');
            },
        ])
        ->asArray()
        ->all();
Run Code Online (Sandbox Code Playgroud)

打印:

Array
(
    [0] => Array
        (
            [Title] => Udemy
            [URL] => http://www.google.com
            [Description] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
            [GradeAge] => 2nd to 5th Grade
            [onlinelearningreviews] => Array
                (
                )

        )
)
Run Code Online (Sandbox Code Playgroud)

aro*_*hev 6

这可能是这样的:

Onlinelearning::find()
    ->select(['Title','URL','Description','GradeAge'])
    ->with([
        'onlinelearningreviews' => function ($query) {
            /* @var $query \yii\db\ActiveQuery */

            $query->select('Rating');
        },
    ])
    ->asArray()
    ->all();
Run Code Online (Sandbox Code Playgroud)

请参阅有关如何自定义关系查询的官方文档with().

如果要从Onlinelearning模型中选择所有属性,可以省略select部分:

->select(['Title','URL','Description','GradeAge'])
Run Code Online (Sandbox Code Playgroud)

更新:

似乎您应该在两个模型中包含连接属性:

->select(['ID', 'Title','URL','Description','GradeAge'])
Run Code Online (Sandbox Code Playgroud)

$query->select(['OnlinelearningID', 'Rating']);
Run Code Online (Sandbox Code Playgroud)

否则Undefinex index将抛出异常.