Development

Changeset 8807

You must first sign up to be able to contribute.

Changeset 8807

Show
Ignore:
Timestamp:
05/06/08 16:12:28 (1 year ago)
Author:
fabien
Message:

added a sfValidatorPropelUnique validator and added automatic support in propel:build-forms (closes #3337, #3239)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/plugins/sfPropelPlugin/data/generator/sfPropelForm/default/template/sfPropelFormGeneratedTemplate.php

    r8780 r8807  
    3131    )); 
    3232 
     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; ?> 
    3347    $this->widgetSchema->setNameFormat('<?php echo $this->underscore($this->table->getPhpName()) ?>[%s]'); 
    3448 
  • branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php

    r7394 r8807  
    5353      $this->addI18nMethods($script); 
    5454    } 
     55 
     56    $this->addUniqueColumnNamesMethod($script); 
    5557  } 
    5658 
     
    403405    } 
    404406  } 
     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  } 
     424EOF; 
     425  } 
    405426} 
  • branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/generator/sfPropelFormGenerator.class.php

    r8805 r8807  
    457457  public function getUniqueColumnNames() 
    458458  { 
    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; 
    460473  } 
    461474 
  • branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/config/schema.xml

    r8780 r8807  
    1717      <reference local="book_id" foreign="id" onDelete="setnull" /> 
    1818    </foreign-key> 
     19    <unique name="unique_title_category"> 
     20      <unique-column name="title" /> 
     21      <unique-column name="category_id" /> 
     22    </unique> 
    1923  </table> 
    2024 
     
    2226    <column name="id" type="integer" required="true" primaryKey="true" autoincrement="true" /> 
    2327    <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> 
    2434  </table> 
    2535 
  • branches/1.1/lib/plugins/sfPropelPlugin/test/functional/fixtures/lib/model/CategoryPeer.php

    r5103 r8807  
    1010class CategoryPeer extends BaseCategoryPeer 
    1111{ 
     12  static public function getByName($name) 
     13  { 
     14    $c = new Criteria(); 
     15    $c->add(self::NAME, $name); 
     16 
     17    return self::doSelectOne($c); 
     18  } 
    1219} 
  • branches/1.1/lib/plugins/sfPropelPlugin/test/functional/formTest.php

    r8535 r8807  
    4242$b->test()->is(count($attachments), 1, 'the attachment has been saved in the database'); 
    4343$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; 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.