使用UNION - Magento的where子句无效

时间:2016-10-08 19:17:48

标签: php mysql magento

我正在尝试合并magento中的两个集合,但我收到此错误

Invalid use of where clause with UNION Zend_Db_Select->_where('`id` IN(0)', '', NULL, true)

另外,当我打印集合查询并且查询在mysql workbench中正常工作时,但是当我将集合传递给setCollection方法时,我收到此错误。

请找到以下代码并帮我解决此问题。

    $collection = Mage::getModel('googleman_fetchprod/list')->getCollection();
    $collection1 = Mage::getModel('googleman_fetchprod/list')->getCollection();

    $select1 = clone $collection;
    $select1->getSelect()->joinLeft(
        array('prav' => $collection->getTable('googleman_fetchprod/account')), 'prav.list_id=main_table.id', array('prav.account_id')
    );
    $select1->getSelect()->joinLeft(
        array('c' => $collection->getTable('googleman_fetchprod/customer')), 'prav.account_id= c.customer_id', array('customer_id'=>'c.customer_id')
    );        
    $select1->addFieldToFilter('main_table.link_type', array('in' =>array('simple','config')));
    $select1->addFieldToFilter('prav.account_id', array('null' =>true));
    $selectOneSql = $select1->getSelect()->__toString();

    $select2 = clone $collection1;
    $select2->getSelect()->setPart('where', null);
    $select2->getSelect()->join(
        array('prav' => $collection1->getTable('googleman_fetchprod/account')), 'prav.list_id=main_table.id', array('prav.account_id')
    );        
    $select2->getSelect()->join(
        array('c' => $collection1->getTable('googleman_fetchprod/customer')), 'prav.account_id= c.customer_id', array('customer_id'=>'c.customer_id')
    );        
    $select2->addFieldToFilter('main_table.link_type', array('in' =>array('simple','config')));
    $select2->addFieldToFilter('prav.account_id', array('eq' =>1));
    $select2->addFieldToFilter('c.customer_id', array('eq' =>1));
    $selectTwoSql = $select2->getSelect()->__toString();

    $collection->getSelect()->reset();
    $collection->getSelect()->union(array(new Zend_Db_Expr($selectOneSql),new Zend_Db_Expr($selectTwoSql)));
     //echo  $select1->getSelect();
     //echo  $select2->getSelect();
     //exit;
    $this->setCollection($collection);
    parent::_prepareCollection();
    return $this;

0 个答案:

没有答案