Development

Changeset 9948

You must first sign up to be able to contribute.

Changeset 9948

Show
Ignore:
Timestamp:
06/27/08 23:18:57 (3 months ago)
Author:
francois
Message:

sfPropelFinderPlugin sfPropelFinder::from() now accepts an array of Propel objects

Files:

Legend:

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

    r9941 r9948  
    172172 
    173173Compare it to the code required to get these `Comment` objects without `sfPropelFinder`, and you will understand all the benefits the `relatedTo()` method provide. 
     174 
     175Tip: Alternatively, a finder can be initialized from an array of Propel object. The resulting SQL query contains a 'IN ()' clause, so use this possibility with caution. 
     176 
     177{{{ 
     178#!php 
     179<?php 
     180// Retrieving the last one of the related comments 
     181$comments = sfPropelFinder::from($article->getComments())-> 
     182  findLast(); 
     183}}} 
    174184 
    175185=== Joins === 
     
    393403=== 2008-06-27 | Trunk === 
    394404 
     405 * francois: `sfPropelFinder::from()` now accepts an array of Propel objects 
    395406 * francois: Added `sfPropelFinder::findByXXX()` and `sfPropelFinder::findOneByXXX()` methods 
    396407 * francois: Added `sfPropelFinder::relatedTo()` method 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r9941 r9948  
    127127  // Finder Initializers 
    128128   
    129   public static function from($class) 
     129  /** 
     130   * Mixed initializer 
     131   * Accepts either a string (Propel class) or an array of Propel objects 
     132   * 
     133   * @param mixed $from The data to initialize the finder with 
     134   * @return sfPropelFinder a finder object 
     135   * @throws Exception If the data is neither a classname nor an array 
     136   */ 
     137  public static function from($from) 
     138  { 
     139    if (is_string($from)) 
     140    { 
     141      return self::fromClass($from); 
     142    } 
     143    if (is_array($from)) 
     144    { 
     145      return self::fromCollection($from); 
     146    } 
     147    throw new Exception('from() only accepts a Propel object classname or an array of Propel objects'); 
     148  } 
     149 
     150  /** 
     151   * Class initializer 
     152   * 
     153   * @param string $from Propel classname on which the search will be done 
     154   * @return sfPropelFinder a finder object 
     155   */ 
     156  public static function fromClass($class) 
    130157  { 
    131158    $me = __CLASS__; 
    132159    $finder = new $me($class); 
     160     
     161    return $finder; 
     162  } 
     163   
     164  /** 
     165   * Collection initializer 
     166   * 
     167   * @param array $from Array of Propel objects of the same class 
     168   * @param string $class Optional classname of the desired objects 
     169   * @param string $class Optional column name of the primary key 
     170   * 
     171   * @return sfPropelFinder a finder object 
     172   * @throws Exception If the array is empty, contains not Propel objects or composite objects 
     173   */ 
     174  public static function fromCollection($collection, $class = '', $pkName = '') 
     175  { 
     176    $pks = array(); 
     177    foreach($collection as $object) 
     178    { 
     179      if($class != get_class($object)) 
     180      { 
     181        if($class) 
     182        { 
     183          throw new Exception('A finder can only be initialized from an array of objects of a single class'); 
     184        } 
     185        if($object instanceof BaseObject) 
     186        { 
     187          $class = get_class($object); 
     188        } 
     189        else 
     190        { 
     191          throw new Exception('A finder can only be initialized from an array of Propel objects'); 
     192        } 
     193      } 
     194      $pks []= $object->getPrimaryKey(); 
     195    } 
     196    if(!$class) 
     197    { 
     198      throw new Exception('A finder cannot be initialized with an empty array'); 
     199    } 
     200     
     201    $tempObject = new $class(); 
     202    foreach ($tempObject->getPeer()->getTableMap()->getColumns() as $column) 
     203    { 
     204      if($column->isPrimaryKey()) 
     205      { 
     206        if($pkName) 
     207        { 
     208          throw new Exception('A finder cannot be initialized from an array of objects with several foreign keys'); 
     209        } 
     210        else 
     211        { 
     212          $pkName = $column->getFullyQualifiedName(); 
     213        } 
     214      } 
     215    } 
     216     
     217    return self::fromArray($pks, $class, $pkName); 
     218  } 
     219   
     220  /** 
     221   * Array initializer 
     222   * 
     223   * @param array $array Array of Primary keys 
     224   * @param string $class Propel classname on which the search will be done 
     225   * 
     226   * @return sfPropelFinder a finder object 
     227   */ 
     228  public static function fromArray($array, $class, $pkName) 
     229  { 
     230    $finder = self::fromClass($class); 
     231    $finder->add($pkName, $array, Criteria::IN); 
    133232     
    134233    return $finder; 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r9941 r9948  
    6363ArticlePeer::doDeleteAll(); 
    6464 
    65 $t = new lime_test(122, new lime_output_color()); 
     65$t = new lime_test(126, new lime_output_color()); 
    6666 
    6767$t->diag('find()'); 
     
    249249$finder = new ArticleFinder(); 
    250250$article = $finder->findOne(); 
    251 $t->is(get_class($article), 'Article', 'A finder extending sfPropelFinder can be used directly if defining the $peerClass property'); 
     251$t->isa_ok($article, 'Article', 'A finder extending sfPropelFinder can be used directly if defining the $peerClass property'); 
    252252$finder = new ArticleFinder(); 
    253253$articles = $finder->find(); 
    254254$t->is(count($articles), 3, 'A finder extending sfPropelFinder can be used directly if defining the $peerClass property'); 
     255 
     256ArticlePeer::doDeleteAll(); 
     257CategoryPeer::doDeleteAll(); 
     258$category1 = new Category(); 
     259$category1->setName('cat1'); 
     260$category1->save(); 
     261$category2 = new Category(); 
     262$category2->setName('cat2'); 
     263$category2->save(); 
     264$article1 = new Article(); 
     265$article1->setTitle('aaaaa'); 
     266$article1->setCategory($category1); 
     267$article1->save(); 
     268$article2 = new Article(); 
     269$article2->setTitle('bbbbb'); 
     270$article2->setCategory($category1); 
     271$article2->save(); 
     272$article3 = new Article(); 
     273$article3->setTitle('ccccc'); 
     274$article3->setCategory($category2); 
     275$article3->save(); 
     276 
     277$finder = sfPropelFinder::from($category1->getArticles()); 
     278$articles = $finder->find(); 
     279$t->is(count($articles), 2, 'from() accepts an array of Propel objects'); 
     280$t->isnt( 
     281  strpos($finder->getLatestQuery(), "WHERE article.ID IN ("), 
     282  false, 
     283  'using from() with an array of Propel objects results in a Criteria::IN' 
     284); 
     285$t->isa_ok($finder->findOne(), 'Article', 'using from() with an array of Propel objects returns some of these objects'); 
     286$articles = sfPropelFinder::from($category1->getArticles())->where('Title', 'aaaaa')->find(); 
     287$t->is(count($articles), 1, 'A finder initialized from an array accepts further conditions'); 
    255288 
    256289$t->diag('count()');