Ejemplo n.º 1
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;
 }
$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');
}
$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');
 /**
  * __construct
  *
  * @return void
  */
 public function __construct($class, $defaultMaxPerPage = 10)
 {
     parent::__construct($class, $defaultMaxPerPage);
     $this->setFinder(sfDoctrineFinder::from($class));
 }
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');
 public function getColName($phpName, $class = null, $detail = false, $autoAddJoin = false)
 {
   return parent::getColName($phpName, $class, $detail, $autoAddJoin);
 }
$articles = $finder->filter(array(
  'Title' => '*foo*',
  'CategoryId' => '1'
))->find();

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

$finder = new sfDoctrineFinder('DArticle');
$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 sfDoctrineFinder('DArticle');
$finder->filter(array(
  'Title' => '*foo*',
  'CategoryId' => '1'
), false, array('Title'))->find();

$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 LIKE \'%foo%\'', 'filter() ignores column names that are not part of the third argument if passed');

$called = false;

class myArticleFinder extends DbFinder
{
  protected $class = 'DArticle';
  
  public function filterByTitle($value)
  {