/** * save * * @param DataInterface|Entity $data * * @return DataInterface|Entity * * @throws \LogicException * @throws \UnexpectedValueException * @throws \Windwalker\Record\Exception\NoResultException * @throws \InvalidArgumentException * @throws \RuntimeException */ public function save(DataInterface $data) { // Prepare Record object, primary keys and dump input data $record = $this->getRecord(); $keys = array_filter((array) $this->getKeyName(true)); // Fix because Record return empty string $dumped = $data->dump(true); // Let's check if primary exists, do action for update. $conditions = array_intersect_key($dumped, array_flip($keys)); if (array_filter($conditions)) { try { $record->load($conditions); } catch (NoResultException $e) { throw new NoResultException('Try to update a non-exists record to database.', $e->getCode(), $e); } } $record->bind($dumped); $this->prepareRecord($record); $record->validate()->store($this->updateNulls); $this->postSaveHook($record); $data->bind($record->dump(true)); return $data; }
/** * filter * * @param DataInterface|Entity $data * * @return DataInterface|Entity */ protected function prepareStore(DataInterface $data) { $model = $this->getModel(); if ($model instanceof FormAwareRepositoryInterface) { $result = $model->prepareStore($data->dump(true)); return $data->bind($result, true); } return $data; }