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

Yii2获取关系模型的特定属性,yii2,yii2-advanced-app,Yii2,Yii2 Advanced App,我有两个模型Onlinelearning和Onlinelearning评论,它们之间有很多关系。我想获得Onlinelearningreviews模型的特定属性 Onlinelearning属性:ID、标题、URL、说明、年级 Onlinelearningreviews属性:ID、OnlinelearningID、评级、评论 仅限车型学习: public function getOnlinelearningreviews() { return $this->hasMany(Onli

我有两个模型Onlinelearning和Onlinelearning评论,它们之间有很多关系。我想获得Onlinelearningreviews模型的特定属性

Onlinelearning属性:
ID、标题、URL、说明、年级

Onlinelearningreviews属性:
ID、OnlinelearningID、评级、评论

仅限车型学习:

public function getOnlinelearningreviews()
{
    return $this->hasMany(Onlinelearningreviews::className(), ['OnlineLearningID' => 'ID']);
}
 public function getOnlineLearning()
 {
        return $this->hasOne(Onlinelearning::className(), ['ID' => 'OnlineLearningID']);
 }
仅限车型收入评论:

public function getOnlinelearningreviews()
{
    return $this->hasMany(Onlinelearningreviews::className(), ['OnlineLearningID' => 'ID']);
}
 public function getOnlineLearning()
 {
        return $this->hasOne(Onlinelearning::className(), ['ID' => 'OnlineLearningID']);
 }
我想要获得
标题、URL、描述、成绩和评级属性

以下工作:

Onlinelearning::find()->select(['Title','URL','Description','GradeAge'])->with('onlinelearningreviews')->asArray()->all();
但当我指定评级时,它会给我错误

Onlinelearning::find()->select(['Title','URL','Description','GradeAge','onlinelearningreviews.Rating'])->with('onlinelearningreviews')->asArray()->all();
如何从Onlinelearningreviews模型中仅获得评级属性?我不想要其他属性

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] => 
                            )

                    )

            )
)

印刷品:

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
                (
                )

        )
)

有可能是这样的:

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

            $query->select('Rating');
        },
    ])
    ->asArray()
    ->all();
请参见
with()
中有关如何自定义关系查询的内容

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

->select(['Title','URL','Description','GradeAge'])
更新:

public function getOnlinelearningreviews()
{
    return $this->hasMany(Onlinelearningreviews::className(), ['OnlineLearningID' => 'ID']);
}
 public function getOnlineLearning()
 {
        return $this->hasOne(Onlinelearning::className(), ['ID' => 'OnlineLearningID']);
 }
似乎您应该在两个模型中都包含连接属性:

->select(['ID', 'Title','URL','Description','GradeAge'])


否则,将抛出关于
Undefinex index
的异常。

@arogachev:我需要Onlinelearning中的所有属性和Onlinelearningreviews中的唯一评级属性。不想获取我没有用的不必要的属性。你想要平均评级还是所有评级?…@soju:我需要所有评级我没有得到任何错误,但数组是空的。我已将输出添加到问题中。用备注更新了答案。但奇怪的是,空数组毫无例外地被返回了。不,尽管仍然得到空数组,但它对我有效。稍后将尝试进行更深入的调查。已选择忘记包含ID。愚蠢的错误。在那之后,它起了作用。谢谢:)