Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function save(ClassMetadata $meta, Component $component, $entity)
 {
     if (!$component instanceof BaseControl) {
         return FALSE;
     }
     if ($meta->hasField($name = $component->getOption(self::FIELD_NAME, $component->getName()))) {
         if (!$component->getOption(self::FIELD_NOT_SAVE, false)) {
             $this->accessor->setValue($entity, $name, $component->getValue());
         }
         return TRUE;
     }
     if (!$meta->hasAssociation($name)) {
         return FALSE;
     }
     if (!($identifier = $component->getValue())) {
         return FALSE;
     }
     $repository = $this->em->getRepository($this->relatedMetadata($entity, $name)->getName());
     if ($relation = $repository->find($identifier)) {
         $meta->setFieldValue($entity, $name, $relation);
     }
     return TRUE;
 }
Ejemplo n.º 2
0
 /**
  * @param \Doctrine\ORM\Mapping\ClassMetadata $meta
  * @param \Nette\ComponentModel\Component $component
  * @param mixed $entity
  * @return boolean
  */
 public function save(ClassMetadata $meta, Component $component, $entity)
 {
     if (!$component instanceof BaseControl) {
         return false;
     }
     $name = $component->getOption(self::FIELD_NAME, $component->getName());
     $value = $component->getValue();
     if ($this->accessor->isWritable($entity, $name) && !$meta->hasAssociation($name)) {
         try {
             $this->accessor->setValue($entity, $name, $value);
             return true;
         } catch (\Kdyby\Doctrine\MemberAccessException $e) {
         }
     }
     if (!$meta->hasAssociation($name)) {
         return false;
     }
     $value = $component->getValue();
     $entityClass = $this->relatedMetadata($entity, $name)->getName();
     $repository = $this->entityManager->getRepository($entityClass);
     if ($meta->isCollectionValuedAssociation($name)) {
         $property = \Doctrine\Common\Util\Inflector::singularize($name);
         foreach ($repository->findAll() as $associatedEntity) {
             if (in_array($associatedEntity->id, $value)) {
                 $hasMethod = 'has' . ucfirst($property);
                 if (!$entity->{$hasMethod}($associatedEntity)) {
                     $addMethod = 'add' . ucfirst($property);
                     $entity->{$addMethod}($associatedEntity);
                 }
             } else {
                 $removeMethod = 'remove' . ucfirst($property);
                 $entity->{$removeMethod}($associatedEntity);
             }
         }
     } elseif ($value === null || ($value = $repository->find($value))) {
         if ($this->accessor->isWritable($entity, $name)) {
             try {
                 $this->accessor->setValue($entity, $name, $value);
             } catch (\Kdyby\Doctrine\MemberAccessException $e) {
                 return false;
             }
         }
     }
     return true;
 }