LeanMapper\Entity::__set PHP Method

__set() public method

public __set ( string $name, mixed $value )
$name string
$value mixed
    public function __set($name, $value)
    {
        $reflection = $this->getCurrentReflection();
        $nativeSetter = $reflection->getSetter('set' . ucfirst($name));
        if ($nativeSetter !== null) {
            try {
                $nativeSetter->invoke($this, $value);
                return;
            } catch (ReflectionException $e) {
                throw new MemberAccessException("Cannot invoke native setter of property '{$name}' in entity " . get_called_class() . '.');
            }
        }
        $property = $reflection->getEntityProperty($name);
        if ($property === null) {
            throw new MemberAccessException("Cannot access undefined property '{$name}' in entity " . get_called_class() . '.');
        }
        if (!$property->isWritable()) {
            throw new MemberAccessException("Cannot write to read-only property '{$name}' in entity " . get_called_class() . '.');
        }
        $customSetter = $property->getSetter();
        if ($customSetter !== null) {
            if (!method_exists($this, $customSetter)) {
                throw new InvalidMethodCallException("Missing setter method '{$customSetter}' in entity " . get_called_class() . '.');
            }
            $this->{$customSetter}($value);
            return;
        }
        $this->set($property, $value);
    }

Usage Example

Example #1
0
 /**
  * Umožňuje předat entitě místo navázaných entit jejich 'id'
  * @Author Shaman
  *
  * @param string $name
  * @param mixed $value
  * @throws \LeanMapper\Exception\InvalidMethodCallException
  * @throws \LeanMapper\Exception\MemberAccessException
  */
 public function __set($name, $value)
 {
     $property = $this->getCurrentReflection()->getEntityProperty($name);
     //dump($name);
     //dump($value);
     //dump($property);
     if ($property->hasRelationship() && !$value instanceof Entity) {
         $relationship = $property->getRelationship();
         $this->row->{$property->getColumn()} = $value;
         $this->row->cleanReferencedRowsCache($relationship->getTargetTable(), $relationship->getColumnReferencingTargetTable());
     } else {
         parent::__set($name, $value);
     }
 }