Esempio n. 1
0
 public function get(Accessor $accessor, $field)
 {
     if (!property_exists($this, $field)) {
         throw new InvalidArgumentException(sprintf("%s has no property %s", get_class($this), $field));
     }
     return $accessor->convert($this, $field, $this->{$field}, Accessor::GETTER);
 }
Esempio n. 2
0
 public function convertToSetterValue(Accessor $accessor, $object, $field, $value)
 {
     $manager = $accessor->getObjectManager();
     $objectMetadata = $manager->getClassMetadata(get_class($object));
     $mappedField = $objectMetadata->getAssociationMappedByTargetField($field);
     $mappedClass = $objectMetadata->getAssociationTargetClass($field);
     $relatedObject = $this->makeObject($accessor, $object, $field, $value);
     if ($relatedObject && $mappedField && ($mappedField != $field || $mappledClass != get_class($object))) {
         $accessor->set($relatedObject, $mappedField, $object);
     }
     return $relatedObject;
 }
Esempio n. 3
0
 public function convertToSetterValue(Accessor $accessor, $object, $field, $value)
 {
     if ($value instanceof SplFileInfo) {
         return $value;
     }
     if ($value === NULL) {
         $value = $accessor->get($object, $field);
     } elseif (is_string($value)) {
         if (!$value) {
             $value = NULL;
         } else {
             $value = new SplFileInfo($value);
         }
     } else {
         if (is_object($value) && !$value instanceof SplFileInfo) {
             $value = new SplFileInfo((string) $value);
         }
     }
     return $value;
 }
Esempio n. 4
0
 /**
  *
  */
 public function convertToSetterValue(Accessor $accessor, $object, $field, $value)
 {
     $manager = $accessor->getObjectManager();
     $objectMetadata = $manager->getClassMetadata(get_class($object));
     $collection = $accessor->get($object, $field);
     $incomingCollection = new ArrayCollection();
     // very helpful:
     // http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html
     $mapping = $objectMetadata->getAssociationMapping($field);
     $mappedField = $objectMetadata->getAssociationMappedByTargetField($field);
     $mappedClass = $objectMetadata->getAssociationTargetClass($field);
     if ($value) {
         $values = !is_array($value) ? array($value) : $value;
         foreach ($values as $key => $value) {
             $relatedObject = $this->makeObject($accessor, $object, $field, $value);
             if ($mappedField) {
                 $inverse = $accessor->get($relatedObject, $mappedField);
                 if ($inverse instanceof Collection) {
                     //
                     // Handle bi-directional
                     //
                     if (!$inverse->contains($object)) {
                         $inverse->add($object);
                     }
                     //
                     // Handle self referencing bi-directional
                     //
                     if ($mappedClass == get_class($object)) {
                         $peer = $accessor->get($object, $mappedField);
                         if (!$peer->contains($relatedObject)) {
                             $peer->add($relatedObject);
                         }
                     }
                 } else {
                     //
                     // If the inverse is not a collection, then we assume it's a one to many and we want to make
                     // sure that the related object's mapped field is set to this object.
                     //
                     $accessor->set($relatedObject, $mappedField, $object);
                 }
             }
             $incomingCollection->add($relatedObject);
         }
     }
     foreach ($collection as $i => $relatedObject) {
         if (!$incomingCollection->contains($relatedObject)) {
             if ($mappedField) {
                 $inverse = $accessor->get($relatedObject, $mappedField);
                 if ($inverse instanceof Collection) {
                     //
                     // Handle bi-directional
                     //
                     if ($inverse->contains($object)) {
                         $inverse->removeElement($object);
                     }
                     //
                     // Handle self referencing bi-directional
                     //
                     if ($mappedClass == get_class($object)) {
                         $peer = $accessor->get($object, $mappedField);
                         if ($peer->contains($relatedObject)) {
                             $peer->removeElement($relatedObject);
                         }
                     }
                 } else {
                     //
                     // If the inverse is not a collection, then we assume it's a one to many and we want to make
                     // sure that the related object's mapped field is set to null.
                     //
                     $accessor->set($relatedObject, $mappedField, NULL);
                 }
             }
             if ($mapping['orphanRemoval']) {
                 $manager->getUnitOfWork()->scheduleOrphanRemoval($relatedObject);
             }
         }
     }
     return $incomingCollection;
 }