[ * 'relationKey' => ['productId' => 'id'], * 'fields' => ['image'], * ]] */ protected $fileFields = []; /** @var bool */ protected $transactionEnabled = true; protected function initData(array &$data) { return $this; } public function insert(array &$data) { $this->initData($data); if (!$this->isTransactionEnabled()) { return $this->_insert($data); } $this->db->beginTransaction(); try { $key = $this->_insert($data); $this->db->commit(); } catch (Zend_Db_Exception $e) { $this->db->rollBack(); throw $e; } return $key; } private function _insert(array &$data) { $key = $this->getTable()->insert($data); $this->injectPrimaryKey($data, $key); $this->saveDependencies($data); return $key; } private function _update(array &$data, $key) { $rowsUpdated = $this->getTable()->updateByKey($data, $key); $this->injectPrimaryKey($data, $key); $this->saveDependencies($data); return $rowsUpdated; } protected function injectPrimaryKey(array &$data, $key) { if (!is_array($key)) { $key = array_combine($this->getTable()->getPrimary(), (array) $key); } $data = array_merge($data, $key); return $this; } public function update(array $data, $key) { $this->initData($data); if (!$this->isTransactionEnabled()) { return $this->_update($data, $key); } $this->db->beginTransaction(); try { $rowsUpdated = $this->getTable()->updateByKey($data, $key); $this->injectPrimaryKey($data, $key); $this->saveDependencies($data); $this->db->commit(); } catch (Zend_Db_Exception $e) { $this->db->rollBack(); throw $e; } return $rowsUpdated; } protected function saveDependencies(array $data) { return $this; } public function getFileFields() { return $this->fileFields; } public function setFileFields($fileFields) { foreach ($fileFields as $tableAlias => $options) { if (is_string($options)) { continue; } if (is_array($options)) { if (empty($tableAlias) || !array_key_exists('fields', $options) || !array_key_exists('relationKey', $options) ) { throw new Exception('Invalid file fields format'); } } else { throw new Exception('Invalid file fields format'); } } $this->fileFields = $fileFields; return $this; } protected function deleteDependencies($key) { return $this; } protected function getRemovableFiles($key) { $files = []; if (!($fields = $this->getFileFields())) { return $files; } if (!($data = $this->getTable()->search($key))) { return $files; } foreach ($fields as $tableAlias => $options) { if (is_string($options)) { if (($file = $data[$options])) { $files[] = $file; } continue; } $relationKey = Qs_Array::map($data, $options['relationKey']); $list = $this->getTable($tableAlias)->getList($options['fields'], $relationKey); foreach ($list as $item) { foreach ($item as $file) { if (empty($file)) { continue; } $files[] = $file; } } } return $files; } public function delete($key) { $files = $this->getRemovableFiles($key); $this->getTable()->deleteByKey($key); $this->deleteDependencies($key); foreach ($files as $name) { $this->getFileAdapter()->deleteFromDefaultDestination($name); } return true; } protected function getFileAdapter() { static $adapter; if (null === $adapter) { $adapter = new Qs_File_Transfer_Adapter_Db(); } return $adapter; } public function isTransactionEnabled() { return $this->transactionEnabled; } public function setTransactionEnabled($transactionEnabled) { $this->transactionEnabled = (bool) $transactionEnabled; return $this; } }