Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Commit

Permalink
Clone a collection's select object when cloning the collection
Browse files Browse the repository at this point in the history
PHP object cloning is shallow. Therefore, any objects owned by the
cloned object are also owned by the original object, because it's all
passed around by reference. This means that if the select object is
modified after the clone, this will still affect the original
collection's select object. This creates problems when you have
collections that modify the select object in the _beforeLoad() method.

We encountered issues when exporting the Sales Transactions grid,
where it would complain that it had already added a column name
correlation whenever it reached the 2nd page when exporting a
collection. This was happening because it was using the same shared
select object from the original collection.

To get around this, we now clone the original select object, and
manually set it on the cloned collection on each iteration of the loop,
using reflection to do so. This appears to work fine for all grids, and
is a general improvement.

It is worth noting that this exact issue is probably why Magento never
made exporting possible on some grids, such as the Sales Transactions
grid.
  • Loading branch information
Matthew Gamble committed Aug 3, 2015
1 parent 8bc41ea commit 024e39c
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,18 @@ public function _exportIterateCollection($callback, array $args)
$break = false;
$first = false;
$count = null;
if ($originalCollection instanceof Varien_Data_Collection_Db) {
$selectProperty = new ReflectionProperty(get_class($originalCollection), "_select");
$selectProperty->setAccessible(true);
$originalSelect = $originalCollection->getSelect();
}
while ($break !== true) {
$collection = clone $originalCollection;
if ($originalCollection instanceof Varien_Data_Collection_Db) {
$select = clone $originalSelect;
$selectProperty->setValue($collection, $select);
}
$collection->setPageSize($pageSize);
$collection->setCurPage($page);
Expand Down

0 comments on commit 024e39c

Please sign in to comment.