Changeset 29166
- Timestamp:
- 04/15/10 23:52:48 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfDataSourcePlugin/trunk/README
r28744 r29166 3 3 4 4 This plugin allows to read various data sources in a unified way. These data sources can be filtered and sorted in a generic way, 5 so they can for example be berendered in a customizable grid.5 so they can for example be rendered in a customizable grid. 6 6 7 Currently arrays , Propel (1.5 supported, but also 1.3 although unsupported) and Doctrine tables can be used as data sources,7 Currently arrays (simple and associative), Propel (1.5 supported, but also 1.3 although unsupported) and Doctrine tables can be used as data sources, 8 8 You are however free to add more implementations, for example to process csv or xml data. Provided is also an imap implementation, 9 9 but this is also unsupported. … … 64 64 -------- 65 65 66 TODO: explain 66 The use of Doctrine is similar that with with Propel. If you like ObjectPaths, you can optionally install the 67 plugin [sfAlyssaDoctrineObjectPath](http://www.symfony-project.org/plugins/sfAlyssaDoctrineObjectPathPlugin) that 68 brings the same feature that sfPropelObjectPathBehaviorPlugin. 67 69 68 Note: Doctrine currently doesn't have an objectPath behavior, so the handling of relations is limited 70 69 71 70 72 Array … … 94 96 TODO: show more difficult schema's and relations 95 97 96 The real ben nefit of the sfDataSource will show up when used with the sfGrid(Plugin).98 The real benefit of the sfDataSource will show up when used with the sfGrid(Plugin). 97 99 RequireColumn is called automatically when you select columns for your grid, which in its turn will automatically join 98 100 related tables. Sorting on foreign fields or filtering on any table is now easy, because the model is aware of the relations. plugins/sfDataSourcePlugin/trunk/lib/sfDataSourceArray.class.php
r29155 r29166 107 107 $this->originalData = $data; 108 108 $this->data = $this->originalData; 109 109 110 110 } 111 111 … … 125 125 } 126 126 127 return $this->data[$this->key()+$this->getOffset()]; 127 // we do this to support associative arrays 128 $keys = array_keys($this->data); 129 130 return $this->data[$keys[$this->key()+$this->getOffset()]]; 128 131 } 129 132 … … 172 175 /** 173 176 * requireColumn checks if the source contains the desired column. 174 * If the source is an empty array, any column will be accepted, since 177 * If the source is an empty array, any column will be accepted, since 175 178 * the DataSource doesn't have any model-data to base its decision on 176 * 179 * 177 180 * @see sfDataSourceInterface::requireColumn() 178 181 */ … … 195 198 usort($this->data, array($this, 'sortCallback')); 196 199 } 197 200 198 201 /** 199 202 * Callback method used by usort(). Compares two arrays by the current … … 217 220 } 218 221 } 219 222 220 223 /** 221 224 * @see sfDataSourceInterface … … 224 227 { 225 228 // TODO: because of this, you should first Filter before you sort! 226 // TODO: possibly add sortState (asc,desc, none (per field)), and sort after filtering 229 // TODO: possibly add sortState (asc,desc, none (per field)), and sort after filtering 227 230 $this->data = array(); 228 231 229 232 $this->requireColumn($columnName); 230 233 … … 241 244 //TODO: implement filtering on an array 242 245 protected function filterCallback($row) 243 { 246 { 244 247 throw new Exception('This method has not been finished yet'); 245 248 } 246 249 247 250 } plugins/sfDataSourcePlugin/trunk/test/unit/datasource/sfDataSourceArrayTest.php
r29155 r29166 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 37, new lime_output_color());13 $t = new lime_test(57, new lime_output_color()); 14 14 15 15 $data = array( … … 29 29 { 30 30 new sfDataSourceArray(array( 31 array('id' => 1, 'name' => 'Fabien'), 31 array('id' => 1, 'name' => 'Fabien'), 32 32 array('id' => 2, 'surname' => 'Stefan'), 33 33 array('id' => 3, 'name' => 'Jonathan'), … … 42 42 { 43 43 new sfDataSourceArray(array( 44 'id' => 1, 'name' => 'Fabien', 44 'id' => 1, 'name' => 'Fabien', 45 45 'id' => 2, 'surname' => 'Stefan', 46 46 'id' => 3, 'name' => 'Jonathan', … … 55 55 { 56 56 new sfDataSourceArray(array( 57 array('id' => 1, 'name' => 'Fabien'), 57 array('id' => 1, 'name' => 'Fabien'), 58 58 'id' => 2, 'surname' => 'Stefan', 59 59 'id' => 3, 'name' => 'Jonathan', … … 273 273 } 274 274 275 276 // associative arrays 277 $t->diag('support for associative arrays'); 278 279 $associative_data = array( 280 'first' => array('id' => 1, 'name' => 'Fabien'), 281 'second' => array('id' => 2, 'name' => 'Francois'), 282 'third' => array('id' => 3, 'name' => 'Jonathan'), 283 ); 284 $s = new sfDataSourceArray($associative_data); 285 try 286 { 287 $s->requireColumn('name'); 288 $t->pass('sfDataSourceArray accepts existing column (name) of an array'); 289 } 290 catch (Exception $e) 291 { 292 $t->fail('sfDataSourceArray accepts existing column (name) of an array'); 293 } 294 try 295 { 296 $s->requireColumn('anyColumn'); 297 $t->fail('sfDataSourceArray does not accept columns that are not in the array'); 298 } 299 catch (LogicException $e) 300 { 301 $t->pass('sfDataSourceArray does not accept columns that are not in the array'); 302 } 303 304 $t->is(array_keys(iterator_to_array($s, true)), range(0, 2), 'sfDataSourceArray implements the SeekableIterator interface'); 305 $t->is(count(iterator_to_array($s)), 3, 'sfDataSourceArray implements the SeekableIterator interface'); 306 307 $s->seek(1); 308 $t->is($s['id'], 2, 'sfDataSourceArray implements the SeekableIterator interface'); 309 $t->is($s['name'], 'Francois', 'sfDataSourceArray implements the SeekableIterator interface'); 310 311 try 312 { 313 $s->seek(30); 314 $t->fail('->seek() throws an "OutOfBoundsException" when the given index is too large'); 315 } 316 catch (OutOfBoundsException $e) 317 { 318 $t->pass('->seek() throws an "OutOfBoundsException" when the given index is too large'); 319 } 320 321 try 322 { 323 $s->seek(-1); 324 $t->fail('->seek() throws an "OutOfBoundsException" when the given index is too small'); 325 } 326 catch (OutOfBoundsException $e) 327 { 328 $t->pass('->seek() throws an "OutOfBoundsException" when the given index is too small'); 329 } 330 $t->is(count($s), 3, 'sfDataSourceArray implements the Countable interface'); 331 332 333 $s = new sfDataSourceArray($associative_data); 334 $s->setLimit(3); 335 $values = array(); 336 foreach ($s as $row) 337 { 338 $values[] = $row['id']; 339 } 340 $t->is($values, range(1,3), '->setLimit() limits the records returned by the iterator'); 341 342 343 $s = new sfDataSourceArray($associative_data); 344 345 $t->is($s['id'], 1, 'sfDataSourceArray implements the ArrayAccess interface'); 346 $t->is($s['name'], 'Fabien', 'sfDataSourceArray implements the ArrayAccess interface'); 347 $t->ok(isset($s['id']), 'sfDataSourceArray implements the ArrayAccess interface'); 348 $t->ok(!isset($s['foobar']), 'sfDataSourceArray implements the ArrayAccess interface'); 349 $s->next(); 350 $t->is($s['id'], 2, 'sfDataSourceArray implements the ArrayAccess interface'); 351 $t->is($s['name'], 'Francois', 'sfDataSourceArray implements the ArrayAccess interface'); 352 353 try 354 { 355 $s['name'] = 'Foobar'; 356 $t->fail('sfDataSourceArray throws a "LogicException" when fields are set using ArrayAccess'); 357 } 358 catch (LogicException $e) 359 { 360 $t->pass('sfDataSourceArray throws a "LogicException" when fields are set using ArrayAccess'); 361 } 362 try 363 { 364 unset($s['name']); 365 $t->fail('sfDataSourceArray throws a "LogicException" when fields are unset using ArrayAccess'); 366 } 367 catch (LogicException $e) 368 { 369 $t->pass('sfDataSourceArray throws a "LogicException" when fields are unset using ArrayAccess'); 370 } 371 372 foreach ($s as $k => $v); 373 374 try 375 { 376 $s['name']; 377 $t->fail('sfDataSourceArray throws an "OutOfBoundsException" when fields are accessed after iterating'); 378 } 379 catch (OutOfBoundsException $e) 380 { 381 $t->pass('sfDataSourceArray throws an "OutOfBoundsException" when fields are accessed after iterating'); 382 } 383 try 384 { 385 isset($s['name']); 386 $t->fail('sfDataSourceArray throws an "OutOfBoundsException" when fields are accessed after iterating'); 387 } 388 catch (OutOfBoundsException $e) 389 { 390 $t->pass('sfDataSourceArray throws an "OutOfBoundsException" when fields are accessed after iterating'); 391 }