$t->is($finder->getLatestQuery(), propel_sql('SELECT [P12article.ID, ]category.NAME AS "Category.Name", article.TITLE AS "Article.Title" FROM article INNER JOIN category ON (article.CATEGORY_ID=category.ID) LIMIT 1'), 'find() does not request twice the columns added by way of withColumn() and select()');
$expectedRow = array(
  'Article.Title' => 'art1',
  'Category.Name' => 'cat1'
);
$t->is_deeply($row, $expectedRow, 'select() can select columns added by way of withColumn()');

$finder = sfPropelFinder::from('Article')->
  join('Category')->
  withColumn('Category.Name', 'cname')->
  select(array('Article.Title', 'cname'));
$row = $finder->findOne();
$t->is($finder->getLatestQuery(), propel_sql('SELECT [P12article.ID, ]category.NAME AS cname, article.TITLE AS "Article.Title" FROM article INNER JOIN category ON (article.CATEGORY_ID=category.ID) LIMIT 1'), 'select() uses any alias specified in withColumn() in the query');
$expectedRow = array(
  'Article.Title' => 'art1',
  'cname'         => 'cat1'
);
$t->is_deeply($row, $expectedRow, 'select() uses any alias specified in withColumn() in the result');

$finder = sfPropelFinder::from('Article')->
  join('Category')->
  withColumn('Category.Name')->
  withColumn('Article.Title')->
  select(array('Article.Title', 'Category.Name'));
$row = $finder->findOne();
$t->is($finder->getLatestQuery(), propel_sql('SELECT [P12article.ID, ]category.NAME AS "Category.Name", article.TITLE AS "Article.Title" FROM article INNER JOIN category ON (article.CATEGORY_ID=category.ID) LIMIT 1'), 'select() can cope with only columns added with withColumn()');
$expectedRow = array(
  'Article.Title' => 'art1',
  'Category.Name' => 'cat1'
);
$t->is_deeply($row, $expectedRow, 'select() can cope with only columns added with withColumn()');
Example #2
0
 /**
  * Collection initializer
  *
  * @param array $from Array of model objects of the same class
  * @param string $class Optional classname of the desired objects
  * @param string $class Optional column name of the primary key
  *
  * @return DbFinder a finder object
  * @throws Exception If the array is empty, contains not model objects or composite objects
  */
 public static function fromCollection($collection, $class = '', $pkName = '')
 {
     if (count($collection) == 0) {
         if (!$class) {
             throw new Exception('A DbFinder cannot be initialized with an empty array and no class');
         } else {
             return self::fromClass($class);
         }
     }
     $testObject = $collection[0];
     if ($testObject instanceof BaseObject) {
         $finder = sfPropelFinder::fromCollection($collection, $class, $pkName);
     } else {
         if ($testObject instanceof Doctrine_Record) {
             $finder = sfDoctrineFinder::fromCollection($collection, $class, $pkName);
         } else {
             throw new Exception('A DbFinder can only be initialized from an array of Propel or Doctrine objects');
         }
     }
     if (class_exists($finderClass = get_class($testObject) . 'Finder')) {
         $dbFinder = new $finderClass();
         $dbFinder->setQueryObject($finder->getQueryObject());
     } else {
         $me = __CLASS__;
         $dbFinder = new $me($finder);
     }
     return $dbFinder;
 }
 public function getColName($phpName, $peerClass = null, $withPeerClass = false, $autoAddJoin = false)
 {
   return parent::getColName($phpName, $peerClass, $withPeerClass, $autoAddJoin);
 }
$human3->save();

$finder = sfPropelFinder::from('Human')->
  join('Human father', 'Human.FatherId', 'father.Id', 'INNER JOIN')->
  join('Human mother', 'Human.MotherId', 'mother.Id', 'INNER JOIN')->
  where('father.Name', 'John')->
  where('mother.Name', 'Jane');
$nbHumans = $finder->count();
$t->is($nbHumans, 1, 'join() allows to join to the current table with several foreign keys using an alias');
$t->is($finder->getLatestQuery(), propel_sql('SELECT COUNT([P13*][P12human.ID]) FROM human INNER JOIN human father ON (human.FATHER_ID=father.ID) INNER JOIN human mother ON (human.MOTHER_ID=mother.ID) WHERE (father.NAME=\'John\' AND mother.NAME=\'Jane\')'), 'join() uses aliased table names when using an alias relation');

HumanPeer::doDeleteAll();
$human1 = new Human();
$human1->setName('John');
$human1->save();
$human2 = new Human();
$human2->setName('Albert');
$human2->setHumanRelatedByFatherId($human1);
$human2->save();
$human3 = new Human();
$human3->setName('Jane');
$human3->setHumanRelatedByFatherId($human2);
$human3->save();

$finder = sfPropelFinder::from('Human')->
  join('Human father', 'Human.FatherId', 'father.Id', 'INNER JOIN')->
  join('Human grandfather', 'father.FatherId', 'grandfather.Id', 'INNER JOIN')->
  where('grandfather.Name', 'John');
$nbHumans = $finder->count();
$t->is($nbHumans, 1, 'join() allows to join to the current table with several foreign keys using an alias');
$t->is($finder->getLatestQuery(), propel_sql('SELECT COUNT([P13*][P12human.ID]) FROM human INNER JOIN human father ON (human.FATHER_ID=father.ID) INNER JOIN human grandfather ON (father.FATHER_ID=grandfather.ID) WHERE grandfather.NAME=\'John\''), 'join() uses aliased table names when using an alias relation');
  leftJoin('Article')->with('Article');
$comments = $finder->find();
$latestQuery = $finder->getLatestQuery();
$t->is($comments[0]->getAuthor(), null, 'First object has no author');
$t->is(Propel::getConnection()->getLastExecutedQuery(), $latestQuery, 'Related hydration occurred correctly');
$t->isnt($comments[0]->getArticle(), null, 'First object has an article');
$t->is(Propel::getConnection()->getLastExecutedQuery(), $latestQuery, 'Related hydration occurred correctly');
$t->isnt($comments[1]->getAuthor(), null, 'Second object has an author');
$t->is(Propel::getConnection()->getLastExecutedQuery(), $latestQuery, 'Related hydration occurred correctly');
$t->isnt($comments[1]->getArticle(), null, 'Second object has an article');
$t->is(Propel::getConnection()->getLastExecutedQuery(), $latestQuery, 'Related hydration occurred correctly');

$t->diag('sfPropelFinder::with() and exotic phpNames');

CivilityPeer::doDeleteAll();
HumanPeer::doDeleteAll();
$civility1 = new Civility();
$civility1->setIsMan(true);
$civility1->save();
$person1 = new Person();
$person1->setName('John');
$person1->setCivility($civility1);
$person1->save();

$finder = sfPropelFinder::from('Person')->
  with('Civility');
$person = $finder->findOne();
$latestQuery = $finder->getLatestQuery();
$t->is($person->getCivility()->getIsMan(), true, 'Related Objects with a non-natural phpName get hydrated correctly');
$t->is(Propel::getConnection()->getLastExecutedQuery(), $latestQuery, 'Related hydration occurred correctly');
$articles = $finder->filter(array(
  'Title' => '*foo*',
  'CategoryId' => '1'
))->find();

$t->is($finder->getLatestQuery(), $query, 'filter() calls filterBy() on each condition');

$finder = new sfPropelFinder('Article');
$articles = $finder->filter(array(
  'title' => '*foo*',
  'category_id' => '1'
), true)->find();

$t->is($finder->getLatestQuery(), $query, 'filter() converts underscore column names to CamelCase when the second argument is true');

$finder = new sfPropelFinder('Article');
$c = $finder->filter(array(
  'Title' => '*foo*',
  'CategoryId' => '1'
), false, array('Title'))->getCriteria();

$expectedC = new Criteria();
$expectedC->add(ArticlePeer::TITLE, '%foo%', Criteria::LIKE);

$t->ok($c->equals($expectedC), 'filter() ignores column names that are not part of the third argument if passed');

$called = false;

class myArticleFinder extends DbFinder
{
  protected $class = 'Article';
$t->isnt($finder->getLatestQuery(), 'UPDATE article SET TITLE = \'updated title\' WHERE article.TITLE=\'bar\'', 'set() issues an Update query on every record when passed a finder with $forceIndividualSaves set to true');
$t->is(sfPropelFinder::from('Article')->where('Title', 'updated title')->count(), 2, 'set() updates only the records found based on the array of values, even with $forceIndividualSaves set to true');

try
{
  sfPropelFinder::from('Comment')->joinArticle()->where('Article_Title', 'updated title')->set(array('Title' => 3));
  $t->fail('set() throws an exception when called on a finder with join()');
}
catch( Exception $e )
{
  $t->pass('set() throws an exception when called on a finder with join()');
}

$t->diag('Table alias');

ArticlePeer::doDeleteAll();
$article1 = new Article();
$article1->setTitle('abc');
$article1->save();
$article = sfPropelFinder::from('Article a')->where('a.Title', 'abc')->findOne();
$t->is($article->getId(), $article1->getId(), 'from() accepts a table alias');

$t->diag('Debugging functions');

$finder = sfPropelFinder::from('Article')->where('Title', 'foo');
$t->isa_ok($finder->getQueryObject(), 'Criteria', 'getQueryObject() returns the criteria as composed by the finder');
$finder->findOne();
$t->is($finder->getLatestQuery(), 'SELECT article.ID, article.TITLE, article.CATEGORY_ID FROM article WHERE article.TITLE=\'foo\' LIMIT 1', 'getLatestQuery() returns the latest SQL query');
$finder = sfPropelFinder::from('Article')->add(ArticlePeer::TITLE, 'bar');
$finder->findOne();
$t->is($finder->getLatestQuery(), 'SELECT article.ID, article.TITLE, article.CATEGORY_ID FROM article WHERE article.TITLE=\'bar\' LIMIT 1', 'you can call Criteria methods directly on the finder object to modify its Criteria');
 /**
  * __construct
  *
  * @return void
  */
 public function __construct($class, $defaultMaxPerPage = 10)
 {
     parent::__construct($class, $defaultMaxPerPage);
     $this->setFinder(sfPropelFinder::from($class));
 }