$this->hasColumn('name', 'string', 255); $this->hasColumn('test_id', 'integer'); } public function setUp() { $this->hasOne('Test', array('local' => 'test_id', 'foreign' => 'id')); } } $column = new sfDoctrineColumn('name', Doctrine::getTable('Test')); $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'));
/** * 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)) : ''; }
/** * 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('\'required\' => false'); if ($column->isForeignKey()) { $columns = $column->getForeignTable()->getColumns(); foreach ($columns as $name => $col) { if (isset($col['primary']) && $col['primary']) { break; } } $options[] = sprintf('\'model\' => $this->getRelatedModelName(\'%s\'), \'column\' => \'%s\'', $column->getRelationKey('alias'), $column->getForeignTable()->getFieldName($name)); } else { if ($column->isPrimaryKey()) { $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->table->getOption('name'), $column->getFieldName()); } else { switch ($column->getDoctrineType()) { case 'boolean': $options[] = "'choices' => array('', 1, 0)"; break; case 'date': $options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDateTime(array('required' => false))"; break; case 'datetime': case 'timestamp': $options[] = "'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59'))"; break; case 'enum': $values = array_combine($column['values'], $column['values']); $options[] = "'choices' => " . $this->arrayExport($values); break; } } } 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(); $default = isset($this->validatorOptions[$type]) ? $this->validatorOptions[$type] : array(); $options = array('required' => 'false'); if ($column->isForeignKey()) { $columns = $column->getForeignTable()->getColumns(); foreach ($columns as $name => $col) { if (isset($col['primary']) && $col['primary']) { break; } } $options['model'] = "\$this->getRelatedModelName( '" . $column->getRelationKey('alias') . "' )"; $options['column'] = "'" . $column->getForeignTable()->getFieldName($name) . "'"; } else { if ($column->isPrimaryKey()) { $options['model'] = "'" . $this->table->getOption('name') . "'"; $options['column'] = "'" . $column->getFieldName() . "'"; } else { if ($type == 'enum') { $values = array_combine($column['values'], $column['values']); $options['choices'] = $this->arrayExport($values); } $options = array_merge($options, $this->config->getFilterValidatorOptions($this->modelName, $column->getName(), $type, $default)); } } $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 . ") " : ''; }
/** * 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 getCreateValidatorOptionsForColumn($column, $model = null) { if (null === $model) { $model = '$this->model'; } $options = array(); if ($column->isForeignKey()) { $options[] = sprintf('\'model\' => Doctrine_Core::getTable(' . $model . ')->getRelation(\'%s\')->getAlias()', $column->getRelationKey('alias')); } else { if ($column->isPrimaryKey()) { $options[] = sprintf('\'pattern\' => \'(.+)\', \'must_match\' => false'); } 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') || $column->getFieldName() == 'created_at' || $column->getFieldName() == 'updated_at') { $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 . ") " : ''; }