Changeset 8807
- Timestamp:
- 05/06/08 16:12:28 (1 year ago)
- Files:
-
- branches/1.1/lib/plugins/sfPropelPlugin/data/generator/sfPropelForm/default/template/sfPropelFormGeneratedTemplate.php (modified) (1 diff)
- branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php (modified) (2 diffs)
- branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/generator/sfPropelFormGenerator.class.php (modified) (1 diff)
- branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/validator/sfValidatorPropelUnique.class.php (added)
- branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/apps/frontend/modules/unique (added)
- branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/apps/frontend/modules/unique/actions (added)
- branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/apps/frontend/modules/unique/actions/actions.class.php (added)
- branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/apps/frontend/modules/unique/templates (added)
- branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/apps/frontend/modules/unique/templates/articleSuccess.php (added)
- branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/apps/frontend/modules/unique/templates/categorySuccess.php (added)
- branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/config/schema.xml (modified) (2 diffs)
- branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/lib/model/CategoryPeer.php (modified) (1 diff)
- branches/1.1/lib/plugins/sfPropelPlugin/test/functional/formTest.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/plugins/sfPropelPlugin/data/generator/sfPropelForm/default/template/sfPropelFormGeneratedTemplate.php
r8780 r8807 31 31 )); 32 32 33 <?php if ($uniqueColumns = $this->getUniqueColumnNames()): ?> 34 $this->validatorSchema->setPostValidator( 35 <?php if (count($uniqueColumns) > 1): ?> 36 new sfValidatorAnd(array( 37 <?php foreach ($uniqueColumns as $uniqueColumn): ?> 38 new sfValidatorPropelUnique(array('model' => '<?php echo $this->table->getPhpName() ?>', 'column' => array('<?php echo implode("', '", $uniqueColumn) ?>'))), 39 <?php endforeach; ?> 40 )) 41 <?php else: ?> 42 new sfValidatorPropelUnique(array('model' => '<?php echo $this->table->getPhpName() ?>', 'column' => array('<?php echo implode("', '", $uniqueColumns[0]) ?>'))) 43 <?php endif; ?> 44 ); 45 46 <?php endif; ?> 33 47 $this->widgetSchema->setNameFormat('<?php echo $this->underscore($this->table->getPhpName()) ?>[%s]'); 34 48 branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php
r7394 r8807 53 53 $this->addI18nMethods($script); 54 54 } 55 56 $this->addUniqueColumnNamesMethod($script); 55 57 } 56 58 … … 403 405 } 404 406 } 407 408 protected function addUniqueColumnNamesMethod(&$script) 409 { 410 $unices = array(); 411 foreach ($this->getTable()->getUnices() as $unique) 412 { 413 $unices[] = sprintf("array('%s')", implode("', '", $unique->getColumns())); 414 } 415 416 $unices = implode(', ', $unices); 417 $script .= <<<EOF 418 419 420 static public function getUniqueColumnNames() 421 { 422 return array($unices); 423 } 424 EOF; 425 } 405 426 } branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/generator/sfPropelFormGenerator.class.php
r8805 r8807 457 457 public function getUniqueColumnNames() 458 458 { 459 return call_user_func(array($this->table->getPhpName().'MapBuilder', 'getUniqueColumnNames')); 459 $uniqueColumns = array(); 460 461 foreach (call_user_func(array($this->table->getPhpName().'Peer', 'getUniqueColumnNames')) as $unique) 462 { 463 $uniqueColumn = array(); 464 foreach ($unique as $column) 465 { 466 $uniqueColumn[] = strtolower($this->table->getColumn($column)->getColumnName()); 467 } 468 469 $uniqueColumns[] = $uniqueColumn; 470 } 471 472 return $uniqueColumns; 460 473 } 461 474 branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/config/schema.xml
r8780 r8807 17 17 <reference local="book_id" foreign="id" onDelete="setnull" /> 18 18 </foreign-key> 19 <unique name="unique_title_category"> 20 <unique-column name="title" /> 21 <unique-column name="category_id" /> 22 </unique> 19 23 </table> 20 24 … … 22 26 <column name="id" type="integer" required="true" primaryKey="true" autoincrement="true" /> 23 27 <column name="name" type="varchar" size="255" /> 28 <unique name="unique_name"> 29 <unique-column name="name" /> 30 </unique> 31 <unique name="unique_name1"> 32 <unique-column name="name" /> 33 </unique> 24 34 </table> 25 35 branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/lib/model/CategoryPeer.php
r5103 r8807 10 10 class CategoryPeer extends BaseCategoryPeer 11 11 { 12 static public function getByName($name) 13 { 14 $c = new Criteria(); 15 $c->add(self::NAME, $name); 16 17 return self::doSelectOne($c); 18 } 12 19 } branches/1.1/lib/plugins/sfPropelPlugin/test/functional/formTest.php
r8535 r8807 42 42 $b->test()->is(count($attachments), 1, 'the attachment has been saved in the database'); 43 43 $b->test()->is($attachments[0]->getFile(), $uploadedFile, 'the attachment filename has been saved in the database'); 44 45 // sfValidatorPropelUnique 46 47 // create a category with a unique name 48 $b-> 49 get('/unique/category')-> 50 isRequestParameter('module', 'unique')-> 51 isRequestParameter('action', 'category')-> 52 isStatusCode(200)-> 53 click('submit', array('category' => array('name' => 'foo')))-> 54 isRedirected()-> 55 followRedirect()-> 56 responseContains('ok') 57 ; 58 59 // create another category with the same name 60 // we must have an error 61 $b-> 62 get('/unique/category')-> 63 isRequestParameter('module', 'unique')-> 64 isRequestParameter('action', 'category')-> 65 isStatusCode(200)-> 66 click('submit', array('category' => array('name' => 'foo')))-> 67 checkResponseElement('.error_list li', 'An object with the same "name" already exist.') 68 ; 69 70 // updating the same category again with the same name is allowed 71 $b-> 72 get('/unique/category?category[id]='.CategoryPeer::getByName('foo')->getId())-> 73 isRequestParameter('module', 'unique')-> 74 isRequestParameter('action', 'category')-> 75 isStatusCode(200)-> 76 click('submit')-> 77 isRedirected()-> 78 followRedirect()-> 79 responseContains('ok') 80 ; 81 82 // create an article with a unique title-category_id 83 $b-> 84 get('/unique/article')-> 85 isRequestParameter('module', 'unique')-> 86 isRequestParameter('action', 'article')-> 87 isStatusCode(200)-> 88 click('submit', array('article' => array('title' => 'foo', 'category_id' => 1)))-> 89 isRedirected()-> 90 followRedirect()-> 91 responseContains('ok') 92 ; 93 94 // create another article with the same title but a different category_id 95 $b-> 96 get('/unique/article')-> 97 isRequestParameter('module', 'unique')-> 98 isRequestParameter('action', 'article')-> 99 isStatusCode(200)-> 100 click('submit', array('article' => array('title' => 'foo', 'category_id' => 2)))-> 101 isRedirected()-> 102 followRedirect()-> 103 responseContains('ok') 104 ; 105 106 // create another article with the same title and category_id as the first one 107 // we must have an error 108 $b-> 109 get('/unique/article')-> 110 isRequestParameter('module', 'unique')-> 111 isRequestParameter('action', 'article')-> 112 isStatusCode(200)-> 113 click('submit', array('article' => array('title' => 'foo', 'category_id' => 1)))-> 114 checkResponseElement('.error_list li', 'An object with the same "title, category_id" already exist.') 115 ;

