产品名称在Magento中的过滤器搜索中未在自定义网格中返回

时间:2016-03-28 09:28:50

标签: php mysql magento zend-framework

我一直在努力在自定义模块的自定义网格上获取基于SKU的产品名称。 我成功地在网格上显示了产品名称。

以下是以下代码......

 <?php
    protected function _prepareCollection()
        {
            $collection = Mage::getModel('questionanswer/answer')->getCollection();
            // $collection_join = Mage::getModel('questionanswer/answer')->getCollection()
            //     ->join(
            //         'questionanswer/question',
            //         '`questionanswer/question`.question_id=`main_table`.question_id'
            //         ,array('question')
            //         )
            //          ->join(
            //         'catalog/product',
            //         '`catalog/product`.entity_id=`main_table`.product_id'
            //         ,array('sku')
            //         );

            $entityTypeId = Mage::getModel('eav/entity')
              ->setType('catalog_product')
              ->getTypeId();
            $prodNameAttrId = Mage::getModel('eav/entity_attribute')
              ->loadByCode($entityTypeId, 'name')
              ->getAttributeId();
            $collection->getSelect()
              ->joinLeft(
            array('prod' => 'catalog_product_entity'), 
            'prod.entity_id = main_table.product_id',
            array('sku')
            )
            ->joinLeft(
            array('cpev' => 'catalog_product_entity_varchar'), 
            'cpev.entity_id=prod.entity_id AND cpev.attribute_id='.$prodNameAttrId.'', 
            array('name' => 'value')
            )->joinLeft(
            array('que'=>'questionanswer_question'),
            'que.question_id = main_table.question_id',
            array('question')
            ); 

                $this->setCollection($collection);
            return parent::_prepareCollection();

        }
    ?>

enter image description here

但是,每当我尝试过滤搜索产品名称时,它会给我以下错误..

  a:5:{i:0;s:413:"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause', query was: SELECT COUNT(*) FROM `questionanswer_question` AS `main_table`
 LEFT JOIN `catalog_product_entity` AS `prod` ON prod.entity_id = main_table.product_id
 LEFT JOIN `catalog_product_entity_varchar` AS `cpev` ON cpev.entity_id=prod.entity_id AND cpev.attribute_id=71 WHERE (`name` LIKE '%hp%') AND (`status` LIKE '%pending%')";i:1;s:6069:"#0 /home/vhost/_default/magento1.9.2/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 /home/vhost/_default/magento1.9.2/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)

我知道它会搜索不存在的列名...但我想在哪里改变......

过滤搜索对所有其他列都非常好......

1 个答案:

答案 0 :(得分:0)

只需添加以下代码

即可
 $this->addColumn('names',array(
                          'header' => 'Product Name',
                          'index' => 'name',
                          'filter_index'=> 'value',
          ));