$test have other $type and price for it different. In cart can be $test with same id, but other $type and that’s a different product (object).
Example:
foreach ($cart as $order) {
$test = $em->getRepository('OrfosCoreBundle:Test')->find($order['test_id']);
$test->setType($order['test_type']);
$tests[] = $test;
$test = null;
}
and in $tests array doctrine return the same object if $order['test_id'] == previous $order['test_id']
array
0 =>
object(Orfos\CoreBundle\Entity\Test)[105]
1 =>
object(Orfos\CoreBundle\Entity\Test)[105]
How I can get new object?
Entities are uniquely identified by an identifier (primary key, in other terms).
Two entities with same identifier are the same object when you’re thinking in ORMs.
So what you will have there is just an array full of references to the same object (
Doctrine\ORM\EntityRepository#findwill get also the same instance if one is registered!)What you should probably do there is creating new instances of your Test entity, assign all fields values to them by calling all the different setters you have defined, persist them via
Doctrine\ORM\EntityManager#persist()and populate the array. Flushing the EntityManager viaDoctrine\ORM\EntityManager#flush()will save all elements in the array into db on separate records.If you want to clone the object instead, then you’ll have to work with
Doctrine\ORM\EntityManager#detach(), which I wouldn’t suggest you if you are just starting with the ORM. In this case, consider reading my solution posted on the Doctrine mailing list about Cloning Persisted Entities