We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.2k

Update Phalcon to 3.1.2 Windows DLL PHP 5.5

Question remains open



14.2k
Accepted
answer
edited May '17

SOLVED:

$newOrdersList = clone $sourceOrdersList;



5.0k

Hi

You must use clone to copy object instance

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