Magento 筛选联接列
我正在管理中创建一个股票报告,到目前为止一切正常,除了我似乎无法过滤加入的列 我加入了股票信息,使用以下内容来获取我的收藏Magento 筛选联接列,magento,Magento,我正在管理中创建一个股票报告,到目前为止一切正常,除了我似乎无法过滤加入的列 我加入了股票信息,使用以下内容来获取我的收藏 $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('name') ->addAttributeToSelect('sku') ->
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('sku')
->addAttributeToSelect('price')
->setStoreId($storeId);
$collection->addFieldToFilter('type_id', 'simple');
// Add on the stock qty information
$collection->getSelect()->join( array('stock'=>'ccmg_cataloginventory_stock_item'), 'e.entity_id = stock.item_id', array('stock.qty'));
这会导致显示该列,但无法对该列进行筛选或排序。我假设是因为没有将选项传递回联接。但是,可以对其他列进行排序和筛选,并将匹配的数据拉回并显示
我一直在搜索,但大多数帖子都是从2008年开始在Magento论坛上发布的,我使用的是1.6!任何指针都会很棒 加入后,您需要将加入的字段添加到
Varien\u Data\u Collection\u Db
中声明的数组\u map
,例如:
$this->_map['fields']['stock_qty'] = 'stock.qty';
class Company_Mohe_Model_Resource_Im_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
protected function _construct()
{
$this->_init('mohe/im');
}
public function joinIhe()
{
$this->getSelect()->join(array('ihe' => $this->getTable('mohe/ihe')),
'main_table.mic_inst_id = ihe.im_id',
array('ihe_name'=>'name', 'ihe_ifms_id'=>'ifms_id'));
//$this->_map['fields'] = array('ihe_name'=>'ihe.name', 'ihe_ifms_id'=>'ihe.ifms_id'); //incorrect method
$this->addFilterToMap('ihe_name', 'ihe.name'); //correct method, see comment by @sh4dydud3_88
return $this;
}
}
[编辑]正如@sh4dydud3_88所指出的,您可以这样做:
$collection->addFilterToMap('stock_qty', 'stock.qty');
这将添加字段“库存数量”以进行过滤。然后,您可以使用
$collection->addFieldToFilter('stock_qty', array('gt', 10));
另一个例子:
$this->_map['fields']['stock_qty'] = 'stock.qty';
class Company_Mohe_Model_Resource_Im_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
protected function _construct()
{
$this->_init('mohe/im');
}
public function joinIhe()
{
$this->getSelect()->join(array('ihe' => $this->getTable('mohe/ihe')),
'main_table.mic_inst_id = ihe.im_id',
array('ihe_name'=>'name', 'ihe_ifms_id'=>'ifms_id'));
//$this->_map['fields'] = array('ihe_name'=>'ihe.name', 'ihe_ifms_id'=>'ihe.ifms_id'); //incorrect method
$this->addFilterToMap('ihe_name', 'ihe.name'); //correct method, see comment by @sh4dydud3_88
return $this;
}
}
加入后,只需使用顺序对结果进行排序
$collection->getSelect()
->join( array('stock'=>'ccmg_cataloginventory_stock_item'), 'e.entity_id = stock.item_id', array('stock.qty'))
->order('stock.qty ASC');
我无法验证这是否有效,因为我不再处理此项目。它有效,但因为我没有扩展Mage_Core_Model_Resource_Db_Collection_Abstract
\u map['fields']
是私有/受保护的。addFilterToMap成功了<代码>$collection->addFilterToMap('ihe_name','ihe.name')addFieldToMap
实际上是正确的方法$this->\u-map['fields']
实际上覆盖了现有的\u-map
数组,因此如果要映射多个列,则会变得很麻烦。这比尝试在自定义集合类中使用两个eav表的方法效果更好。