/** * */ public function execute() { $data = $this->entity->tabelizeRecord($this->record); /** * We need to get entity table extensions. * Lets hardcode them for now. */ $extensions = ['i18n', 'p17n', '']; $table = $this->entity->getTable(); $primaryKeys = $this->entity->getRepository()->getCache()->getTablePrimaryKeys($table); if (!$primaryKeys) { throw new Exception('Will NOT delete from table without primary keys ...'); } foreach ($extensions as $ext) { if ($ext) { $ext = '_' . $ext; } if ($this->entity->getRepository()->getCache()->hasTable($table . $ext)) { /** * We will delete record from $table ... */ $query = (new Delete())->setTable($table . $ext); /** * ... add primary key condition ... */ foreach ($primaryKeys as $key) { $query->where($key, $data[$table][$key]); } /** * ... prepare query ... */ $prepare = $this->repository->prepareQuery($query); /** * ... and execute it. */ $this->repository->executePrepared($prepare); } } $this->record->setSaved(false); $this->record->setDeleted(true); return true; }
/** * @param $table * @param array $data * * @return mixed * @throws Exception */ public function insert($table, array $data = []) { /** * We will insert $data into $table ... */ $query = (new Insert())->table($table)->setInsert($data); /** * ... prepare query ... */ $prepare = $this->repository->prepareQuery($query); /** * ... execute it ... */ $this->repository->executePrepared($prepare); /** * ... and return inserted ID. */ return $this->repository->getConnection()->lastInsertId(); }
/** * @param $table * @param array $data * * @return bool * @throws Exception */ public function update($table, array $data = []) { /** * We will update record in $table with $data ... */ $query = (new Update())->setTable($table)->setSet($data); /** * ... add primary key condition ... */ $query->primaryWhere($this->entity, $data, $table); /** * ... prepare query ... */ $prepare = $this->repository->prepareQuery($query, null); /** * ... and execute it. */ $this->repository->executePrepared($prepare); /** * Return number of updated records. */ return $prepare->rowCount(); }