在yii2中使用`with`的嵌套关系

Ado*_*obe 2 php yii2

谁能告诉我,我有几个模式:一种产品有很多→ 属性,在属性有许多→ 特点 ,我可以这样做:

return Item::find()->where(['code' => $code])->with('properties')->asArray()->one();
Run Code Online (Sandbox Code Playgroud)

结果:

{
  title: "????? ???????? ????????? 150 ? ???????",
  new: "0",
  hit: "0",
  recommend: "0",
  properties: [
  {
    //lallala
  },
  {
    //lallala
  },
  ]
}
Run Code Online (Sandbox Code Playgroud)

但我想在每个属性中嵌套行(特征)我怎么能做那么优雅的方式?

top*_*her 7

从使用数据库的Yii指南:

您可以急切地加载深层嵌套的关系,例如a.b.c.d.所有的父母关系都会被急切地加载.也就是说,当你调用with()使用a.b.c.d,你会急切地加载a,a.b,a.b.c和a.b.c.d.

所以使用properties.characteristics:

return Item::find()->where(['code' => $code])->with('properties.characteristics')->asArray()->one()
Run Code Online (Sandbox Code Playgroud)