Development

Changeset 10606

You must first sign up to be able to contribute.

Changeset 10606

Show
Ignore:
Timestamp:
08/02/08 00:16:44 (4 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Implemented sfDoctrineFinder::fromArray(), and sfDoctrineFinder::getLatestQuery()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelFinderPlugin/README

    r10592 r10606  
    611611== Changelog == 
    612612 
    613 === 2008-08-01 | Trunk === 
    614  
     613=== 2008-08-02 | Trunk === 
     614 
     615 * francois: Implemented `sfDoctrineFinder::fromArray()`, and `sfDoctrineFinder::getLatestQuery()` 
    615616 * francois: Added `DbFinderAdminGenerator` (WIP) 
    616617 * francois: Fixed problem with `join()` and `with()` when called by children of `sfPropelPager` 
  • plugins/sfPropelFinderPlugin/lib/DbFinder.php

    r10540 r10606  
    3434      return self::fromClass($from); 
    3535    } 
    36     if (is_array($from)
     36    if (is_array($from) || $from instanceof Doctrine_Collection
    3737    { 
    3838      return self::fromCollection($from); 
     
    117117      else 
    118118      { 
    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; 
    120125      } 
    121126       
     
    147152      foreach ($tempObject->getTable()->getColumns() as $name => $column) 
    148153      { 
    149         if($column->contains('primary')) 
     154        if(array_key_exists('primary', $column)) 
    150155        { 
    151156          if($pkName) 
     
    175180  { 
    176181    $finder = self::fromClass($class); 
    177     $finder->where($pkName, ' in ', $array); 
     182    $finder->where($pkName, 'in', $array); 
    178183     
    179184    return $finder; 
  • plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php

    r10344 r10606  
    1212class sfDoctrineFinder extends DbFinder 
    1313{ 
    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; 
    2021 
    2122  public function __construct($class = '') 
    2223  { 
    23     list($class, $alias) = $this->getClassAndAlias($class); 
    24     $this->query = Doctrine_Query::create()->from($class . ' ' . $alias); 
    2524    if($class) 
    2625    { 
     26      list($class, $alias) = $this->getClassAndAlias($class); 
    2727      $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); 
    2933    } 
    3034  } 
     
    3943    $this->addRelation($class, $alias); 
    4044    $this->class = $class; 
     45    $this->object = new $class(); 
     46    $this->query = Doctrine_Query::create()->from($class . ' ' . $alias); 
     47    $this->queryListener = sfDoctrineFinderListener::getInstance(); 
     48     
    4149     
    4250    return $this; 
     
    5664  public function getLatestQuery($con = null) 
    5765  { 
    58     throw new Exception('This method is not yet implemented'); 
     66    return $this->queryListener->getLatestQuery(); 
    5967  } 
    6068   
     
    189197      if(is_array($pk)) 
    190198      { 
    191         $this->addCondition($pkColumns[0], 'in', $pk); 
     199        $this->addCondition($pkColumns[0], ' IN ', $pk); 
    192200        return $this->find(); 
    193201      } 
     
    302310  protected function addCondition($column, $comparison, $value) 
    303311  { 
    304     if($comparison == 'in') 
     312    if($comparison == ' IN ') 
    305313    { 
    306314      $this->query->whereIn($column, $value); 
  • plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php

    r10344 r10606  
    1818        title:       string(255) 
    1919        category_id: integer 
     20      relations: 
     21        Category: 
     22          class:    DCategory 
     23          local:    category_id 
     24          type:     one 
     25          foreign:  id 
     26          foreignAlias: Articles 
    2027    DArticleI18n: 
    2128      columns: 
     
    6471Doctrine_Query::create()->delete()->from('DArticle')->execute(); 
    6572 
    66 $t = new lime_test(36, new lime_output_color()); 
     73$t = new lime_test(47, new lime_output_color()); 
    6774 
    6875$t->diag('find()'); 
     
    252259$articleI18n = sfDoctrineFinder::from('DArticleI18n')->findPk(array($articlei18n1->getId(), $articlei18n1->getCulture())); 
    253260$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 
     264Doctrine_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 
     294class 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 
     305Doctrine_Query::create()->delete()->from('DArticle')->execute(); 
     306Doctrine_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');