/** * _getTableFromString * * @param string $tableName * @return Zend_Db_Table_Abstract */ protected function _getTableFromString($tableName) { if ($this->_table instanceof Zend_Db_Table_Abstract) { $tableDefinition = $this->_table->getDefinition(); if ($tableDefinition !== null && $tableDefinition->hasTableConfig($tableName)) { return new Zend_Db_Table($tableName, $tableDefinition); } } // assume the tableName is the class name if (!class_exists($tableName)) { try { require_once 'Zend/Loader.php'; Zend_Loader::loadClass($tableName); } catch (Zend_Exception $e) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception($e->getMessage(), $e->getCode(), $e); } } $options = array(); if ($table = $this->_getTable()) { $options['db'] = $table->getAdapter(); } if (isset($tableDefinition) && $tableDefinition !== null) { $options[Zend_Db_Table_Abstract::DEFINITION] = $tableDefinition; } return new $tableName($options); }
/** * @param string|Zend_Db_Table_Abstract $matchTable * @param string|Zend_Db_Table_Abstract $intersectionTable * @param string OPTIONAL $callerRefRule * @param string OPTIONAL $matchRefRule * @param Zend_Db_Table_Select OPTIONAL $select * @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)) { $intersectionTable = $this->_getTableFromString($intersectionTable); } 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}"); } // even if we are interacting between a table defined in a class and a // table via extension, ensure to persist the definition if (($tableDefinition = $this->_table->getDefinition()) !== null && $intersectionTable->getDefinition() == null) { $intersectionTable->setOptions(array(Zend_Db_Table_Abstract::DEFINITION => $tableDefinition)); } if (is_string($matchTable)) { $matchTable = $this->_getTableFromString($matchTable); } 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}"); } // even if we are interacting between a table defined in a class and a // table via extension, ensure to persist the definition if (($tableDefinition = $this->_table->getDefinition()) !== null && $matchTable->getDefinition() == null) { $matchTable->setOptions(array(Zend_Db_Table_Abstract::DEFINITION => $tableDefinition)); } 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']; $interSchema = isset($interInfo['schema']) ? $interInfo['schema'] : null; $matchInfo = $matchTable->info(); $matchName = $matchInfo['name']; $matchSchema = isset($matchInfo['schema']) ? $matchInfo['schema'] : null; $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), array(), $interSchema)->joinInner(array('m' => $matchName), $joinCond, Zend_Db_Select::SQL_WILDCARD, $matchSchema)->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); $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(); if (!class_exists($rowsetClass)) { try { // require_once 'Zend/Loader.php'; 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(), $e->getCode(), $e); } } $rowset = new $rowsetClass($config); return $rowset; }
/** * Get table gateway object from string * * @param string $tableName * @param Zend_Db_Table_Abstract $referenceTable * @throws Zend_Db_Table_Row_Exception * @return Zend_Db_Table_Abstract */ public static function getTableFromString($tableName, Zend_Db_Table_Abstract $referenceTable = null) { if ($referenceTable instanceof Zend_Db_Table_Abstract) { $tableDefinition = $referenceTable->getDefinition(); if ($tableDefinition !== null && $tableDefinition->hasTableConfig($tableName)) { return new Zend_Db_Table($tableName, $tableDefinition); } } // assume the tableName is the class name if (!class_exists($tableName)) { try { Zend_Loader::loadClass($tableName); } catch (Zend_Exception $e) { throw new Zend_Db_Table_Row_Exception($e->getMessage(), $e->getCode(), $e); } } $options = array(); if ($referenceTable instanceof Zend_Db_Table_Abstract) { $options['db'] = $referenceTable->getAdapter(); } if (isset($tableDefinition) && $tableDefinition !== null) { $options[Zend_Db_Table_Abstract::DEFINITION] = $tableDefinition; } return new $tableName($options); }