📄 abstract.php
字号:
/** * 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 (!isset($map[Zend_Db_Table_Abstract::REF_COLUMNS])) { $parentInfo = $parentTable->info(); $map[Zend_Db_Table_Abstract::REF_COLUMNS] = array_values((array) $parentInfo['primary']); } $map[Zend_Db_Table_Abstract::COLUMNS] = (array) $map[Zend_Db_Table_Abstract::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, Zend_Db_Table_Select $select = 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"); } if ($select === null) { $select = $dependentTable->select(); } else { $select->setTable($dependentTable); } $map = $this->_prepareReference($dependentTable, $this->_getTable(), $ruleKey); for ($i = 0; $i < count($map[Zend_Db_Table_Abstract::COLUMNS]); ++$i) { $parentColumnName = $db->foldCase($map[Zend_Db_Table_Abstract::REF_COLUMNS][$i]); $value = $this->_data[$parentColumnName]; // Use adapter from dependent table to ensure correct query construction $dependentDb = $dependentTable->getAdapter(); $dependentColumnName = $dependentDb->foldCase($map[Zend_Db_Table_Abstract::COLUMNS][$i]); $dependentColumn = $dependentDb->quoteIdentifier($dependentColumnName, true); $dependentInfo = $dependentTable->info(); $type = $dependentInfo[Zend_Db_Table_Abstract::METADATA][$dependentColumnName]['DATA_TYPE']; $select->where("$dependentColumn = ?", $value, $type); } return $dependentTable->fetchAll($select); } /** * 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, Zend_Db_Table_Select $select = 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"); } if ($select === null) { $select = $parentTable->select(); } else { $select->setTable($parentTable); } $map = $this->_prepareReference($this->_getTable(), $parentTable, $ruleKey); for ($i = 0; $i < count($map[Zend_Db_Table_Abstract::COLUMNS]); ++$i) { $dependentColumnName = $db->foldCase($map[Zend_Db_Table_Abstract::COLUMNS][$i]); $value = $this->_data[$dependentColumnName]; // Use adapter from parent table to ensure correct query construction $parentDb = $parentTable->getAdapter(); $parentColumnName = $parentDb->foldCase($map[Zend_Db_Table_Abstract::REF_COLUMNS][$i]); $parentColumn = $parentDb->quoteIdentifier($parentColumnName, true); $parentInfo = $parentTable->info(); $type = $parentInfo[Zend_Db_Table_Abstract::METADATA][$parentColumnName]['DATA_TYPE']; $select->where("$parentColumn = ?", $value, $type); } return $parentTable->fetchRow($select); } /** * @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, Zend_Db_Table_Select $select = 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"); } if ($select === null) { $select = $matchTable->select(); } else { $select->setTable($matchTable); } // Use adapter from intersection table to ensure correct query construction $interInfo = $intersectionTable->info(); $interDb = $intersectionTable->getAdapter(); $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 = $interDb->quoteIdentifier('i' . '.' . $matchMap[Zend_Db_Table_Abstract::COLUMNS][$i], true); $matchCol = $interDb->quoteIdentifier('m' . '.' . $matchMap[Zend_Db_Table_Abstract::REF_COLUMNS][$i], true); $joinCond[] = "$interCol = $matchCol"; } $joinCond = implode(' AND ', $joinCond); $select->from(array('i' => $interName)) ->joinInner(array('m' => $matchName), $joinCond) ->setIntegrityCheck(false); $callerMap = $this->_prepareReference($intersectionTable, $this->_getTable(), $callerRefRule); for ($i = 0; $i < count($callerMap[Zend_Db_Table_Abstract::COLUMNS]); ++$i) { $callerColumnName = $db->foldCase($callerMap[Zend_Db_Table_Abstract::REF_COLUMNS][$i]); $value = $this->_data[$callerColumnName]; $interColumnName = $interDb->foldCase($callerMap[Zend_Db_Table_Abstract::COLUMNS][$i]); $interCol = $interDb->quoteIdentifier("i.$interColumnName", true); $matchColumnName = $interDb->foldCase($matchMap[Zend_Db_Table_Abstract::REF_COLUMNS][$i]); $interInfo = $intersectionTable->info(); $type = $interInfo[Zend_Db_Table_Abstract::METADATA][$interColumnName]['DATA_TYPE']; $select->where($interDb->quoteInto("$interCol = ?", $value, $type)); } $stmt = $select->query(); $config = array( 'table' => $matchTable, 'data' => $stmt->fetchAll(Zend_Db::FETCH_ASSOC), 'rowClass' => $matchTable->getRowClass(), 'readOnly' => false, '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 OPTIONAL Zend_Db_Table_Select query modifier * @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) { $matches = array(); if (count($args) && $args[0] instanceof Zend_Db_Table_Select) { $select = $args[0]; } else { $select = null; } /** * 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, $select); } /** * 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, $select); } /** * 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, $select); } 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 + -