如何在 Yii2 中获得 M:1 关系

How to get M:1 relation in Yii2

我正在尝试获取相关模型,但我似乎找不到正确的文档。在 yii 1.x 我可以做 $jobsprocess->category0,但是 yii 2.x 告诉我做 $jobsprocess->getCategory()。这不是 return 模型,而是 ActiveQuery。我怎样才能 return 模型对象?

在您的查询中使用 $model = YourModel::find()->with(['category])->all()

所有使用 getRelation() 函数的关系都可以通过 with() 函数访问,但没有 get 和小写首字母。

然后您可以使用 $model->category 访问关系数据。