We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

copy model question

I have follow php code ("copy order"):

            $sourceOrdersList = ordersListUsers::findFirst(array(
                'ordersListId  = :ordersListId: AND userId = :userId:',
                'bind' => array (
                    'ordersListId' => $ordersListId,
                    'userId' => $user->id
                ),
            ))->getOrdersList();

            if ($sourceOrdersList) {
                $newOrdersList = $sourceOrdersList;
                $newOrdersList->id = null;
                $newOrdersList->save();

                var_dump($newOrdersList->toarray());
                var_dump($sourceOrdersList->toarray());
                exit;

var_dump output:

array(2) {
  ["id"]=>
  string(4) "5338"
  ["label"]=>
  string(1) "0"
}
array(2) {
  ["id"]=>
  string(4) "5338"
  ["label"]=>
  string(1) "0"
}

Why "id" the same in both output?

$sourceOrdersList also modifical?

Phalcon 3.0.4 config: https://yadi.sk/i/8XOVHEfP3JfPyo



14.3k

Update Phalcon to 3.1.2 Windows DLL PHP 5.5

Question remains open



14.3k
Accepted
answer
edited May '17

SOLVED:

$newOrdersList = clone $sourceOrdersList;



5.1k

Hi

You must use clone to copy object instance

$newOrdersList = $sourceOrdersList; is one instance of the object in two variables