doufang3001 2019-01-18 10:48
浏览 25

原始对象和克隆对象doctrine集合之间有什么区别?

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 ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度