0

As the title suggests, I'd like to call my collection, store it into different variables, then manipulate the variables but leaving the original collection intact. The code should be something like this:

$collection = $this->collection->create()->addFieldToFilter('attribute', 'value');

$originalCollection = $collection;

$columnValues = $collection->joinLeft($table, $on, $columns)->reset(\Zend_Db_Select::COLUMNS)->columns(['fieldname1']);

$firstItem = $originalCollection->getFirstItem()->getData();

$this->logger->log($columnValues[0]);
$this->logger->log($firstItem);

In this instance, I'd like the first log to print only the first "fieldname1" while the second log to print the whole first item of the original collection.

Instead, what I get is that $columnValues[0] and $firstItem are the same so $firstItem also prints only fieldname1.

Is there a way to copy the original collection in a variable to preserve it from the subsequent modifications?

1 Answer 1

0

You should use clone to make sure the change on the new variable does not reflect the original variable.
Your code should look like the following:

$collection = $this->collection->create()->addFieldToFilter('attribute', 'value');

$originalCollection = clone $collection;

$columnValues = $collection->joinLeft($table, $on, $columns)->reset(\Zend_Db_Select::COLUMNS)->columns(['fieldname1']);

$firstItem = $originalCollection->getFirstItem()->getData();

$this->logger->log($columnValues[0]);
$this->logger->log($firstItem);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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