public function testMatchingUsesThePersisterOnlyOnce()
 {
     $foo = new stdClass();
     $bar = new stdClass();
     $baz = new stdClass();
     $foo->val = 'foo';
     $bar->val = 'bar';
     $baz->val = 'baz';
     $this->persister->expects($this->once())->method('loadCriteria')->with($this->criteria)->will($this->returnValue(array($foo, $bar, $baz)));
     $criteria = new Criteria();
     $criteria->andWhere($criteria->expr()->eq('val', 'foo'));
     $filtered = $this->lazyCriteriaCollection->matching($criteria);
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\Collection', $filtered);
     $this->assertEquals(array($foo), $filtered->toArray());
     $this->assertEquals(array($foo), $this->lazyCriteriaCollection->matching($criteria)->toArray());
 }
 public function testInvokeExists()
 {
     $entity = new Country("Foo");
     $persister = $this->createPersisterDefault();
     $this->entityPersister->expects($this->once())->method('exists')->with($this->equalTo($entity), $this->equalTo(null));
     $this->assertNull($persister->exists($entity));
 }
 /**
  * {@inheritdoc}
  */
 public function refresh(array $id, $entity, $lockMode = null)
 {
     $this->persister->refresh($id, $entity, $lockMode);
 }
Example #4
0
 /**
  * Creates a closure capable of finalizing state a cloned proxy
  *
  * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata
  * @param \Doctrine\ORM\Persisters\Entity\EntityPersister    $entityPersister
  *
  * @return \Closure
  *
  * @throws \Doctrine\ORM\EntityNotFoundException
  */
 private function createCloner(ClassMetadata $classMetadata, EntityPersister $entityPersister)
 {
     return function (BaseProxy $proxy) use($entityPersister, $classMetadata) {
         if ($proxy->__isInitialized()) {
             return;
         }
         $proxy->__setInitialized(true);
         $proxy->__setInitializer(null);
         $class = $entityPersister->getClassMetadata();
         $identifier = $classMetadata->getIdentifierValues($proxy);
         $original = $entityPersister->loadById($identifier);
         if (null === $original) {
             throw EntityNotFoundException::fromClassNameAndIdentifier($classMetadata->getName(), $this->identifierFlattener->flattenIdentifier($classMetadata, $identifier));
         }
         foreach ($class->getReflectionClass()->getProperties() as $property) {
             if (!$class->hasField($property->name) && !$class->hasAssociation($property->name)) {
                 continue;
             }
             $property->setAccessible(true);
             $property->setValue($proxy, $property->getValue($original));
         }
     };
 }
 /**
  * Modified version of BasicEntityPersister::prepareUpdateData()
  * git revision d9fc5388f1aa1751a0e148e76b4569bd207338e9 (v2.5.3)
  *
  * @license MIT
  *
  * @author  Roman Borschel <*****@*****.**>
  * @author  Giorgio Sironi <*****@*****.**>
  * @author  Benjamin Eberlei <*****@*****.**>
  * @author  Alexander <*****@*****.**>
  * @author  Fabio B. Silva <*****@*****.**>
  * @author  Rob Caiger <*****@*****.**>
  * @author  Simon Mönch <*****@*****.**>
  *
  * @param EntityPersister|BasicEntityPersister $persister
  * @param                 $entity
  *
  * @return array
  */
 private function prepareUpdateData($persister, $entity)
 {
     $uow = $this->em->getUnitOfWork();
     $classMetadata = $persister->getClassMetadata();
     $versionField = null;
     $result = array();
     if (($versioned = $classMetadata->isVersioned) != false) {
         $versionField = $classMetadata->versionField;
     }
     foreach ($uow->getEntityChangeSet($entity) as $field => $change) {
         if (isset($versionField) && $versionField == $field) {
             continue;
         }
         if (isset($classMetadata->embeddedClasses[$field])) {
             continue;
         }
         $newVal = $change[1];
         if (!isset($classMetadata->associationMappings[$field])) {
             $columnName = $classMetadata->columnNames[$field];
             $result[$persister->getOwningTable($field)][$columnName] = $newVal;
             continue;
         }
         $assoc = $classMetadata->associationMappings[$field];
         // Only owning side of x-1 associations can have a FK column.
         if (!$assoc['isOwningSide'] || !($assoc['type'] & ClassMetadata::TO_ONE)) {
             continue;
         }
         if ($newVal !== null) {
             if ($uow->isScheduledForInsert($newVal)) {
                 $newVal = null;
             }
         }
         $newValId = null;
         if ($newVal !== null) {
             if (!$uow->isInIdentityMap($newVal)) {
                 continue;
             }
             $newValId = $uow->getEntityIdentifier($newVal);
         }
         $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
         $owningTable = $persister->getOwningTable($field);
         foreach ($assoc['joinColumns'] as $joinColumn) {
             $sourceColumn = $joinColumn['name'];
             $targetColumn = $joinColumn['referencedColumnName'];
             $result[$owningTable][$sourceColumn] = $newValId ? $newValId[$targetClass->getFieldForColumn($targetColumn)] : null;
         }
     }
     return $result;
 }