Exemplo n.º 1
0
$t->is($column->getName(), 'name');
$t->is($column->getFieldName(), 'name');
$t->is($column->getPhpName(), 'name');
$t->is($column->isNotNull(), true);
$column = new sfDoctrineColumn('test', Doctrine::getTable('Test'));
$t->is($column->getName(), 'test');
$t->is($column->getFieldName(), 'TEST');
$t->is($column->getPhpName(), 'TEST');
$t->is($column->getDoctrineType(), 'string');
$t->is($column->getType(), 'VARCHAR');
$t->is($column->getLength(), 255);
$t->is($column->getSize(), 255);
$t->is($column->hasDefinitionKey('length'), true);
$t->is($column->getDefinitionKey('type'), 'string');
$t->is($column->isNotNull(), false);
// Is not null and has definition key
$column = new sfDoctrineColumn('email', Doctrine::getTable('Test'));
$t->is($column->isNotNull(), true);
$t->is($column->hasDefinitionKey('email'), true);
$t->is($column->getDefinitionKey('email'), true);
// Is primary key
$column = new sfDoctrineColumn('id', Doctrine::getTable('Test'));
$t->is($column->isPrimaryKey(), true);
// Relation/foreign key functions
$column = new sfDoctrineColumn('test_id', Doctrine::getTable('TestRelation'));
$t->is($column->isForeignKey(), true);
$t->is($column->getForeignClassName(), 'Test');
$t->is($column->getForeignTable()->getOption('name'), 'Test');
$t->is($column->getTable()->getOption('name'), 'TestRelation');
// Array access
$t->is($column['type'], 'integer');
 /**
  * Returns a PHP string representing options to pass to a validator for a given column.
  *
  * @param sfDoctrineColumn $column
  * @return string    The options to pass to the validator as a PHP string
  */
 public function getValidatorOptionsForColumn($column)
 {
     $options = array();
     $isForeignKey = $column->isForeignKey();
     $requiresEm = $isForeignKey || $column->isPrimaryKey();
     if ($isForeignKey) {
         $options[] = sprintf('\'model\' => \'%s\'', $column->getForeignMetadata()->name);
     } else {
         if ($column->isPrimaryKey()) {
             $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->modelName, $column->getName());
         } else {
             switch ($column->getDoctrineType()) {
                 case 'string':
                     if ($column['length']) {
                         $options[] = sprintf('\'max_length\' => %s', $column['length']);
                     }
                     if (isset($column['minlength'])) {
                         $options[] = sprintf('\'min_length\' => %s', $column['minlength']);
                     }
                     if (isset($column['regexp'])) {
                         $options[] = sprintf('\'pattern\' => \'%s\'', $column['regexp']);
                     }
                     break;
                 case 'enum':
                     $values = array_combine($column['values'], $column['values']);
                     $options[] = "'choices' => " . str_replace("\n", '', $this->arrayExport($values));
                     break;
             }
         }
     }
     // If notnull = false, is a primary or the column has a default value then
     // make the widget not required
     if (!$column->isNotNull() || $column->isPrimaryKey() || $column->hasDefinitionKey('default')) {
         $options[] = '\'required\' => false';
     }
     return ($requiresEm ? '$this->em, ' : '') . (count($options) ? sprintf('array(%s)', implode(', ', $options)) : '');
 }
 /**
  * Returns a PHP string representing options to pass to a validator for a given column.
  *
  * @param sfDoctrineColumn $column
  * @return string    The options to pass to the validator as a PHP string
  */
 public function getValidatorOptionsForColumn($column)
 {
     $options = array();
     if ($column->isForeignKey()) {
         $options[] = sprintf('\'model\' => $this->getRelatedModelName(\'%s\')', $column->getRelationKey('alias'));
     } else {
         if ($column->isPrimaryKey()) {
             $options[] = sprintf('\'choices\' => array($this->getObject()->get(\'%s\')), \'empty_value\' => $this->getObject()->get(\'%1$s\')', $column->getFieldName());
         } else {
             switch ($column->getDoctrineType()) {
                 case 'string':
                     if ($column['length']) {
                         $options[] = sprintf('\'max_length\' => %s', $column['length']);
                     }
                     if (isset($column['minlength'])) {
                         $options[] = sprintf('\'min_length\' => %s', $column['minlength']);
                     }
                     if (isset($column['regexp'])) {
                         $options[] = sprintf('\'pattern\' => \'%s\'', $column['regexp']);
                     }
                     break;
                 case 'enum':
                     $options[] = '\'choices\' => ' . $this->arrayExport($column['values']);
                     break;
             }
         }
     }
     // If notnull = false, is a primary or the column has a default value then
     // make the widget not required
     if (!$column->isNotNull() || $column->isPrimaryKey() || $column->hasDefinitionKey('default')) {
         $options[] = '\'required\' => false';
     }
     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
 }
 /**
  * Get validator options for column
  * 
  * @param   sfDoctrineColumn    $column
  * @param   integer             $indent Indentation value [optional]
  * @return  string  The options to pass to the validator as a PHP string
  */
 public function getValidatorOptionsForColumn($column, $indent = 0)
 {
     $type = $column->getDoctrineType();
     $options = array();
     if ($column->isForeignKey()) {
         $options['model'] = "\$this->getRelatedModelName( '" . $column->getRelationKey('alias') . "' )";
     } else {
         if ($column->isPrimaryKey()) {
             $options['choices'] = "array( \$this->getObject()->get( '" . $column->getFieldName() . "' ) )";
             $options['empty_value'] = "\$this->getObject()->get( '" . $column->getFieldName() . "' )";
         } else {
             if ($type == 'string') {
                 if ($column['length']) {
                     $options['max_length'] = $column['length'];
                 }
                 if (isset($column['minlength'])) {
                     $options['min_length'] = $column['minlength'];
                 }
                 if (isset($column['regexp'])) {
                     $options['pattern'] = "'" . $column['regexp'] . "'";
                 }
             } else {
                 if ($type == 'enum') {
                     $options['choices'] = $this->arrayExport($column['values']);
                 }
             }
         }
     }
     if (!$column->isNotNull() || $column->isPrimaryKey() || $column->hasDefinitionKey('default')) {
         $options['required'] = 'false';
         if (!$column->isNotNull() && !$column->isPrimaryKey()) {
             $options['empty_value'] = 'null';
         }
     }
     $options = array_merge($options, $this->config->getFormValidatorOptions($this->modelName, $column->getName(), $type, array()));
     $out = array();
     $ni = str_repeat(' ', $indent);
     foreach ($options as $k => $v) {
         if (!is_null($v)) {
             $out[] = $ni . "    '" . $k . "' => " . $v;
         }
     }
     return count($out) ? " array(\n" . implode(",\n", $out) . "\n" . $ni . ") " : '';
 }