Changeset 10606
- Timestamp:
- 08/02/08 00:16:44 (4 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (1 diff)
- plugins/sfPropelFinderPlugin/lib/DbFinder.php (modified) (4 diffs)
- plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php (modified) (5 diffs)
- plugins/sfPropelFinderPlugin/lib/sfDoctrineFinderListener.php (added)
- plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r10592 r10606 611 611 == Changelog == 612 612 613 === 2008-08-01 | Trunk === 614 613 === 2008-08-02 | Trunk === 614 615 * francois: Implemented `sfDoctrineFinder::fromArray()`, and `sfDoctrineFinder::getLatestQuery()` 615 616 * francois: Added `DbFinderAdminGenerator` (WIP) 616 617 * francois: Fixed problem with `join()` and `with()` when called by children of `sfPropelPager` plugins/sfPropelFinderPlugin/lib/DbFinder.php
r10540 r10606 34 34 return self::fromClass($from); 35 35 } 36 if (is_array($from) )36 if (is_array($from) || $from instanceof Doctrine_Collection) 37 37 { 38 38 return self::fromCollection($from); … … 117 117 else 118 118 { 119 $pks []= $object->indentifier(); 119 $identifier = array_values($object->identifier()); 120 if(count($identifier) == 1) 121 { 122 $identifier = array_shift($identifier); 123 } 124 $pks []= $identifier; 120 125 } 121 126 … … 147 152 foreach ($tempObject->getTable()->getColumns() as $name => $column) 148 153 { 149 if( $column->contains('primary'))154 if(array_key_exists('primary', $column)) 150 155 { 151 156 if($pkName) … … 175 180 { 176 181 $finder = self::fromClass($class); 177 $finder->where($pkName, ' in', $array);182 $finder->where($pkName, 'in', $array); 178 183 179 184 return $finder; plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php
r10344 r10606 12 12 class sfDoctrineFinder extends DbFinder 13 13 { 14 protected $class = null; 15 protected $object = null; 16 protected $latestQuery = ''; 17 protected $withClasses = array(); 18 protected $withColumns = array(); 19 protected $culture = null; 14 protected 15 $class = null, 16 $object = null, 17 $queryListener = null, 18 $withClasses = array(), 19 $withColumns = array(), 20 $culture = null; 20 21 21 22 public function __construct($class = '') 22 23 { 23 list($class, $alias) = $this->getClassAndAlias($class);24 $this->query = Doctrine_Query::create()->from($class . ' ' . $alias);25 24 if($class) 26 25 { 26 list($class, $alias) = $this->getClassAndAlias($class); 27 27 $this->setClass($class, $alias); 28 $this->object = new $class(); 28 } 29 elseif($this->class) 30 { 31 list($class, $alias) = $this->getClassAndAlias($this->class); 32 $this->setClass($class, $alias); 29 33 } 30 34 } … … 39 43 $this->addRelation($class, $alias); 40 44 $this->class = $class; 45 $this->object = new $class(); 46 $this->query = Doctrine_Query::create()->from($class . ' ' . $alias); 47 $this->queryListener = sfDoctrineFinderListener::getInstance(); 48 41 49 42 50 return $this; … … 56 64 public function getLatestQuery($con = null) 57 65 { 58 throw new Exception('This method is not yet implemented');66 return $this->queryListener->getLatestQuery(); 59 67 } 60 68 … … 189 197 if(is_array($pk)) 190 198 { 191 $this->addCondition($pkColumns[0], ' in', $pk);199 $this->addCondition($pkColumns[0], ' IN ', $pk); 192 200 return $this->find(); 193 201 } … … 302 310 protected function addCondition($column, $comparison, $value) 303 311 { 304 if($comparison == ' in')312 if($comparison == ' IN ') 305 313 { 306 314 $this->query->whereIn($column, $value); plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php
r10344 r10606 18 18 title: string(255) 19 19 category_id: integer 20 relations: 21 Category: 22 class: DCategory 23 local: category_id 24 type: one 25 foreign: id 26 foreignAlias: Articles 20 27 DArticleI18n: 21 28 columns: … … 64 71 Doctrine_Query::create()->delete()->from('DArticle')->execute(); 65 72 66 $t = new lime_test( 36, new lime_output_color());73 $t = new lime_test(47, new lime_output_color()); 67 74 68 75 $t->diag('find()'); … … 252 259 $articleI18n = sfDoctrineFinder::from('DArticleI18n')->findPk(array($articlei18n1->getId(), $articlei18n1->getCulture())); 253 260 $t->is_deeply($articleI18n->toArray(), $articlei18n1->toArray(), 'findPk() retrieves objects with composite primary keys based on an array of Pks'); 261 262 $t->diag('Instanciation possibilities'); 263 264 Doctrine_Query::create()->delete()->from('DArticle')->execute(); 265 266 $article1 = new DArticle(); 267 $article1->setTitle('foo'); 268 $article1->save(); 269 270 $article2 = new DArticle(); 271 $article2->setTitle('foo2'); 272 $article2->save(); 273 274 $article3 = new DArticle(); 275 $article3->setTitle('foo3'); 276 $article3->save(); 277 278 $finder = new sfDoctrineFinder('DArticle'); 279 $t->is($finder->getClass(), 'DArticle', 'Record Class can be set during instanciation, in which case the finder is automatically initialized'); 280 $articles = $finder->find(); 281 $article = $articles[0]; 282 $t->is($article->getTitle(), 'foo', 'A finder instanciated directly with a Record class returns the correct objects'); 283 284 $finder = new sfDoctrineFinder(); 285 $finder->setClass('DArticle'); 286 $t->is($finder->getClass(), 'DArticle', 'setClass() and getClass() are accesors to the protected $class property'); 287 $articles = $finder->find(); 288 $article = $articles[0]; 289 $t->is($article->getTitle(), 'foo', 'A finder can be instanciated without parameter, and initialized later after defining its class'); 290 291 $articles = sfDoctrineFinder::from('DArticle')->find(); 292 $t->is(count($articles), 3, 'from() allows direct chaining of conditions'); 293 294 class DArticleFinder extends sfDoctrineFinder 295 { 296 protected $class = 'DArticle'; 297 } 298 $finder = new DArticleFinder(); 299 $article = $finder->findOne(); 300 $t->isa_ok($article, 'DArticle', 'A finder extending sfDoctrineFinder can be used directly if defining the $class property'); 301 $finder = new DArticleFinder(); 302 $articles = $finder->find(); 303 $t->is(count($articles), 3, 'A finder extending sfDoctrineFinder can be used directly if defining the $class property'); 304 305 Doctrine_Query::create()->delete()->from('DArticle')->execute(); 306 Doctrine_Query::create()->delete()->from('DCategory')->execute(); 307 $category1 = new DCategory(); 308 $category1->setName('cat1'); 309 $category1->save(); 310 $category2 = new DCategory(); 311 $category2->setName('cat2'); 312 $category2->save(); 313 $article1 = new DArticle(); 314 $article1->setTitle('aaaaa'); 315 $article1->setCategory($category1); 316 $article1->save(); 317 $article2 = new DArticle(); 318 $article2->setTitle('bbbbb'); 319 $article2->setCategory($category1); 320 $article2->save(); 321 $article3 = new DArticle(); 322 $article3->setTitle('ccccc'); 323 $article3->setCategory($category2); 324 $article3->save(); 325 326 $finder = sfDoctrineFinder::from($category1->getArticles()); 327 $articles = $finder->find(); 328 $t->is(count($articles), 2, 'from() accepts an array of Doctrine objects'); 329 $t->isnt( 330 strpos($finder->getLatestQuery(), "WHERE d.id IN ("), 331 false, 332 'using from() with an array of Propel objects results in a IN' 333 ); 334 $t->isa_ok($finder->findOne(), 'DArticle', 'using from() with an array of Doctrine objects returns some of these objects'); 335 $articles = sfDoctrineFinder::from($category1->getArticles())->where('Title', 'aaaaa')->find(); 336 $t->is(count($articles), 1, 'A finder initialized from an array accepts further conditions');