$t->diag('Cached results'); apc_clear_cache('user'); Doctrine_Query::create()->delete()->from('DArticle')->execute(); $article1 = new DArticle(); $article1->setTitle('foo1'); $article1->save(); $article2 = new DArticle(); $article2->setTitle('foo2'); $article2->save(); DbFinder::from('DArticle')->useCache($cache, 10)->where('Title', 'foo1')->findOne(); // normal query $article = DbFinder::from('DArticle')->useCache($cache, 10)->where('Title', 'foo1')->findOne(); // cached query $t->isa_ok($article, 'DArticle', 'Cached finder queries return Model objects'); $t->is($article->getId(), $article1->getId(), 'find() finder queries can be cached'); DbFinder::from('DArticle')->useCache($cache, 10)->where('Title', 'foo1')->count(); // normal query $nb = DbFinder::from('DArticle')->useCache($cache, 10)->where('Title', 'foo1')->count(); // cached query $t->is($nb, 1, 'count() finder queries can be cached'); apc_clear_cache('user'); DbFinder::from('DArticle')->useCache($cache, 10)->where('Title', 'foo1')->limit(1)->find(); // normal query $article = DbFinder::from('DArticle')->useCache($cache, 10)->where('Title', 'foo1')->findOne(); // cached query $t->isa_ok($article, 'DArticle', 'Cached queries return the correct result type'); $t->diag('useCache(true)'); apc_clear_cache('user');
$t->pass('getColumn() is not available as long as you don\'t add a column with withColumn()'); } Doctrine_Query::create()->delete()->from('DComment')->execute(); Doctrine_Query::create()->delete()->from('DArticle')->execute(); $article1 = new DArticle(); $article1->setTitle('bbbbb'); $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(); Doctrine::getTable('DArticle')->clear(); Doctrine::getTable('DComment')->clear(); $finder = sfDoctrineFinder::from('DComment')-> join('DArticle')-> withColumn('DArticle.Title'); $comment = $finder->findOne(); $t->is($comment['Article']['DArticle.Title'], 'bbbbb', 'Additional columns added with withColumn() are stored in the object and can be retrieved as properties of the related object'); try { $t->is($comment->getColumn('DArticle.Title'), 'bbbbb', 'Additional columns added with withColumn() are stored in the object and can be retrieved with getColumn()'); }
$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');