Php Yii:状态与条件的关系
在我的Yii项目中,我有Php Yii:状态与条件的关系,php,mysql,yii,Php,Mysql,Yii,在我的Yii项目中,我有订单,产品,以及订单产品模型,它们相互关联。 为了统计某段时间内销售的产品,我在产品模型中添加了以下关系: 我与Yii项目有以下关系: ... 'orderProductsCount' => array(self::STAT, 'OrderProduct', 'product_id', 'select' => 'SUM(quantity)'), 'orderProductsAmount' => array(self::STAT, 'OrderProduc
订单
,产品
,以及订单产品
模型,它们相互关联。
为了统计某段时间内销售的产品,我在产品模型中添加了以下关系:
我与Yii项目有以下关系:
...
'orderProductsCount' => array(self::STAT, 'OrderProduct', 'product_id', 'select' => 'SUM(quantity)'),
'orderProductsAmount' => array(self::STAT, 'OrderProduct', 'product_id', 'select' => 'SUM(total)'),
...
整体数据返回正确。然而,当我试图获取特定时间段内的订单数据时,我得到的数字与总数相同(一直在销售)
我试着在纯MySQL中做同样的事情-这是描述我需求的查询:
SELECT product.id, `order`.added, COUNT( order_product.product_id ) , SUM( order_product.total )
FROM product
LEFT JOIN order_product ON order_product.product_id = product.id
LEFT JOIN `order` ON `order`.id = order_product.order_id
WHERE `order`.added > "2015-01-10"
GROUP BY product.id
但我不知道如何用Yii的方式来表达。。。请帮帮我
PS.尝试在关系中添加条件,如下所示:
'orderProductsAmount' => array(self::STAT, 'OrderProduct', 'product_id', 'select' => 'SUM(total)', 'condition' => 'orders.added < "2015-01-10"'),
'orderProductsAmount'=>数组(self::STAT,'OrderProduct','product_id','select'=>'SUM(total)','condition'=>'orders.added<“2015-01-10”),
接收错误,因为条件在相关表中,而不是
产品中,因为与的关系中未定义与的关系
您必须通过with语句提供条件
您在产品模型中的关系
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'order' => array(self::MANY_MANY, 'Order', OrderProduct::model()->tableName().'(product_id,order_id)'),
'amount'=>array(self::STAT, 'OrderProduct', 'product_id','select' => 'sum(total)'),
'total'=>array(self::STAT, 'OrderProduct', 'product_id','select' => 'sum(product_id)'),
);
}
还有你的情况findAll()
在OrderProduct
和Order
模型中写入了什么关系?
$model= Product::model()->with(array(
'order' => array(
'alias' => 'o',
'condition' => 'o.added > :date',
'params' => array(':date' => '2015-06-01')
),
'amount' => array(),
'total' => array()
))->findAll();
CVarDumper::dump($model,10,true);