/** * @return null|ContainerInterface */ protected function container() { if (!empty($this->parent) && $this->parent instanceof Component) { return $this->parent->container(); } return parent::container(); }
/** * {@inheritdoc} * * Parent record MUST be saved in order to preserve parent association. */ public function associate(EntityInterface $related = null) { if ($related === null) { $this->deassociate(); return; } /** * @var RecordEntity $related */ if (!$related->isLoaded()) { throw new RelationException("Unable to set 'belongs to' parent, parent has be fetched from database."); } parent::associate($related); //Key in parent record $outerKey = $this->definition[RecordEntity::OUTER_KEY]; //Key in child record $innerKey = $this->definition[RecordEntity::INNER_KEY]; if ($this->parent->getField($innerKey, false) != $related->getField($outerKey, false)) { //We are going to set relation keys right on assertion $this->parent->setField($innerKey, $related->getField($outerKey, false)); } }
/** * {@inheritdoc} */ protected function mountRelation(EntityInterface $record) { //Key in child record $outerKey = $this->definition[RecordEntity::OUTER_KEY]; //Key in parent record $innerKey = $this->definition[RecordEntity::INNER_KEY]; if ($record->getField($outerKey, false) != $this->parent->getField($innerKey, false)) { $record->setField($outerKey, $this->parent->getField($innerKey, false)); } if (!isset($this->definition[RecordEntity::MORPH_KEY])) { //No morph key presented return $record; } $morphKey = $this->definition[RecordEntity::MORPH_KEY]; if ($record->getField($morphKey) != $this->parent->recordRole()) { $record->setField($morphKey, $this->parent->recordRole()); } return $record; }
/** * Save simple related entity. * * @param EntityInterface $entity * @param bool $validate * @return bool|void */ private function saveEntity(EntityInterface $entity, $validate) { if ($entity instanceof RecordInterface && $entity->isDeleted()) { return true; } if (!$entity instanceof ActiveEntityInterface) { throw new RelationException("Unable to save non active entity."); } $this->mountRelation($entity); if (!$entity->save($validate)) { return false; } if ($entity instanceof IdentifiedInterface) { $this->orm->cache()->remember($entity); } return true; }