LeanMapper\Entity::assignEntityToProperty PHP Method

assignEntityToProperty() protected method

protected assignEntityToProperty ( Entity $entity = null, Property | string $property )
$entity Entity
$property LeanMapper\Reflection\Property | string micro-optimalization
    protected function assignEntityToProperty(Entity $entity = null, $property)
    {
        if ($entity !== null) {
            $this->useMapper($entity->mapper);
            $this->setEntityFactory($entity->entityFactory);
        }
        if (is_string($property)) {
            $property = $this->getCurrentReflection()->getEntityProperty($property);
        }
        $relationship = $property->getRelationship();
        if (!$relationship instanceof Relationship\HasOne) {
            throw new InvalidMethodCallException("Cannot assign value to property '{$property->getName()}' in entity " . get_called_class() . '. Only properties with m:hasOne relationship can be set via magic __set.');
        }
        $column = $relationship->getColumnReferencingTargetTable();
        if ($entity !== null) {
            $row = $entity->row;
            $pkColumn = $this->mapper->getPrimaryKey($this->mapper->getTable(get_class($entity)));
            $this->row->{$column} = $row->{$pkColumn};
            $this->row->setReferencedRow($row, $column);
        } else {
            $this->row->{$column} = null;
            $this->row->setReferencedRow(null, $column);
        }
    }