0

I want to get all IDs from collection using Mage_Core_Model_Resource_Db_Collection_Abstract::getAllIds() method.

My collection is joined with orders table:

$collection
     ->getSelect()
     ->joinLeft(
         ['o' => $this->getTable('sales/order')], // Table and alias
         'main_table.order_id = o.increment_id', // Join condition
         [ // Select fields
             'store_id' => 'store_id',
             'order_created_at' => 'created_at'
         ]);

But when I want to get all ID's from that collection with a having condition:

$collection->getSelect()->having('order_created_at >= ?', '20-02-2016 0:00:00')
$ids = $collection->getAllIds();

It gives me an error: Unknown column 'order_created_at' in 'having clause', because getAllIds() method resets collections' select columns and there is no way to filter by joined columns:

public function getAllIds()
{
    $idsSelect = clone $this->getSelect();
    $idsSelect->reset(Zend_Db_Select::ORDER);
    $idsSelect->reset(Zend_Db_Select::LIMIT_COUNT);
    $idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
    $idsSelect->reset(Zend_Db_Select::COLUMNS); // Reset columns here

    $idsSelect->columns($this->getResource()->getIdFieldName(), 'main_table');
    return $this->getConnection()->fetchCol($idsSelect);
}

So how to retreive joined collection ID's?

1

Use where instead of having.

$collection->getSelect()->where('order_created_at >= ?', '20-02-2016 0:00:00')

Or add order_created_at field to your select:

$collection
  ->getSelect()
  ->from($this->getTable('sales/order'), '*')
  ...

having requires fields to be in your results. It filters after your select instead of filtering the select itself as where does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.