题
我有以下集合,有助于实施自定义报告。
<?php
class VMR_Customreports_Model_Resource_Productsordered_Collection
extends Mage_Reports_Model_Resource_Order_Collection
{
public function __construct()
{
parent::__construct();
$this->_init('sales/order_item');
}
public function setDateRange($from, $to)
{
$this->_reset();
$this->getSelect()
->joinInner(
array('i' => $this->getTable('sales/order_item')),
'i.`order_id` = main_table.`entity_id`'
)
->where("main_table.created_at BETWEEN '" . $from . "' AND '" . $to . "'")
->where('main_table.status != \'canceled\'')
->where('main_table.customer_email NOT LIKE \'%@v2cigs.%\'')
->where('i.parent_item_id IS NULL')
->columns(array(
'myname' => 'i.name',
'ordered_qty' => 'sum(i.qty_ordered)',
'ordered_money' => 'sum(i.`price`*i.qty_ordered)'))
->group('i.name');
return $this;
}
public function setStoreIds($storeIds)
{
if ($storeIds)
$this->addAttributeToFilter('i.store_id', array('in' => (array)$storeIds));
return $this;
}
}
当我作为完整管理登录时,它会选择正确的数据,但是当我以更有限的用户登录时,我会遇到有关“违规诚信约束:1052列'store_id'的错误,其中的子句是模棱两可的”。
我可以理解,在order_item表和订单表中都在谈论store_id都存在,但是我在使用时指定表格 store_id
.
此外,错误发生在联接处。如果我评论store_id代码,我仍然会收到错误。为什么?
解决方案
如果您正在使用企业版,则 AdminGws
模块正在应用商店示波器过滤器,因此管理员只能访问被授权查看的数据。
另外,您可能想删除您的 __construct()
方法。有关其他信息,请参见下文。
背景:
您正在从 Mage_Reports_Model_Resource_Order_Collection
。该课程使用 sales/order
表(也就是说 sales_flat_order
作为主表。
$this->getSelect()->from(array('main_table' => $this->getMainTable()));
实际表实际上是从关联的资源模型中获取的:
public function getMainTable()
{
if ($this->_mainTable === null) {
$this->setMainTable($this->getResource()->getMainTable());
}
return $this->_mainTable;
}
因此,资源模型负责提供主表名称。
Mage::getResourceModel($this->getResourceModelName())
用于收集资源模型。
那么做什么 getResourceModelName()
返回?
它返回由 _init()
称呼。
protected function _init($model, $resourceModel = null)
{
$this->setModel($model);
if (is_null($resourceModel)) {
$resourceModel = $model;
}
$this->setResourceModel($resourceModel);
return $this;
}
在您的班上,您正在覆盖 _init()
父母班级的呼叫:
$this->_init('sales/order_item');
实际上,您指定您的集合应使用订单项目资源模型,该模型将返回表 sales/order_item
作为主表。
因为你 _init()
打电话给你加入 sales_flat_order_item
与自己的桌子。
通过删除 _init()
致电,或者实际上是整体 __construct()
覆盖可能会为您解决这个问题。
其他提示
如果我这样做:
public function addStoreAttributeToFilter($collection)
{
if($collection->getModelName() == 'sales/order' || $collection->getModelName() == 'sales/order_item')
{
$collection->addFieldToFilter('main_table.store_id', array('in' => $this->_role->getStoreIds()));
}
else
{
$collection->addAttributeToFilter('store_id', array('in' => $this->_role->getStoreIds()));
}
}
在Andingws/model/collections.php中
在我的集合中,创建了一个构建器,该构造函数可以使用$ this-> _ INIT('sales/order_item')。
构造函数部分对我没有意义,但我会分别询问这一点。
今天,我面临类似的问题。打开 $_logAllQueries
正如@vinai建议的那样表明 Enterprise_AdminGws
正在过滤我的收藏 store_id
. 。我通过添加:
$collection->addFilterToMap('store_id', 'main_table.store_id');
就在 $this->setCollection($collection);