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



4.2k

Update Phalcon to 3.1.2 Windows DLL PHP 5.5

Question remains open



4.2k
Accepted
answer
edited May '17

SOLVED:

$newOrdersList = clone $sourceOrdersList;



2.9k

Hi

You must use clone to copy object instance

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