public static function getTableFromString($tableName, \libDb\Table\AbstractTable $referenceTable = null) { if ($referenceTable instanceof \libDb\Table\AbstractTable) { $tableDefinition = $referenceTable->getDefinition(); if ($tableDefinition !== null && $tableDefinition->hasTableConfig($tableName)) { return new \libDb\Table($tableName, $tableDefinition); } } $options = array(); if ($referenceTable instanceof \libDb\Table\AbstractTable) { $options['db'] = $referenceTable->getAdapter(); } if (isset($tableDefinition) && $tableDefinition !== null) { $options[\libDb\Table\AbstractTable::DEFINITION] = $tableDefinition; } return new $tableName($options); }
/** * @param string|\libDb\Table\AbstractTable $matchTable * @param string|\libDb\Table\AbstractTable $intersectionTable * @param string OPTIONAL $callerRefRule * @param string OPTIONAL $matchRefRule * @param libDb\Table\Select OPTIONAL $select * @return libDb\Table\Rowset\RowsetAbstract Query result from $matchTable * @throws \libDb\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, libDb\Table\Select $select = null) { $db = $this->_getTable()->getAdapter(); if (is_string($intersectionTable)) { $intersectionTable = $this->_getTableFromString($intersectionTable); } if (!$intersectionTable instanceof \libDb\Table\AbstractTable) { $type = gettype($intersectionTable); if ($type == 'object') { $type = get_class($intersectionTable); } throw new \libDb\Table\Row\Exception("Intersection table must be a \\libDb\\Table\\AbstractTable, 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(\libDb\Table\AbstractTable::DEFINITION => $tableDefinition)); } if (is_string($matchTable)) { $matchTable = $this->_getTableFromString($matchTable); } if (!$matchTable instanceof \libDb\Table\AbstractTable) { $type = gettype($matchTable); if ($type == 'object') { $type = get_class($matchTable); } throw new \libDb\Table\Row\Exception("Match table must be a \\libDb\\Table\\AbstractTable, 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(\libDb\Table\AbstractTable::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[\libDb\Table\AbstractTable::COLUMNS]); ++$i) { $interCol = $interDb->quoteIdentifier('i' . '.' . $matchMap[\libDb\Table\AbstractTable::COLUMNS][$i], true); $matchCol = $interDb->quoteIdentifier('m' . '.' . $matchMap[\libDb\Table\AbstractTable::REF_COLUMNS][$i], true); $joinCond[] = "{$interCol} = {$matchCol}"; } $joinCond = implode(' AND ', $joinCond); $select->from(array('i' => $interName), array(), $interSchema)->joinInner(array('m' => $matchName), $joinCond, libDb\Select::SQL_WILDCARD, $matchSchema)->setIntegrityCheck(false); $callerMap = $this->_prepareReference($intersectionTable, $this->_getTable(), $callerRefRule); for ($i = 0; $i < count($callerMap[\libDb\Table\AbstractTable::COLUMNS]); ++$i) { $callerColumnName = $db->foldCase($callerMap[\libDb\Table\AbstractTable::REF_COLUMNS][$i]); $value = $this->_data[$callerColumnName]; $interColumnName = $interDb->foldCase($callerMap[\libDb\Table\AbstractTable::COLUMNS][$i]); $interCol = $interDb->quoteIdentifier("i.{$interColumnName}", true); $interInfo = $intersectionTable->info(); $type = $interInfo[\libDb\Table\AbstractTable::METADATA][$interColumnName]['DATA_TYPE']; $select->where($interDb->quoteInto("{$interCol} = ?", $value, $type)); } $stmt = $select->query(); $config = array('table' => $matchTable, 'data' => $stmt->fetchAll(Db::FETCH_ASSOC), 'rowClass' => $matchTable->getRowClass(), 'readOnly' => false, 'stored' => true); $rowsetClass = $matchTable->getRowsetClass(); $rowset = new $rowsetClass($config); return $rowset; }