/**
* @param string $entityClass
* @param string $key
* @param null $column
* @return StoredEntity|null
* @throws NoReferenceException
*/
public function reference($entityClass, $key = null, $column = null)
{
if ($this->cachedReferences == null) {
$this->cachedReferences = new ArrayHash();
}
if ($this->isNewEntity()) {
return null;
}
if ($key == null) {
$columnToDiscoverReference = $entityClass;
$reflectProperty = $this->getPropertyByColumnName($columnToDiscoverReference);
if ($reflectProperty) {
$entityClass = $this->getReferenceClassForProperty($reflectProperty);
if (!$entityClass) {
throw new NoReferenceException($columnToDiscoverReference);
}
$column = $columnToDiscoverReference;
$key = $entityClass::getTableName($this->annotationReader);
}
}
self::mustBeChildOf($entityClass, StoredEntity::class);
if (!$this->cachedReferences->offsetExists($key)) {
$reference = $entityClass::create($this, $this->row->getTable()->getReferencedTable($this->row, $key, $column));
$this->cachedReferences->offsetSet($key, $reference);
}
return $this->cachedReferences->offsetGet($key);
}