过滤连接的列 [英] Filtering a joined column
问题描述
我正在管理员中创建一个股票报告,到目前为止一切正常,除了我似乎无法在连接的列上进行过滤.
I am creating a stock report in the admin and have everything working so far, except that I can't seem to be able to filter on the joined column.
我已经加入了股票信息,并使用以下信息来获取我的收藏集.
I have joined the stock information, using the following to grab my collection.
$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'));
这将导致它显示,但是您不能对列进行过滤或排序.我认为是因为选项没有被传递回联接中.但是,可以对其他列进行排序和过滤,并将匹配的数据拉回并显示.
This is causing it to display, but you can't filter or sort the column. I assume because the options aren't being passed back into the join. However, the other columns can be sorted and filtered and the matching data is pulled back and displayed.
我一直在搜索,但是大多数帖子都在2008年的Magento论坛上,并且我正在使用1.6!任何指针都很棒!
I've been searching but most posts are on the Magento forums from 2008, and I'm using 1.6! Any pointers would be great!
推荐答案
加入后,您需要将加入的字段添加到在Varien_Data_Collection_Db
中声明的数组_map
中,例如:
After the join, you need to add the joined field to the array _map
declared in Varien_Data_Collection_Db
, for example:
$this->_map['fields']['stock_qty'] = 'stock.qty';
正如@ sh4dydud3_88所指出的,您可以执行以下操作:
[edit] As pointed out by @sh4dydud3_88, you can do this:
$collection->addFilterToMap('stock_qty', 'stock.qty');
这将添加字段 stock_qty 进行过滤.然后您可以使用
which will add the field stock_qty for filtering. Then you can filter with
$collection->addFieldToFilter('stock_qty', array('gt', 10));
另一个例子:
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;
}
}
这篇关于过滤连接的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!