/** * {@inheritdoc} * * Relation will automatically create related record if relation is not nullable. Usually * applied for has one relations ($user->profile). */ public function getRelated() { if (!empty($this->instance)) { if ($this->instance instanceof RecordInterface && !empty($this->data)) { //We have to keep record relation context (pivot data and pre-loaded relations) $this->instance->setContext($this->data); } //RecordIterator will update context automatically return $this->instance; } if (!$this->isLoaded()) { //Loading data if not already loaded $this->loadData(); } if (empty($this->data)) { if (array_key_exists(RecordEntity::NULLABLE, $this->definition) && !$this->definition[RecordEntity::NULLABLE] && !static::MULTIPLE) { //Not nullable relations must always return requested instance return $this->instance = $this->emptyRecord(); } //Can not be loaded, let's use empty iterator return static::MULTIPLE ? $this->createIterator() : null; } return $this->instance = static::MULTIPLE ? $this->createIterator() : $this->createRecord(); }