There is a doctrine collection and I want to change some values on it. These are happening in another class and pass to dao level to save it to database. and there there is another line to fetch data from db. because of that line, all the changed objects are replaced.
$doctrineCollection1 = $empSalaryComponents->findBy('employee_number', $empNumber);
foreach ($empSalComCollection1 as $empSalComponent) {
$empSalComponent->setValue(9999999999999);
}
$doctrineCollection2 = $empSalaryComponents->findBy('employee_number', $empNumber);
Now if you printed this 2 collections, you will get same results.
To avoid this, use this approach,
$doctrineCollection1 = $empSalaryComponents->findBy('employee_number', $empNumber);
$employeeSalaryComponents = new Doctrine_Collection('EmployeeSalaryComponent');
foreach ($doctrineCollection1 as $object) {
$object = clone $object;
$object->setValue(9999999999999);
$employeeSalaryComponents->add($object);
}
$doctrineCollection2 = $empSalaryComponents->findBy('employee_number', $empNumber);
now $doctrineCollection1 and $employeeSalaryComponents are difference. Can any one explain the reason for this ?