Yii2合并查询,例如yii1中的cdbcriteria [英] Yii2 merge queries like cdbcriteria in yii1

查看:275
本文介绍了Yii2合并查询,例如yii1中的cdbcriteria的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在合并多个查询时遇到问题. 在Yii 1.x中,您可以将CDbCriteria与

I have a problem regarding merge of multiple queries. In Yii 1.x you could merge a CDbCriteria with

$criteria->merge($otherCriteria)

如何通过Yii2中的查询实现相同的嵌套条件?

How can I achieve the same nested conditions etc with queries in Yii2?

修改: 假设我想要单独的查询来形成子查询.在完成所有子查询之后,我想将它们合并到一个大查询中.

Let's say I want separate queries to form subqueries. And after all subqueries are done I want to merge them together to the one big query.

推荐答案

Yii2中不再有CDbCriteria概念.相反,您可以参考以下类:

There is no CDbCriteria concept in Yii2 anymore. Instead you can refer to the following classes:

  • http://www.yiiframework.com/doc-2.0/yii-db-query.html (yii\db\Query)
  • http://www.yiiframework.com/doc-2.0/yii-db-activequery.html (yii\db\ActiveQuery)

您之前使用CDbCriteria所做的所有操作现在都可以使用上述类.因此,无需将两个条件相互合并.

All you did before with CDbCriteria now you can do with above classes. So, there will be no need to merge two criteria with each other.

更新

Yii2还支持以下子查询(如Yii2的官方指南):

Yii2 also supports sub-queries like below (as Yii2's official guide):

$subQuery = (new Query)->select('COUNT(*)')->from('user');
$query = (new Query)->select(['id', 'count' => $subQuery])->from('post');

这将导致:

SELECT `id`, (SELECT COUNT(*) FROM `user`) AS `count` FROM `post`

http://www.yiiframework. com/doc-2.0/guide-db-query-builder.html#building-query

这篇关于Yii2合并查询,例如yii1中的cdbcriteria的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆