2015-04-23 35 views
0

Magento 1.7.0.2 - 我已經通過擴展Catalog Product Collection創建了一個自定義集合。基本上這個系列是什麼:一系列獨特的產品組合,如果任何產品具有自定義選項,那麼每個產品ID /選項ID的唯一組合就是集合中的另一個項目。收集和計數工作得很好。Magento自定義集合阻止正確的批量操作複選框選擇

但是,對於質量作用複選框選擇「全選」鍵僅與一個產品ID而不是那些其中PRODUCT_ID與option_id級聯如下面的圖像中選擇行。

enter image description here

這是我的集合類。

<?php 

class Lightsnholsters_Inventory_Model_Resource_Catalog_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection 
{ 
    public function getSelectCountSql() 
    { 
     $this->_renderFilters(); 
     $countSelect = clone $this->getSelect(); 
     $countSelect->reset(Zend_Db_Select::ORDER); 
     $countSelect->reset(Zend_Db_Select::LIMIT_COUNT); 
     $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET); 
     $countSelect->reset(Zend_Db_Select::COLUMNS); 

     // Count doesn't work with group by columns keep the group by 
     if(count($this->getSelect()->getPart(Zend_Db_Select::GROUP)) > 0) { 
      $countSelect->reset(Zend_Db_Select::GROUP); 
      $countSelect->distinct(true); 
      $group = $this->getSelect()->getPart(Zend_Db_Select::GROUP); 
      $countSelect->columns("COUNT(DISTINCT ".implode(", ", $group).")"); 
     } else { 
      $countSelect->columns('COUNT(*)'); 
     } 
     return $countSelect; 
    } 

    protected function _initSelect() 
    { 
     parent::_initSelect(); 
     $this->joinAttribute('name','catalog_product/name','entity_id');     

     $this->getSelect()->reset(Zend_Db_Select::COLUMNS); 

     $this->getSelect()->joinLeft(
      array('opt' => $this->getTable('catalog/product_option')), 
      "e.entity_id = opt.product_id AND opt.type IN('radio','drop_down')", 
      array('')); 

     $this->getSelect()->joinLeft(
      array('opt_val' => $this->getTable('catalog/product_option_type_value')), 
      "opt.option_id = opt_val.option_id AND opt_val.sku != '' AND opt_val.sku IS NOT NULL", 
      array('entity_id' => new Zend_Db_Expr("CONCAT_WS('_',e.entity_id,opt_val.option_type_id)"), 
       'option_type_id' => new Zend_Db_Expr("IF(opt_val.option_type_id > 0, opt_val.option_type_id,0)"))); 

     $this->getSelect()->joinLeft(
      array('opt_prc' => $this->getTable('catalog/product_option_type_price')), 
      "opt_val.option_type_id = opt_prc.option_type_id AND opt_prc.store_id = {$this->getStoreId()}", 
      array('')); 

     $this->getSelect()->joinLeft(
      array('opt_ttl' => $this->getTable('catalog/product_option_type_title')), 
      "opt_val.option_type_id = opt_ttl.option_type_id AND opt_ttl.store_id = {$this->getStoreId()}", 
      array('name' => new Zend_Db_Expr("CONCAT_WS(' ',at_name.value,title)"))); 

     return $this; 
    } 
} 

這裏是massaction代碼,我在網格類

protected function _prepareMassaction() 
    { 
    $this->setMassactionIdField('entity_id'); 
    $this->setMassactionIdFilter('entity_id'); 
    $this->getMassactionBlock()->setFormFieldName('entity_id'); 

    $this->getMassactionBlock()->addItem('updateAttributes', array(
     'label' => $this->__('Update Attributes'), 
     'url'  => $this->getUrl('*/*/massUpdateAttributes'), 
    )); 

    $statuses = array(
     array('label' => '',     'value'=>''), 
     array('label' => $this->__('Disabled'), 'value'=>0), 
     array('label' => $this->__('Enabled') , 'value'=>1) 
    ); 
    $this->getMassactionBlock()->addItem('status', array(
     'label'=> $this->__('Change status'), 
     'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)), 
     'additional' => array(
      'visibility' => array(
      'name' => 'status', 
      'type' => 'select', 
      'class' => 'required-entry', 
      'label' => $this->__('Status'), 
      'values' => $statuses 
      ) 
     ) 
    )); 

    Mage::dispatchEvent('lightsnholsters_inventory_autolist_grid_massaction_prepare', array('block' => $this)); 
    return $this; 
    } 

使用它好像我把一切領域$this->setMassactionIdField('entity_id');被完全忽略。

我的目標是,當我點擊「Select All」選中所有複選框時,不僅僅是那些沒有option_id連接的複選框。

謝謝你的幫助。

回答

0

我已經重新討論了這個問題,並找出爲什麼我的集合中使用 - > joinLeft和動態創建的id列有目的地和動態地創建的行不允許批量操作塊「全選」在單擊時正確選擇複選框。

它,因爲當我伸出Mage_Catalog_Model_Resource_Collection作爲自己的自定義集合的基地來使用,這個類裏已經重寫getAllIds()的Varien_Data_Collection方法,其中massaction塊使用,以確定哪個ID,我們實際上要選擇我們的時候點擊「全選」。

在我模塊的集合類,我在那裏擴展Magento的產品集合,我根本就創建自己的getAllIds()方法,該方法將完全覆蓋的方法的父版本。

簡而言之,一切正常,請參閱下面的代碼。

<?php 

class Lightsnholsters_Inventory_Model_Resource_Catalog_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection 
{ 
    /* 
    * 
    * we had to rewrite this function because the grid had the wrong count, which is actually why we created this class to begin with 
    * 
    */ 
    public function getSelectCountSql() 
    { 
     $this->_renderFilters(); 
     $countSelect = clone $this->getSelect(); 
     $countSelect->reset(Zend_Db_Select::ORDER); 
     $countSelect->reset(Zend_Db_Select::LIMIT_COUNT); 
     $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET); 
     $countSelect->reset(Zend_Db_Select::COLUMNS); 

     // Count doesn't work with group by columns keep the group by 
     if(count($this->getSelect()->getPart(Zend_Db_Select::GROUP)) > 0) { 
      $countSelect->reset(Zend_Db_Select::GROUP); 
      $countSelect->distinct(true); 
      $group = $this->getSelect()->getPart(Zend_Db_Select::GROUP); 
      $countSelect->columns("COUNT(DISTINCT ".implode(", ", $group).")"); 
     } else { 
      $countSelect->columns('COUNT(*)'); 
     } 
     return $countSelect; 
    } 

    protected function _initSelect() 
    { 
     parent::_initSelect(); 
     $this->joinAttribute('name','catalog_product/name','entity_id');     

     $this->getSelect()->reset(Zend_Db_Select::COLUMNS); 

     $this->getSelect()->joinLeft(
      array('opt' => $this->getTable('catalog/product_option')), 
      "e.entity_id = opt.product_id AND opt.type IN('radio','drop_down')", 
      array('')); 

     $this->getSelect()->joinLeft(
      array('opt_val' => $this->getTable('catalog/product_option_type_value')), 
      "opt.option_id = opt_val.option_id AND opt_val.sku != '' AND opt_val.sku IS NOT NULL", 
      array('entity_id' => new Zend_Db_Expr("CONCAT_WS('_',e.entity_id,opt_val.option_type_id)"), 
       'option_type_id' => new Zend_Db_Expr("IF(opt_val.option_type_id > 0, opt_val.option_type_id,0)"))); 

     $this->getSelect()->joinLeft(
      array('opt_prc' => $this->getTable('catalog/product_option_type_price')), 
      "opt_val.option_type_id = opt_prc.option_type_id AND opt_prc.store_id = {$this->getStoreId()}", 
      array('')); 

     $this->getSelect()->joinLeft(
      array('opt_ttl' => $this->getTable('catalog/product_option_type_title')), 
      "opt_val.option_type_id = opt_ttl.option_type_id AND opt_ttl.store_id = {$this->getStoreId()}", 
      array('name' => new Zend_Db_Expr("CONCAT_WS(' ',at_name.value,title)"))); 

     return $this; 
    } 

    /* 
    * 
    * we override the parent method for all ids because it does not fetch the proper ids for the massaction checkboxes 
    * 
    */ 
    public function getAllIds($limit = null, $offset = null) 
    { 
    $this->_renderFilters(); 
    $connection = $this->getConnection(); 
    $idsSelect = clone $this->getSelect(); 
    $idsSelect->reset(Zend_Db_Select::LIMIT_COUNT); 
    $idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);  
    $statement = $connection->query($idsSelect); 
    $ids = $statement->fetchAll(PDO::FETCH_COLUMN); 
    return $ids; 
    } 
}