Changeset 9948
- Timestamp:
- 06/27/08 23:18:57 (3 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (1 diff)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r9941 r9948 172 172 173 173 Compare it to the code required to get these `Comment` objects without `sfPropelFinder`, and you will understand all the benefits the `relatedTo()` method provide. 174 175 Tip: 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 }}} 174 184 175 185 === Joins === … … 393 403 === 2008-06-27 | Trunk === 394 404 405 * francois: `sfPropelFinder::from()` now accepts an array of Propel objects 395 406 * francois: Added `sfPropelFinder::findByXXX()` and `sfPropelFinder::findOneByXXX()` methods 396 407 * francois: Added `sfPropelFinder::relatedTo()` method plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r9941 r9948 127 127 // Finder Initializers 128 128 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) 130 157 { 131 158 $me = __CLASS__; 132 159 $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); 133 232 134 233 return $finder; plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r9941 r9948 63 63 ArticlePeer::doDeleteAll(); 64 64 65 $t = new lime_test(12 2, new lime_output_color());65 $t = new lime_test(126, new lime_output_color()); 66 66 67 67 $t->diag('find()'); … … 249 249 $finder = new ArticleFinder(); 250 250 $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'); 252 252 $finder = new ArticleFinder(); 253 253 $articles = $finder->find(); 254 254 $t->is(count($articles), 3, 'A finder extending sfPropelFinder can be used directly if defining the $peerClass property'); 255 256 ArticlePeer::doDeleteAll(); 257 CategoryPeer::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'); 255 288 256 289 $t->diag('count()');