protected function getColName($phpName, $peerClass = null, $withPeerClass = false, $autoAddJoin = true) { if (array_key_exists($phpName, $this->withColumns)) { return $phpName; } if (strpos($phpName, '.') !== false) { // Table.Column list($class, $phpName) = explode('.', $phpName); if ($this->hasRelation($class)) { $relation = $this->relations[$class]; $toClass = $relation->getToClass(); if ($toClass == $class) { // Relation with no alias $peerClass = sfPropelFinderUtils::getPeerClassFromClass($relation->getToClass()); } else { // Relation with an alias return sfPropelFinderUtils::getColNameUsingAlias($class, $phpName, $toClass, $withPeerClass); } } elseif ($class == $this->alias) { $class = $this->class; } else { $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); } } else { if (strpos($phpName, '_') !== false) { // Table_Column, or Table_Name_Column, so explode is not a solution here $limit = strrpos($phpName, '_'); $class = substr($phpName, 0, $limit); $phpName = substr($phpName, $limit + 1); $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); } } if (!$peerClass) { // Column $peerClass = $this->peerClass; } if ($peerClass != $this->peerClass && !$this->hasRelation($class) && $autoAddJoin) { $this->join($class); } try { $column = call_user_func(array($peerClass, 'translateFieldName'), ucfirst($phpName), BasePeer::TYPE_PHPNAME, BasePeer::TYPE_COLNAME); return $withPeerClass ? array($peerClass, $column) : $column; } catch (PropelException $e) { throw new Exception(sprintf('sfPropelFinder: %s has no %s column', $peerClass, $phpName)); } }
/** * Finds a relation between two classes by introspection * A relation is found only if the foreign key lies in the columns of the first class * * @param string $from Propel Class name (e.g. 'Article') * @param string $to Propel Class name (e.g. 'Comment') * * @return sfPropelFinderRelation A relationship, or false if no relationship is found */ public function findRelation($from, $to) { $fromPeer = sfPropelFinderUtils::getPeerClassFromClass($from); $toPeer = sfPropelFinderUtils::getPeerClassFromClass($to); foreach (sfPropelFinderUtils::getColumnsForPeerClass($fromPeer) as $c) { if ($c->isForeignKey()) { if (!$this->databaseMap->containsTable($c->getRelatedTableName())) { $mapBuilder = call_user_func(array($toPeer, 'getMapBuilder')); $mapBuilder->doBuild(); } try { $tableMap = $this->databaseMap->getTable($c->getRelatedTableName()); } catch (PropelException $e) { // so $c->getRelatedTable() is not in the database map // even though the $phpName table map has been initialized // we are obviously looking for the wrong table here continue; } if ($tableMap->getPhpName() == $to) { return new sfPropelFinderRelation($from, constant($fromPeer . '::' . $c->getColumnName()), $to, $c->getRelatedName()); } } } return false; }
public function addSelectColumns($c) { $peerClass = sfPropelFinderUtils::getPeerClassFromClass($this->getToClass()); foreach (call_user_func(array($peerClass, 'getFieldNames'), BasePeer::TYPE_COLNAME) as $column) { if ($this->isAlias()) { $column = call_user_func(array($peerClass, 'alias'), $this->getAlias(), $column); } $c->addSelectColumn($column); } return $c; }
category_id: ~ article_i18n: content: varchar(255) category: id: ~ name: varchar(255) comment: id: ~ content: varchar(255) article_id: ~ author_id: ~ author: id: ~ name: varchar(255) Beware that the tables for these models will be emptied by the tests, so use a test database connection. */ include dirname(__FILE__).'/../../bootstrap.php'; $t = new lime_test(3, new lime_output_color()); $t->diag('sfPropelFinderUtils::getColNameUsingAlias()'); $col = sfPropelFinderUtils::getColNameUsingAlias('foo', 'Title', 'Article'); $t->is($col, 'foo.TITLE', 'getColNameUsingAlias() returns the correct colname when the columnname is simple'); $col = sfPropelFinderUtils::getColNameUsingAlias('c', 'ArticleId', 'Comment'); $t->is($col, 'c.ARTICLE_ID', 'getColNameUsingAlias() returns the correct colname when the column name is CamelCased'); $col = sfPropelFinderUtils::getColNameUsingAlias('Comment', 'ArticleId', 'Comment'); $t->is($col, 'Comment.ARTICLE_ID', 'getColNameUsingAlias() returns the correct colname even when the alias and the main class are the same');