setValue() public method

Sets property value.
public setValue ( string $name, mixed $value ) : self
$name string
$value mixed
return self
Ejemplo n.º 1
0
 public function setValues($values, $erase = FALSE) : self
 {
     $this->initEntityRelations();
     foreach ($values as $property => $value) {
         if ($this[$property] instanceof Nette\Forms\IControl) {
             $value = $value === '' ? NULL : $value;
             if ($this[$property] instanceof Nette\Forms\Controls\Checkbox && !$value && $this->metadata->getProperty($property)->isNullable) {
                 $value = NULL;
             }
             $this->entity->setValue($property, $value);
         }
     }
     return parent::setValues($values, $erase);
 }
Ejemplo n.º 2
0
 /**
  * @param  IEntity $entity
  * @param  PropertyMetadata[] $metadata
  * @parma  IModel $model
  * @param  array $pre
  */
 private static function setNulls($entity, array $metadata, IModel $model, array &$pre)
 {
     foreach ($metadata as $propertyMeta) {
         $type = $propertyMeta->relationship->type;
         $name = $propertyMeta->name;
         $reverseRepository = $model->getRepository($propertyMeta->relationship->repository);
         $reverseProperty = $propertyMeta->relationship->property ? $reverseRepository->getEntityMetadata()->getProperty($propertyMeta->relationship->property) : null;
         if ($type === Relationship::MANY_HAS_MANY) {
             /** @var ManyHasMany $property */
             $property = $entity->getProperty($name);
             $pre[] = $property;
             if ($reverseProperty !== null) {
                 foreach ($property as $reverseEntity) {
                     $pre[] = $reverseEntity->getProperty($reverseProperty->name);
                 }
             }
             $entity->setValue($name, []);
         } elseif ($type === Relationship::MANY_HAS_ONE || $type === Relationship::ONE_HAS_ONE && $propertyMeta->relationship->isMain) {
             /** @var ManyHasOne|OneHasOne $property */
             $property = $entity->getProperty($name);
             if ($reverseProperty !== null && $entity->hasValue($name)) {
                 $pre[] = $entity->getValue($name)->getProperty($reverseProperty->name);
             }
             $property->set(null, true);
         } else {
             // $type === Relationship::ONE_HAS_MANY or
             // $type === Relationship::ONE_HAS_ONE && !$isMain
             if (!$entity->hasValue($name) || $reverseProperty === null) {
                 continue;
             }
             if ($reverseProperty->isNullable) {
                 if ($type === Relationship::ONE_HAS_MANY) {
                     foreach ($entity->getValue($name) as $subValue) {
                         $pre[] = $subValue;
                     }
                     $entity->setValue($name, []);
                 } else {
                     $pre[] = $entity->getValue($name);
                     $entity->getProperty($name)->set(null, true);
                 }
             } else {
                 if ($type === Relationship::ONE_HAS_MANY && $entity->getValue($name)->count() === 0) {
                     continue;
                 }
                 $entityClass = get_class($entity);
                 $reverseEntityClass = $propertyMeta->relationship->entity;
                 $primaryValue = $entity->getValue('id');
                 $primaryValue = is_array($primaryValue) ? '[' . implode(', ', $primaryValue) . ']' : $primaryValue;
                 throw new InvalidStateException("Cannot remove {$entityClass}::\$id={$primaryValue} because {$reverseEntityClass}::\${$reverseProperty->name} cannot be a null.");
             }
         }
     }
 }
Ejemplo n.º 3
0
 protected function updateRelationshipRemove(IEntity $entity)
 {
     $this->updatingReverseRelationship = TRUE;
     $entity->setValue($this->metadata->relationshipProperty, NULL);
     $this->updatingReverseRelationship = FALSE;
 }
Ejemplo n.º 4
0
 /**
  * @param  IEntity $entity
  * @param  PropertyMetadata[] $metadata
  * @parma  IModel $model
  * @param  array $pre
  */
 private static function setNulls($entity, array $metadata, IModel $model, array &$pre)
 {
     foreach ($metadata as $propertyMeta) {
         $type = $propertyMeta->relationship->type;
         $name = $propertyMeta->name;
         if ($type === Relationship::MANY_HAS_MANY) {
             $entity->setValue($name, []);
         } elseif ($type === Relationship::MANY_HAS_ONE || $type === Relationship::ONE_HAS_ONE && $propertyMeta->relationship->isMain) {
             $entity->getProperty($name)->set(NULL, TRUE);
         } else {
             // $type === Relationship::ONE_HAS_MANY or
             // $type === Relationship::ONE_HAS_ONE && !$isPrimary
             $reverseRepository = $model->getRepository($propertyMeta->relationship->repository);
             $reverseProperty = $reverseRepository->getEntityMetadata()->getProperty($propertyMeta->relationship->property);
             if ($reverseProperty->isNullable) {
                 if ($entity->hasValue($name)) {
                     if ($type === Relationship::ONE_HAS_MANY) {
                         foreach ($entity->getValue($name) as $subValue) {
                             $pre[] = $subValue;
                         }
                         $entity->getValue($name)->set([]);
                     } else {
                         $pre[] = $entity->getValue($name);
                         $entity->setValue($name, NULL);
                     }
                 }
             } else {
                 $entityClass = get_class($entity);
                 $reverseEntityClass = $propertyMeta->relationship->entity;
                 $primaryValue = $entity->getValue('id');
                 $primaryValue = is_array($primaryValue) ? '[' . implode(', ', $primaryValue) . ']' : $primaryValue;
                 throw new InvalidStateException("Cannot remove {$entityClass}::\$id={$primaryValue} because {$reverseEntityClass}::\${$reverseProperty->name} cannot be a null.");
             }
         }
     }
 }