加入creditmemo_flat_table

时间:2013-11-19 09:20:24

标签: php mysql magento

加入creditmemo_flat_table

$collection = Mage::getResourceModel('sales/order_creditmemo_grid_collection');
$collection->getSelect()
            ->join(
                array('creditmemo_table' => 'sales_flat_creditmemo'),
                'main_table.entity_id = creditmemo_table.entity_id',
                array('creditmemo_table.gift_cards_amount')
            );

gift_cards_amount存在时我得到了正确的值,当它不存在时得到 null 。 但是对于网格我需要而不是 null 。 有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

SQL select语法提供了IFNULL方法,可以在这里直接使用。

这就是你应该如何修改你的选择陈述

$collection->getSelect()
        ->join(
            array('creditmemo_table' => 'sales_flat_creditmemo'),
            'main_table.entity_id = creditmemo_table.entity_id',
            array('IFNULL(creditmemo_table.gift_cards_amount,0) as gift_cards_amount')
        );

答案 1 :(得分:0)

你可以通过两种方式来实现mysql和magento方式的渲染器这样的mysql方式:

$collection->getSelect()
        ->join(
            array('creditmemo_table' => 'sales_flat_creditmemo'),
            'main_table.entity_id = creditmemo_table.entity_id',
            array('COALESCE(creditmemo_table.gift_cards_amount,0) as gift_cards_amount')
        );

    OR

Magento方式 您可以为此字段设置渲染器,如下所示:

$this->addColumn('title', array(
          'header'    => Mage::helper('module')->__('Title'),
          'width'     => '250px',
          'align'     =>'left',
          'index'     => 'title',
          'renderer' => new Namespace_Module_Block_Adminhtml_Renderer_CheckNull(),
      ));

在Block-> Adminhtml下创建名称为Renderer的文件夹和名为CheckNull.php的文件。    像Block->Adminhtml->Renderer->CheckNull.php    在checknull页面中写下这样的代码:

   <?php

class Namespace_Module_Block_Adminhtml_Renderer_CheckNull extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

    public function render(Varien_Object $row) {
        if($row->getData('title')==Null)
        {
            return '0';
        }else{
            return $row->getData('title');
        }
    }

}