Yii 向 CDbCriteria 添加一个 select
Yii add one more select to CDbCriteria
我对 Yii 比较陌生。
我对原始 SQL 充满信心,但在谈到原始时仍然有点迷茫
ORM。所以这可能是一个虚拟问题。
我已经检索了构建 CDbCriteria
:
所需的所有记录
$criteria = new CDbCriteria(array(
'select' => 'sum(items) as items',
// 'condition' => 't.items > 0 and order.storage = "'Product::STORAGE_LOCAL . '"',
'condition' => 't.items > 0 and order.storage = "' . Product::STORAGE_LOCAL . '"',
'order' => 'sum(items) DESC',
'with' => array(
'product' => array(
'select' => 'code, title, producer, local_rest',
**// 'select' => 'code, title, producer, sum(local_rest) as local_rest',**
'group' => 'product.code',
)
),
'join' => 'inner join `order` `order` on `t`.`order_id` = `order`.`id`',
// 'group' => '`product`.`code`, `product`.`title`',
'group' => '`product`.`code`',
'together' => true
));
我尝试通过 group by
获取 local_rest
字段的总和。不幸的是,它没有 return 它应该是什么。
这就是我试图将其构建到 CDbCriteria
中的方式:
// 'select' => 'code, title, producer, sum(local_rest) as local_rest',
。
- 运气不好。
我可以使用单独的查询来获取它:
$sum_local_rest = Yii::app()->db->createCommand("
SELECT id,code, title, sum(local_rest) as sum_rest FROM product GROUP BY code
ORDER BY `sum_rest` DESC
");
请注意 - 产品 table 中存在重复记录。
IE。我们不止一次拥有相同的产品。但是如果我使用 GROUP BY
它有助于划定这个缺点。这是由于糟糕的数据库设计造成的,应该在将来修复。
问题是我需要以某种方式将它与 CDbCriteria
绑定,因为它被 CDataProvider
使用,而 CDataProvider
被 GridView
使用。
关于如何将这两个问题合而为一的任何提示CDbCriteria
?
提前致谢
编辑
看着目前的答案,我觉得我需要总结一下。主要问题是我需要告诉 CDbCriteria
检索记录(由 HAS_Many 连接绑定)并计算所有这些记录的 SUM 并让 CDbCriteria
对这些记录进行 GROUP BY .
没有其他办法了。我不能明确地做到这一点。因为我将 CDbCriteria
传递给 CDataProvider
并且它应该 运行 查询。这就是 Yii 中的工作方式(据我所知)。
//You can merge your criteria like here:
$criteria = new CDbCriteria(); //First criteria
$criteria_2 = new CDbCriteria(); //Second criteria
$criteria->mergeWith($criteria_2); //Merge criteria and criteria_2
SomeModel::model()->findAll($criteria); //Find by criteria
您也不必将所有元素都传递给标准。尝试将标准拆分为更多代码,如下所示:
$criteria = new CDbCriteria();
$criteria->select = 'sum(items) as items, ' . Product::STORAGE_LOCAL;
$criteria->condition = 't.items > 0 and order.storage = ' . Product::STORAGE_LOCAL;
//etc.
我不明白为什么这样的东西不起作用:
$criteria = new CDbCriteria;
$criteria->select = array(
"SUM(t.items) as items",
"SUM(product.local_rest) as product_local_rest"
);
$criteria->condition = "t.items > 0 and order.storage = "' . Product::STORAGE_LOCAL . '"';
$criteria->join = 'inner join `order` `order` on `t`.`order_id` = `order`.`id`';
$criteria->with = "product";
$criteria->group = "product.code";
$criteria->together = true;
由于您将 together
设置为 true,您的关系的列应该可用于您的查询,别名是关系的名称(产品)。 (注意:要在 CActiveDataProvider 返回的模型上访问 SUM(product.local_rest) 的结果,您需要将 $product_local_rest
设置为 public 属性 class 个返回模型。)
或者,如果您更愿意编写原始代码 SQL,您可以使用 CDbCommand 生成结果数组,然后使用 CArrayDataProvider 而不是 CActiveDataProvider。 http://www.yiiframework.com/doc/api/1.1/CArrayDataProvider
我对 Yii 比较陌生。
我对原始 SQL 充满信心,但在谈到原始时仍然有点迷茫 ORM。所以这可能是一个虚拟问题。
我已经检索了构建 CDbCriteria
:
$criteria = new CDbCriteria(array(
'select' => 'sum(items) as items',
// 'condition' => 't.items > 0 and order.storage = "'Product::STORAGE_LOCAL . '"',
'condition' => 't.items > 0 and order.storage = "' . Product::STORAGE_LOCAL . '"',
'order' => 'sum(items) DESC',
'with' => array(
'product' => array(
'select' => 'code, title, producer, local_rest',
**// 'select' => 'code, title, producer, sum(local_rest) as local_rest',**
'group' => 'product.code',
)
),
'join' => 'inner join `order` `order` on `t`.`order_id` = `order`.`id`',
// 'group' => '`product`.`code`, `product`.`title`',
'group' => '`product`.`code`',
'together' => true
));
我尝试通过 group by
获取 local_rest
字段的总和。不幸的是,它没有 return 它应该是什么。
这就是我试图将其构建到 CDbCriteria
中的方式:
// 'select' => 'code, title, producer, sum(local_rest) as local_rest',
。
- 运气不好。
我可以使用单独的查询来获取它:
$sum_local_rest = Yii::app()->db->createCommand("
SELECT id,code, title, sum(local_rest) as sum_rest FROM product GROUP BY code
ORDER BY `sum_rest` DESC
");
请注意 - 产品 table 中存在重复记录。
IE。我们不止一次拥有相同的产品。但是如果我使用 GROUP BY
它有助于划定这个缺点。这是由于糟糕的数据库设计造成的,应该在将来修复。
问题是我需要以某种方式将它与 CDbCriteria
绑定,因为它被 CDataProvider
使用,而 CDataProvider
被 GridView
使用。
关于如何将这两个问题合而为一的任何提示CDbCriteria
?
提前致谢
编辑
看着目前的答案,我觉得我需要总结一下。主要问题是我需要告诉 CDbCriteria
检索记录(由 HAS_Many 连接绑定)并计算所有这些记录的 SUM 并让 CDbCriteria
对这些记录进行 GROUP BY .
没有其他办法了。我不能明确地做到这一点。因为我将 CDbCriteria
传递给 CDataProvider
并且它应该 运行 查询。这就是 Yii 中的工作方式(据我所知)。
//You can merge your criteria like here:
$criteria = new CDbCriteria(); //First criteria
$criteria_2 = new CDbCriteria(); //Second criteria
$criteria->mergeWith($criteria_2); //Merge criteria and criteria_2
SomeModel::model()->findAll($criteria); //Find by criteria
您也不必将所有元素都传递给标准。尝试将标准拆分为更多代码,如下所示:
$criteria = new CDbCriteria();
$criteria->select = 'sum(items) as items, ' . Product::STORAGE_LOCAL;
$criteria->condition = 't.items > 0 and order.storage = ' . Product::STORAGE_LOCAL;
//etc.
我不明白为什么这样的东西不起作用:
$criteria = new CDbCriteria;
$criteria->select = array(
"SUM(t.items) as items",
"SUM(product.local_rest) as product_local_rest"
);
$criteria->condition = "t.items > 0 and order.storage = "' . Product::STORAGE_LOCAL . '"';
$criteria->join = 'inner join `order` `order` on `t`.`order_id` = `order`.`id`';
$criteria->with = "product";
$criteria->group = "product.code";
$criteria->together = true;
由于您将 together
设置为 true,您的关系的列应该可用于您的查询,别名是关系的名称(产品)。 (注意:要在 CActiveDataProvider 返回的模型上访问 SUM(product.local_rest) 的结果,您需要将 $product_local_rest
设置为 public 属性 class 个返回模型。)
或者,如果您更愿意编写原始代码 SQL,您可以使用 CDbCommand 生成结果数组,然后使用 CArrayDataProvider 而不是 CActiveDataProvider。 http://www.yiiframework.com/doc/api/1.1/CArrayDataProvider