📄 abstract.php
字号:
/** * Execute cascading deletes against dependent tables */ $depTables = $this->_getTable()->getDependentTables(); if (!empty($depTables)) { $db = $this->_getTable()->getAdapter(); $pk = $this->_getPrimaryKey(); $thisClass = get_class($this); foreach ($depTables as $tableClass) { try { Zend_Loader::loadClass($tableClass); } catch (Zend_Exception $e) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception($e->getMessage()); } $t = new $tableClass(array('db' => $db)); $t->_cascadeDelete($this->getTableClass(), $pk); } } /** * Execute the DELETE (this may throw an exception) */ $result = $this->_getTable()->delete($where); /** * Execute post-DELETE logic */ $this->_postDelete(); /** * Reset all fields to null to indicate that the row is not there */ $this->_data = array_combine( array_keys($this->_data), array_fill(0, count($this->_data), null) ); return $result; } /** * Returns the column/value data as an array. * * @return array */ public function toArray() { return $this->_data; } /** * Sets all data in the row from an array. * * @param array $data * @return Zend_Db_Table_Row_Abstract Provides a fluent interface */ public function setFromArray(array $data) { foreach ($data as $columnName => $value) { $this->$columnName = $value; } return $this; } /** * Retrieves an instance of the parent table. * * @return Zend_Db_Table_Abstract */ protected function _getTable() { if (!$this->_connected) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception('Cannot save a Row unless it is connected'); } return $this->_table; } /** * Retrieves an associative array of primary keys. * * @param bool $dirty * @return array */ protected function _getPrimaryKey($dirty = true) { $primary = array_flip($this->_primary); if ($dirty) { return array_intersect_key($this->_data, $primary); } else { return array_intersect_key($this->_cleanData, $primary); } } /** * Constructs where statement for retrieving row(s). * * @return array */ protected function _getWhereQuery($dirty = true) { $where = array(); $db = $this->_getTable()->getAdapter(); $primaryKey = $this->_getPrimaryKey($dirty); // retrieve recently updated row using primary keys foreach ($primaryKey as $columnName => $val) { $where[] = $db->quoteInto($db->quoteIdentifier($columnName, true) . ' = ?', $val); } return $where; } /** * Refreshes properties from the database. * * @return void */ protected function _refresh() { $where = $this->_getWhereQuery(); $row = $this->_getTable()->fetchRow($where); if (null === $row) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception('Cannot refresh row as parent is missing'); } $this->_data = $row->toArray(); $this->_cleanData = $this->_data; } /** * Allows pre-insert logic to be applied to row. * Subclasses may override this method. * * @return void */ protected function _insert() { } /** * Allows post-insert logic to be applied to row. * Subclasses may override this method. * * @return void */ protected function _postInsert() { } /** * Allows pre-update logic to be applied to row. * Subclasses may override this method. * * @return void */ protected function _update() { } /** * Allows post-update logic to be applied to row. * Subclasses may override this method. * * @return void */ protected function _postUpdate() { } /** * Allows pre-delete logic to be applied to row. * Subclasses may override this method. * * @return void */ protected function _delete() { } /** * Allows post-delete logic to be applied to row. * Subclasses may override this method. * * @return void */ protected function _postDelete() { } /** * Prepares a table reference for lookup. * * Ensures all reference keys are set and properly formatted. * * @param Zend_Db_Table_Abstract $dependentTable * @param Zend_Db_Table_Abstract $parentTable * @param string $ruleKey * @return array */ protected function _prepareReference(Zend_Db_Table_Abstract $dependentTable, Zend_Db_Table_Abstract $parentTable, $ruleKey) { $map = $dependentTable->getReference(get_class($parentTable), $ruleKey); if (!is_array($map[Zend_Db_Table_Abstract::COLUMNS])) { $map[Zend_Db_Table_Abstract::COLUMNS] = (array) $map[Zend_Db_Table_Abstract::COLUMNS]; } if (!isset($map[Zend_Db_Table_Abstract::REF_COLUMNS])) { $parentInfo = $parentTable->info(); $map[Zend_Db_Table_Abstract::REF_COLUMNS] = (array) $parentInfo['primary']; } if (!is_array($map[Zend_Db_Table_Abstract::REF_COLUMNS])) { $map[Zend_Db_Table_Abstract::REF_COLUMNS] = (array) $map[Zend_Db_Table_Abstract::REF_COLUMNS]; } return $map; } /** * Query a dependent table to retrieve rows matching the current row. * * @param string|Zend_Db_Table_Abstract $dependentTable * @param string OPTIONAL $ruleKey * @return Zend_Db_Table_Rowset_Abstract Query result from $dependentTable * @throws Zend_Db_Table_Row_Exception If $dependentTable is not a table or is not loadable. */ public function findDependentRowset($dependentTable, $ruleKey = null) { $db = $this->_getTable()->getAdapter(); if (is_string($dependentTable)) { try { Zend_Loader::loadClass($dependentTable); } catch (Zend_Exception $e) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception($e->getMessage()); } $dependentTable = new $dependentTable(array('db' => $db)); } if (! $dependentTable instanceof Zend_Db_Table_Abstract) { $type = gettype($dependentTable); if ($type == 'object') { $type = get_class($dependentTable); } require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception("Dependent table must be a Zend_Db_Table_Abstract, but it is $type"); } $map = $this->_prepareReference($dependentTable, $this->_getTable(), $ruleKey); for ($i = 0; $i < count($map[Zend_Db_Table_Abstract::COLUMNS]); ++$i) { $cond = $db->quoteIdentifier($map[Zend_Db_Table_Abstract::COLUMNS][$i], true) . ' = ?'; $where[$cond] = $this->_data[$db->foldCase($map[Zend_Db_Table_Abstract::REF_COLUMNS][$i])]; } return $dependentTable->fetchAll($where); } /** * Query a parent table to retrieve the single row matching the current row. * * @param string|Zend_Db_Table_Abstract $parentTable * @param string OPTIONAL $ruleKey * @return Zend_Db_Table_Row_Abstract Query result from $parentTable * @throws Zend_Db_Table_Row_Exception If $parentTable is not a table or is not loadable. */ public function findParentRow($parentTable, $ruleKey = null) { $db = $this->_getTable()->getAdapter(); if (is_string($parentTable)) { try { Zend_Loader::loadClass($parentTable); } catch (Zend_Exception $e) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception($e->getMessage()); } $parentTable = new $parentTable(array('db' => $db)); } if (! $parentTable instanceof Zend_Db_Table_Abstract) { $type = gettype($parentTable); if ($type == 'object') { $type = get_class($parentTable); } require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception("Parent table must be a Zend_Db_Table_Abstract, but it is $type"); } $map = $this->_prepareReference($this->_getTable(), $parentTable, $ruleKey); for ($i = 0; $i < count($map[Zend_Db_Table_Abstract::COLUMNS]); ++$i) { $cond = $db->quoteIdentifier($map[Zend_Db_Table_Abstract::REF_COLUMNS][$i], true) . ' = ?'; $where[$cond] = $this->_data[$db->foldCase($map[Zend_Db_Table_Abstract::COLUMNS][$i])]; } return $parentTable->fetchRow($where); } /** * @param string|Zend_Db_Table_Abstract $matchTable * @param string|Zend_Db_Table_Abstract $intersectionTable * @param string OPTIONAL $primaryRefRule * @param string OPTIONAL $matchRefRule * @return Zend_Db_Table_Rowset_Abstract Query result from $matchTable * @throws Zend_Db_Table_Row_Exception If $matchTable or $intersectionTable is not a table class or is not loadable. */ public function findManyToManyRowset($matchTable, $intersectionTable, $callerRefRule = null, $matchRefRule = null) { $db = $this->_getTable()->getAdapter(); if (is_string($intersectionTable)) { try { Zend_Loader::loadClass($intersectionTable); } catch (Zend_Exception $e) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception($e->getMessage()); } $intersectionTable = new $intersectionTable(array('db' => $db)); } if (! $intersectionTable instanceof Zend_Db_Table_Abstract) { $type = gettype($intersectionTable); if ($type == 'object') { $type = get_class($intersectionTable); } require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception("Intersection table must be a Zend_Db_Table_Abstract, but it is $type"); } if (is_string($matchTable)) { try { Zend_Loader::loadClass($matchTable); } catch (Zend_Exception $e) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception($e->getMessage()); } $matchTable = new $matchTable(array('db' => $db)); } if (! $matchTable instanceof Zend_Db_Table_Abstract) { $type = gettype($matchTable); if ($type == 'object') { $type = get_class($matchTable); } require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception("Match table must be a Zend_Db_Table_Abstract, but it is $type"); } $interInfo = $intersectionTable->info(); $interName = $interInfo['name']; $matchInfo = $matchTable->info(); $matchName = $matchInfo['name']; $matchMap = $this->_prepareReference($intersectionTable, $matchTable, $matchRefRule); for ($i = 0; $i < count($matchMap[Zend_Db_Table_Abstract::COLUMNS]); ++$i) { $interCol = $db->quoteIdentifier('i', true) . '.' . $db->quoteIdentifier($matchMap[Zend_Db_Table_Abstract::COLUMNS][$i], true); $matchCol = $db->quoteIdentifier('m', true) . '.' . $db->quoteIdentifier($matchMap[Zend_Db_Table_Abstract::REF_COLUMNS][$i], true); $joinCond[] = "$interCol = $matchCol"; } $joinCond = implode(' AND ', $joinCond); $select = $db->select() ->from(array('i' => $interName), array()) ->join(array('m' => $matchName), $joinCond, '*'); $callerMap = $this->_prepareReference($intersectionTable, $this->_getTable(), $callerRefRule); for ($i = 0; $i < count($callerMap[Zend_Db_Table_Abstract::COLUMNS]); ++$i) { $interCol = $db->quoteIdentifier('i', true) . '.' . $db->quoteIdentifier($callerMap[Zend_Db_Table_Abstract::COLUMNS][$i], true); $value = $this->_data[$db->foldCase($callerMap[Zend_Db_Table_Abstract::REF_COLUMNS][$i])]; $select->where("$interCol = ?", $value); } $stmt = $select->query(); $config = array( 'table' => $matchTable, 'data' => $stmt->fetchAll(Zend_Db::FETCH_ASSOC), 'rowClass' => $matchTable->getRowClass(), 'stored' => true ); $rowsetClass = $matchTable->getRowsetClass(); try { Zend_Loader::loadClass($rowsetClass); } catch (Zend_Exception $e) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception($e->getMessage()); } $rowset = new $rowsetClass($config); return $rowset; } /** * Turn magic function calls into non-magic function calls * to the above methods. * * @param string $method * @param array $args * @return Zend_Db_Table_Row_Abstract|Zend_Db_Table_Rowset_Abstract * @throws Zend_Db_Table_Row_Exception If an invalid method is called. */ protected function __call($method, array $args) { /** * Recognize methods for Has-Many cases: * findParent<Class>() * findParent<Class>By<Rule>() * Use the non-greedy pattern repeat modifier e.g. \w+? */ if (preg_match('/^findParent(\w+?)(?:By(\w+))?$/', $method, $matches)) { $class = $matches[1]; $ruleKey1 = isset($matches[2]) ? $matches[2] : null; return $this->findParentRow($class, $ruleKey1); } /** * Recognize methods for Many-to-Many cases: * find<Class1>Via<Class2>() * find<Class1>Via<Class2>By<Rule>() * find<Class1>Via<Class2>By<Rule1>And<Rule2>() * Use the non-greedy pattern repeat modifier e.g. \w+? */ if (preg_match('/^find(\w+?)Via(\w+?)(?:By(\w+?)(?:And(\w+))?)?$/', $method, $matches)) { $class = $matches[1]; $viaClass = $matches[2]; $ruleKey1 = isset($matches[3]) ? $matches[3] : null; $ruleKey2 = isset($matches[4]) ? $matches[4] : null; return $this->findManyToManyRowset($class, $viaClass, $ruleKey1, $ruleKey2); } /** * Recognize methods for Belongs-To cases: * find<Class>() * find<Class>By<Rule>() * Use the non-greedy pattern repeat modifier e.g. \w+? */ if (preg_match('/^find(\w+?)(?:By(\w+))?$/', $method, $matches)) { $class = $matches[1]; $ruleKey1 = isset($matches[2]) ? $matches[2] : null; return $this->findDependentRowset($class, $ruleKey1); } require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception("Unrecognized method '$method()'"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -