/**
* @param Entity|null $entity
* @param Property|string $property micro-optimalization
* @throws InvalidMethodCallException
*/
protected function assignEntityToProperty(Entity $entity = null, $property)
{
if ($entity !== null) {
$this->useMapper($entity->mapper);
$this->setEntityFactory($entity->entityFactory);
$table = $this->mapper->getTable(get_class($entity));
$idProperty = $this->mapper->getEntityField($table, $this->mapper->getPrimaryKey($table));
}
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) {
$this->row->setReferencedRow($entity->row, $column);
$this->row->{$column} = $entity->{$idProperty};
} else {
$this->row->{$column} = null;
}
}