gpt4 book ai didi

php - yii2中使用 `with`的嵌套关系

转载 作者:行者123 更新时间:2023-12-04 00:06:42 26 4
gpt4 key购买 nike

谁能告诉我,我有几个模型:一个Item有很多→Properties,On Property有很多→Characteristics我可以这样做:

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

结果:

{
title: "Ванна чугунная Ностальжи 150 с ножками",
new: "0",
hit: "0",
recommend: "0",
properties: [
{
//lallala
},
{
//lallala
},
]
}

但我想要嵌套行(特征)在每个属性中我怎样才能做到这么优雅?

最佳答案

来自 Yii guide on working with databases :

You can eagerly load deeply nested relations, such as a.b.c.d. All parent relations will be eagerly loaded. That is, when you call with() using a.b.c.d, you will eagerly load a, a.b, a.b.c and a.b.c.d.

所以使用properties.characteristics:

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

关于php - yii2中使用 `with`的嵌套关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40304715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com