如何在yii2中的不同表上获得两个左连接?

时间:2022-09-10 11:59:21

I have 3 tables (companies, services, params). The relations between the tables is as follows: a company has many services, a service has many params, a service belongs to one company. I'm trying to join the companies with the services and the services with the params and return it as a json.

我有3张桌子(公司,服务,参数)。表之间的关系如下:公司有很多服务,服务有很多参数,服务属于一个公司。我正试图通过params加入服务和服务的公司,并将其作为json返回。

my code is:

我的代码是:

$query = Companies::find()
            ->joinWith('services')
            ->leftJoin('params', '`services`.`id` = `params`.`serviceid`')
            ->asArray()->all();
return $query;  

However in the json i'm getting the relation between companies and services is working but the relation between services and params is not.

然而,在json我得到公司和服务之间的关系是有效的,但服务和params之间的关系不是。

If it helps this is the json i'm getting: 如何在yii2中的不同表上获得两个左连接?

如果它有助于这是我得到的json:

Can anyone please help me? It looks like i'm missing something basic but can't figure out what it is. Thanks

谁能帮帮我吗?看起来我错过了一些基本但却无法弄清楚它是什么的东西。谢谢

2 个解决方案

#1


0  

Did you set up also relations between those models ? It is much more easier to use, especialy if you need to use that relation on more than just one place.

你是否也建立了这些模型之间的关系?使用起来要容易得多,特别是如果你需要在一个以上的地方使用这种关系。

Yii2 - Working with Relational Data

Yii2 - 使用关系数据

And then just use ActiveQuery with() method

然后只使用ActiveQuery with()方法

#2


0  

I found the solution. It was a nested relation issue, instead of:

我找到了解决方案。这是一个嵌套的关系问题,而不是:

$query = Companies::find()
            ->joinWith('services')
            ->leftJoin('params', '`services`.`id` = `params`.`serviceid`')
            ->asArray()->all();
return $query;

the query should look like this:

查询应如下所示:

$query = Companies::find()
            ->with('services.params')
            ->asArray()->all();
return $query;

Yii2 understands the nested relations automatically if it's correctly set in your models. Thanks to this post Nested relations using `with` in yii2

如果在模型中正确设置,Yii2会自动理解嵌套关系。感谢这个帖子在yii2中使用`with`的嵌套关系

Cheers!

干杯!

#1


0  

Did you set up also relations between those models ? It is much more easier to use, especialy if you need to use that relation on more than just one place.

你是否也建立了这些模型之间的关系?使用起来要容易得多,特别是如果你需要在一个以上的地方使用这种关系。

Yii2 - Working with Relational Data

Yii2 - 使用关系数据

And then just use ActiveQuery with() method

然后只使用ActiveQuery with()方法

#2


0  

I found the solution. It was a nested relation issue, instead of:

我找到了解决方案。这是一个嵌套的关系问题,而不是:

$query = Companies::find()
            ->joinWith('services')
            ->leftJoin('params', '`services`.`id` = `params`.`serviceid`')
            ->asArray()->all();
return $query;

the query should look like this:

查询应如下所示:

$query = Companies::find()
            ->with('services.params')
            ->asArray()->all();
return $query;

Yii2 understands the nested relations automatically if it's correctly set in your models. Thanks to this post Nested relations using `with` in yii2

如果在模型中正确设置,Yii2会自动理解嵌套关系。感谢这个帖子在yii2中使用`with`的嵌套关系

Cheers!

干杯!