toArray(); } if (!isset($config['name'])) { if (null === $this->_tableAlias) { throw new Zend_Db_Table_Exception('property _tableAlias is not defined'); } $config['name'] = Qs_Db::getTableName($this->_tableAlias, $this->getIsService()); } if (!isset($config['db'])) { $config['db'] = Qs_Db::getInstance(); } parent::__construct($config); } public function getIsService() { return $this->_isService; } public function setIsService($flag) { $this->_isService = (bool) $flag; return $this; } public function getPrimary() { if (null === $this->_primary) { $this->_setupPrimaryKey(); } return (array) $this->_primary; } public function getMetaData() { $this->_setupMetadata(); return (array) $this->_metadata; } public function insert(array $data) { $date = date('Y-m-d H:i:s'); if (!isset($data['added'])) { $data['added'] = $date; } if (!isset($data['changed'])) { $data['changed'] = $date; } $this->unsetUnknownFields($data); $metaData = $this->getMetaData(); foreach ($metaData as $field => $fieldMetaData) { if ($fieldMetaData['DATA_TYPE'] == 'decimal' && $data[$field] === '') { $data[$field] = '0'; } } return parent::insert($data); } public function update(array $data, $where) { if (!isset($data['changed'])) { $data['changed'] = date('Y-m-d H:i:s'); } $this->unsetUnknownFields($data); $metaData = $this->getMetaData(); foreach ($metaData as $field => $fieldMetaData) { if ($fieldMetaData['DATA_TYPE'] == 'decimal' && $data[$field] === '') { $data[$field] = '0'; } } return parent::update($data, $where); } public function updateByKey(array $data, $key) { if (!isset($data['changed'])) { $data['changed'] = date('Y-m-d H:i:s'); } $this->unsetUnknownFields($data); if (empty($data)) { return false; } $where = $this->prepareWhere($key); $metaData = $this->getMetaData(); foreach ($metaData as $field => $fieldMetaData) { if ($fieldMetaData['DATA_TYPE'] == 'decimal' && $data[$field] === '') { $data[$field] = '0'; } } return parent::update($data, $where); } public function save($data, $key) { if (($row = $this->findRow($key))) { if (array_key_exists('id', $data)) { unset($data['id']); } $row->setFromArray($data); return $row->save(); } else { $row = $this->createRow($data); return $row->save(); } } /** * Search row by primary key * @param $key * @param bool $field * @param null $default * @return null */ public function search($key, $field = false, $default = null) { if (false === ($row = $this->findRow($key))) { return $default; } return Qs_Array::get($row->toArray(), $field, $default); } /** * Search row using custom filter * @param array $filter * @param bool $field * @param null $default * @return array|null */ public function searchBy(array $filter, $field = false, $default = null) { $select = $this->select(Qs_Db_Table::SELECT_WITH_FROM_PART); Qs_Db::filter($select, $filter, $this->_name); $select->limit(1); $result = $this->_db->fetchRow($select); return false === $result ? $result : Qs_Array::get($result, $field, $default); } /** * Search row by primary key * @param $key * @return bool | Zend_Db_Table_Row */ public function findRow($key) { /** @var $rowset Zend_Db_Table_Rowset */ $rowset = call_user_func_array(array($this, 'find'), (array)$key); if (count($rowset)) { return $rowset->current(); } return false; } public function deleteByKey($key) { $where = $this->prepareWhere($key); return parent::delete($where); } public function deleteBy(array $filter) { $select = $this->select(Qs_Db_Table::SELECT_WITH_FROM_PART); Qs_Db::filter($select, $filter, $this->_name); return parent::delete($select->getPart(Zend_Db_Select::WHERE)); } public function unsetUnknownFields(&$data) { $metaData = $this->getMetaData(); $data = array_intersect_key($data, $metaData); } public function prepareWhere($args, $alias = null) { if (!is_array($args)) { $args = (array)$args; } $args = array_values($args); $this->_setupPrimaryKey(); $keyNames = array_values((array) $this->_primary); if (count($args) < count($keyNames)) { // require_once 'Zend/Db/Table/Exception.php'; throw new Zend_Db_Table_Exception("Too few columns for the primary key"); } if (count($args) > count($keyNames)) { // require_once 'Zend/Db/Table/Exception.php'; throw new Zend_Db_Table_Exception("Too many columns for the primary key"); } $whereList = array(); $numberTerms = 0; foreach ($args as $keyPosition => $keyValues) { // Coerce the values to an array. // Don't simply typecast to array, because the values // might be Zend_Db_Expr objects. if (!is_array($keyValues)) { $keyValues = array($keyValues); } if ($numberTerms == 0) { $numberTerms = count($keyValues); } else if (count($keyValues) != $numberTerms) { // require_once 'Zend/Db/Table/Exception.php'; throw new Zend_Db_Table_Exception("Missing value(s) for the primary key"); } for ($i = 0; $i < count($keyValues); ++$i) { if (!isset($whereList[$i])) { $whereList[$i] = array(); } $whereList[$i][$keyPosition] = $keyValues[$i]; } } $whereClause = null; if (count($whereList)) { $whereOrTerms = array(); foreach ($whereList as $keyValueSets) { $whereAndTerms = array(); foreach ($keyValueSets as $keyPosition => $keyValue) { $type = $this->_metadata[$keyNames[$keyPosition]]['DATA_TYPE']; $tableName = $this->_db->quoteTableAs((null === $alias) ? $this->_name : $alias, null, true); $columnName = $this->_db->quoteIdentifier($keyNames[$keyPosition], true); $whereAndTerms[] = $this->_db->quoteInto( $tableName . '.' . $columnName . ' = ?', $keyValue, $type); } $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')'; } $whereClause = '(' . implode(' OR ', $whereOrTerms) . ')'; } while (substr($whereClause, 0, 1) == '(' && substr($whereClause, -1) == ')') { $whereClause = substr($whereClause, 1, -1); } return $whereClause; } }