$article2 = new DArticle();
$article2->setTitle('bbb');
$article2->save();

$article = sfDoctrineFinder::from('DArticle')->leftJoin('DCategory')->with('DCategory')->findLast();
$category = $article->getCategory();
if (is_object($category))
{
  $t->isa_ok($article->getCategory(), 'Doctrine_Null', 'In a left join using with(), empty related objects are not hydrated');
}
else
{
  $t->isa_ok($article->getCategory(), 'NULL', 'In a left join using with(), empty related objects are not hydrated');
}

Doctrine_Query::create()->delete()->from('DComment')->execute();
Doctrine_Query::create()->delete()->from('DArticle')->execute();
Doctrine_Query::create()->delete()->from('DCategory')->execute();
Doctrine_Query::create()->delete()->from('DAuthor')->execute();

$t->diag('sfDoctrineFinder::withColumn() on calculated columns with decimals');
$finder = sfDoctrineFinder::from('DArticle');
try
{
  $finder->withColumn('COUNT(DComment.Id) * 1.5', 'foo')->findOne();
  $t->pass('withColumn() doesn\'t transform decimal numbers');
}
catch(Exception $e)
{
  $t->fail('withColumn() doesn\'t transform decimal numbers');
}
 /**
  * __construct
  *
  * @return void
  */
 public function __construct($class, $defaultMaxPerPage = 10)
 {
     parent::__construct($class, $defaultMaxPerPage);
     $this->setFinder(sfDoctrineFinder::from($class));
 }
$article2->setTitle('bar');
$article2->save();
$finder = sfDoctrineFinder::from('DArticle')->where('Title', 'foo');
$t->is($finder->set(array('Title' => 'updated title')), 1, 'set() returns the number of updated rows');
$t->is($finder->getLatestQuery(), 'UPDATE d_article SET title = \'updated title\' WHERE title = \'foo\'', 'set() issues an Update query when passed a finder');
$t->is(sfDoctrineFinder::from('DArticle')->where('Title', 'updated title')->count(), 1, 'set() updates only the records found based on the array of values');
$finder = sfDoctrineFinder::from('DArticle')->where('Title', 'bar');
$t->is($finder->set(array('Title' => 'updated title'), true), 1, 'set() returns the number of updated rows, even with $forceIndividualSaves set to true');
$t->isnt($finder->getLatestQuery(), 'UPDATE d_article SET title = \'updated title\' WHERE title = \'bar\'', 'set() issues an Update query on every record when passed a finder with $forceIndividualSaves set to true');
$t->is(sfDoctrineFinder::from('DArticle')->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 {
    sfDoctrineFinder::from('DComment')->joinArticle()->where('DArticle.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');
Doctrine_Query::create()->delete()->from('DArticle')->execute();
$article1 = new DArticle();
$article1->setTitle('abc');
$article1->save();
$article = sfDoctrineFinder::from('DArticle a')->where('a.Title', 'abc')->findOne();
$t->is($article->getId(), $article1->getId(), 'from() accepts a table alias');
$t->diag('Debugging functions');
$finder = sfDoctrineFinder::from('DArticle')->where('Title', 'foo');
$t->isa_ok($finder->getQueryObject(), 'Doctrine_Query', 'getQueryObject() returns the query object as composed by the finder');
$finder->findOne();
$t->is($finder->getLatestQuery(), 'SELECT d.id AS d__id, d.title AS d__title, d.category_id AS d__category_id FROM d_article d WHERE d.title = \'foo\' LIMIT 1', 'getLatestQuery() returns the latest SQL query');
$finder = sfDoctrineFinder::from('DArticle d')->addWhere('d.title = :foo', array(':foo' => 'bar'));
$finder->findOne();
$t->is($finder->getLatestQuery(), 'SELECT d.id AS d__id, d.title AS d__title, d.category_id AS d__category_id FROM d_article d WHERE d.title = \'bar\' LIMIT 1', 'you can call Doctrine_Query methods directly on the finder object to modify its query');
Doctrine_Query::create()->delete()->from('DComment')->execute();
Doctrine_Query::create()->delete()->from('DArticle')->execute();

$article1 = new DArticle();
$article1->setTitle('aaaaa');
$article1->setCategory($category1);
$article1->save();
$author1 = new DAuthor();
$author1->setName('John');
$author1->save();
$comment = new DComment();
$comment->setContent('foo');
$comment->setArticleId($article1->getId());
$comment->setAuthor($author1);
$comment->save();
$article = sfDoctrineFinder::from('DArticle')->
  join('DComment')->
  join('DAuthor')->
  where('DAuthor.Name', 'John')->
  findOne();
$t->is($article->getTitle(), 'aaaaa', 'you can chain several join() statements');
$article = sfDoctrineFinder::from('DArticle')->join('DComment')->where('DAuthor.Name', 'John')->findOne();
$t->is($article->getTitle(), 'aaaaa', 'join() can be omitted if column names are explicit');
$article = sfDoctrineFinder::from('DArticle')->joinDComment()->joinDAuthor()->where('DAuthor.Name', 'John')->findOne();
$t->is($article->getTitle(), 'aaaaa', 'joinXXX() does a join according to the XXX column name');

$comment = sfDoctrineFinder::from('DComment')->join('DArticle')->join('DAuthor')->where('DAuthor.Name', 'John')->findOne();
$t->is($comment->getContent(), 'foo', 'you can add several join() statements');
$t->is($comment->getArticle()->getTitle(), 'aaaaa', 'you can add several join() statements');
$t->is($comment->getAuthor()->getName(), 'John', 'you can add several join() statements');