protected function addAutoJoin(Zend_Db_Table_Select $select) { $tables = array(); $columns = array(); $references = $this->getDefaultAdapter()->getReferences(null, $this->_name); if ($references) { foreach ($references as $key => $reference) { $tableName = $reference['table']; $table = new La_Db_Table($tableName); $columnName = $table->getNameForOptionField(); $comments = $this->getComments(); $column = $reference['columns']; $columnAlias = $comments[$column]; $tableAlias = $tableName . $key; $columns[$columnAlias] = $tableAlias . '.' . $columnName; $tables[$tableName] = $tableName; $joinTable = array($tableAlias => $tableName); $condition = sprintf('`%s`.`id` = `%s`.`%s`', $tableAlias, $this->_name, $column); $select->joinLeft($joinTable, $condition, array()); } $select->setIntegrityCheck(false)->columns($columns); } return $select; }
/** * Add some data from other table, tests the joinTables * property. If not empty add tables and join clauses. * * @param Zend_Db_Table_Select $select * @param array $params * * @return Zend_Db_Table_Select */ private function _addJoinQuery($select, array $params = array()) { if (isset($params['joinTables']) && count($params['joinTables'])) { $this->_joinTables = $params['joinTables']; } /* If needs to add some data from other table, tests the joinTables * property. If not empty add tables and join clauses. */ if (count($this->_joinTables) > 0) { // Get the constraint attribute = foreign key to link tables. $constraint = $params['constraint']; // Loop on tables list(given by object class) to build the query foreach ($this->_joinTables as $key => $object) { //Create an object and fetch data from object. $tmpObject = new $object(); $tmpDataTable = $tmpObject->getDataTableName(); $tmpIndexTable = $tmpObject->getIndexTableName(); $tmpColumnData = $tmpObject->getDataColumns(); $tmpColumnIndex = $tmpObject->getIndexColumns(); //Add data to tables list $tables[$tmpDataTable] = $tmpColumnData; $tables[$tmpIndexTable] = $tmpColumnIndex; //Get the primary key of the first data object to join table $tmpDataId = $tmpObject->getDataId(); // If it's the first loop, join first table to the current table if ($key == 0) { $select->joinLeft($tmpDataTable, $tmpDataId . ' = ' . $constraint); //If there's an index table then it too and filter according language if (!empty($tmpIndexTable)) { $tmpIndexId = $tmpObject->getIndexId(); $select->joinLeft($tmpIndexTable, $tmpDataId . ' = ' . $tmpIndexId); $select->where($tmpObject->getIndexLanguageId() . ' = ?', $this->_defaultEditLanguage); } /* If there's more than one table to link, store the current * table name for the next loop */ if (count($this->_joinTables) > 1) { $prevConstraint = $tmpObject->getConstraint(); } } elseif ($key > 0) { // We have an other table to join to previous. $tmpDataId = $tmpObject->getDataId(); $select->joinLeft($tmpDataTable, $prevConstraint . ' = ' . $tmpDataId); if (!empty($tmpIndexTable)) { $tmpIndexId = $tmpObject->getIndexId(); $select->joinLeft($tmpIndexTable, $constraint . ' = ' . $tmpIndexId); $select->where($tmpObject->getIndexLanguageId() . ' = ?', $this->_defaultEditLanguage); } } } } return $select; }
/** * Ordering to be applied on the result set. * * @param boolean $order Sort ascending if true, descending otherwise. * @return Opus_DocumentFinder Fluent interface. */ public function orderByTitleMain($order = true) { $this->select->joinLeft(array('t' => 'document_title_abstracts'), 't.document_id = d.id AND t.type = "main"', array())->group('d.id')->order('t.value ' . ($order ? 'ASC' : 'DESC')); return $this; }