/** * Load Map builders. * * @param string $connectionName A connection name. */ protected function loadMapBuilders($connectionName = null) { if (null !== $this->dbMap) { return; } $this->dbMap = Propel::getDatabaseMap($connectionName); if (0 === count($this->dbMap->getTables())) { $finder = new Finder(); $files = $finder->files()->name('*TableMap.php')->in($this->getModelSearchPaths($connectionName))->notName('TableMap.php')->exclude('PropelBundle')->exclude('Tests'); foreach ($files as $file) { $class = $this->guessFullClassName($file->getRelativePath(), basename($file, '.php')); if (null !== $class && $this->isInDatabase($class, $connectionName)) { $this->dbMap->addTableFromMapClass($class); } } } }
function getLessonTags($lesson_id, $order_by = 'vote_count') { # Get lesson object $lesson_object = getLesson($lesson_id); # Get lesson tags IDs $lesson_tags_ids = $lesson_object->getLessonTags()->getPrimaryKeys(); # Get tag verse column to sort by $tag_verse_column_to_order_by = Propel::getDatabaseMap()->getTableByPhpName('TagVerse')->getColumnByPhpName('VerseId')->getFullyQualifiedName(); # Get applicable tag objects $tags_objects = TagQuery::create()->useLessonTagQuery()->filterByPrimaryKeys($lesson_tags_ids)->endUse()->_if($order_by == 'vote_count')->orderByVoteCount()->_elseif($order_by == 'date_tagged')->orderById('DESC')->_endif()->joinWithTagVerse()->addAscendingOrderByColumn($tag_verse_column_to_order_by)->find(); # Handle lesson tags objects $lesson_tags_to_return = []; foreach ($tags_objects as $tag_object) { # Append lesson tag to lesson tags to return $lesson_tags_to_return[] = ['id' => $tag_object->getId()]; } # Return lesson tags return $lesson_tags_to_return; }
protected function setUp() { parent::setUp(); $this->databaseMap = Propel::getDatabaseMap('bookstore'); }
public function getTableMap() { return Propel::getDatabaseMap($this->dbName)->getTableByPhpName($this->class); }
/** * Method to create an SQL query based on values in a Criteria. * * This method creates only prepared statement SQL (using ? where values * will go). The second parameter ($params) stores the values that need * to be set before the statement is executed. The reason we do it this way * is to let the PDO layer handle all escaping & value formatting. * * @param Criteria $criteria Criteria for the SELECT query. * @param array &$params Parameters that are to be replaced in prepared statement. * @return string * @throws PropelException Trouble creating the query string. */ public static function createSelectSql(Criteria $criteria, &$params) { $db = Propel::getDB($criteria->getDbName()); $dbMap = Propel::getDatabaseMap($criteria->getDbName()); $fromClause = array(); $joinClause = array(); $joinTables = array(); $whereClause = array(); $orderByClause = array(); $orderBy = $criteria->getOrderByColumns(); $groupBy = $criteria->getGroupByColumns(); $ignoreCase = $criteria->isIgnoreCase(); // get the first part of the SQL statement, the SELECT part $selectSql = $db->createSelectSqlPart($criteria, $fromClause); // Handle joins // joins with a null join type will be added to the FROM clause and the condition added to the WHERE clause. // joins of a specified type: the LEFT side will be added to the fromClause and the RIGHT to the joinClause foreach ($criteria->getJoins() as $join) { $join->setDB($db); // add 'em to the queues.. if (!$fromClause) { $fromClause[] = $join->getLeftTableWithAlias(); } $joinTables[] = $join->getRightTableWithAlias(); $joinClause[] = $join->getClause($params); } // add the criteria to WHERE clause // this will also add the table names to the FROM clause if they are not already // included via a LEFT JOIN foreach ($criteria->keys() as $key) { $criterion = $criteria->getCriterion($key); $table = null; foreach ($criterion->getAttachedCriterion() as $attachedCriterion) { $tableName = $attachedCriterion->getTable(); $table = $criteria->getTableForAlias($tableName); if ($table !== null) { $fromClause[] = $table . ' ' . $tableName; } else { $fromClause[] = $tableName; $table = $tableName; } if (($criteria->isIgnoreCase() || $attachedCriterion->isIgnoreCase()) && $dbMap->getTable($table)->getColumn($attachedCriterion->getColumn())->isText()) { $attachedCriterion->setIgnoreCase(true); } } $criterion->setDB($db); $sb = ''; $criterion->appendPsTo($sb, $params); $whereClause[] = $sb; } // Unique from clause elements $fromClause = array_unique($fromClause); $fromClause = array_diff($fromClause, array('')); // tables should not exist in both the from and join clauses if ($joinTables && $fromClause) { foreach ($fromClause as $fi => $ftable) { if (in_array($ftable, $joinTables)) { unset($fromClause[$fi]); } } } // Add the GROUP BY columns $groupByClause = $groupBy; $having = $criteria->getHaving(); $havingString = null; if ($having !== null) { $sb = ''; $having->appendPsTo($sb, $params); $havingString = $sb; } if (!empty($orderBy)) { foreach ($orderBy as $orderByColumn) { // Add function expression as-is. if (strpos($orderByColumn, '(') !== false) { $orderByClause[] = $orderByColumn; continue; } // Split orderByColumn (i.e. "table.column DESC") $dotPos = strrpos($orderByColumn, '.'); if ($dotPos !== false) { $tableName = substr($orderByColumn, 0, $dotPos); $columnName = substr($orderByColumn, $dotPos + 1); } else { $tableName = ''; $columnName = $orderByColumn; } $spacePos = strpos($columnName, ' '); if ($spacePos !== false) { $direction = substr($columnName, $spacePos); $columnName = substr($columnName, 0, $spacePos); } else { $direction = ''; } $tableAlias = $tableName; if ($aliasTableName = $criteria->getTableForAlias($tableName)) { $tableName = $aliasTableName; } $columnAlias = $columnName; if ($asColumnName = $criteria->getColumnForAs($columnName)) { $columnName = $asColumnName; } $column = $tableName ? $dbMap->getTable($tableName)->getColumn($columnName) : null; if ($criteria->isIgnoreCase() && $column && $column->isText()) { $ignoreCaseColumn = $db->ignoreCaseInOrderBy("{$tableAlias}.{$columnAlias}"); $orderByClause[] = $ignoreCaseColumn . $direction; $selectSql .= ', ' . $ignoreCaseColumn; } else { $orderByClause[] = $orderByColumn; } } } if (empty($fromClause) && $criteria->getPrimaryTableName()) { $fromClause[] = $criteria->getPrimaryTableName(); } // tables should not exist as alias of subQuery if ($criteria->hasSelectQueries()) { foreach ($fromClause as $key => $ftable) { if (strpos($ftable, ' ') !== false) { list($realtable, $tableName) = explode(' ', $ftable); } else { $tableName = $ftable; } if ($criteria->hasSelectQuery($tableName)) { unset($fromClause[$key]); } } } // from / join tables quoted if it is necessary if ($db->useQuoteIdentifier()) { $fromClause = array_map(array($db, 'quoteIdentifierTable'), $fromClause); $joinClause = $joinClause ? $joinClause : array_map(array($db, 'quoteIdentifierTable'), $joinClause); } // add subQuery to From after adding quotes foreach ($criteria->getSelectQueries() as $subQueryAlias => $subQueryCriteria) { $fromClause[] = '(' . BasePeer::createSelectSql($subQueryCriteria, $params) . ') AS ' . $subQueryAlias; } // build from-clause $from = ''; if (!empty($joinClause) && count($fromClause) > 1) { $from .= implode(" CROSS JOIN ", $fromClause); } else { $from .= implode(", ", $fromClause); } $from .= $joinClause ? ' ' . implode(' ', $joinClause) : ''; // Build the SQL from the arrays we compiled $sql = $selectSql . " FROM " . $from . ($whereClause ? " WHERE " . implode(" AND ", $whereClause) : "") . ($groupByClause ? " GROUP BY " . implode(",", $groupByClause) : "") . ($havingString ? " HAVING " . $havingString : "") . ($orderByClause ? " ORDER BY " . implode(",", $orderByClause) : ""); // APPLY OFFSET & LIMIT to the query. if ($criteria->getLimit() || $criteria->getOffset()) { $db->applyLimit($sql, $criteria->getOffset(), $criteria->getLimit(), $criteria); } return $sql; }
public function testGetTableByPhpNameNotLoaded() { $this->assertEquals('book', Propel::getDatabaseMap('bookstore')->getTableByPhpName('Book')->getName(), 'getTableByPhpName() can autoload a TableMap when the Peer class is generated and autoloaded'); }
/** * Get all the parameters to bind to this criteria * Does part of the job of BasePeer::createSelectSql() for the cache * * @return array list of parameters, each parameter being an array like * array('table' => $realtable, 'column' => $column, 'value' => $value) */ public function getParams() { $params = array(); $dbMap = Propel::getDatabaseMap($this->getDbName()); foreach ($this->getMap() as $criterion) { $table = null; foreach ($criterion->getAttachedCriterion() as $attachedCriterion) { $tableName = $attachedCriterion->getTable(); $table = $this->getTableForAlias($tableName); if (null === $table) { $table = $tableName; } if (($this->isIgnoreCase() || $attachedCriterion->isIgnoreCase()) && $dbMap->getTable($table)->getColumn($attachedCriterion->getColumn())->isText()) { $attachedCriterion->setIgnoreCase(true); } } $sb = ''; $criterion->appendPsTo($sb, $params); } $having = $this->getHaving(); if ($having !== null) { $sb = ''; $having->appendPsTo($sb, $params); } return $params; }