added a Propel Type guesser
This commit is contained in:
parent
7e4c15261d
commit
8016d1156a
126
Form/PropelTypeGuesser.php
Normal file
126
Form/PropelTypeGuesser.php
Normal file
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
|
||||
namespace Propel\PropelBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\FormTypeGuesserInterface;
|
||||
use Symfony\Component\Form\Guess\Guess;
|
||||
use Symfony\Component\Form\Guess\TypeGuess;
|
||||
use Symfony\Component\Form\Guess\ValueGuess;
|
||||
|
||||
/**
|
||||
* Propel Type guesser.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class PropelTypeGuesser implements FormTypeGuesserInterface
|
||||
{
|
||||
private $cache = array();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function guessType($class, $property)
|
||||
{
|
||||
if (!$table = $this->getTable($class)) {
|
||||
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
|
||||
}
|
||||
|
||||
foreach ($table->getRelations() as $relation) {
|
||||
if (in_array($relation->getType(), array(\RelationMap::MANY_TO_ONE, \RelationMap::ONE_TO_MANY))) {
|
||||
if ($property == $relation->getForeignTable()->getName()) {
|
||||
return new TypeGuess('model', array(
|
||||
'class' => $class,
|
||||
'multiple' => \RelationMap::MANY_TO_ONE === $relation->getType() ? false : true,
|
||||
), Guess::HIGH_CONFIDENCE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$column = $this->getColumn($class, $property)) {
|
||||
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
|
||||
}
|
||||
|
||||
switch ($column->getType()) {
|
||||
case \PropelColumnTypes::BOOLEAN:
|
||||
case \PropelColumnTypes::BOOLEAN_EMU:
|
||||
return new TypeGuess('checkbox', array(), Guess::HIGH_CONFIDENCE);
|
||||
case \PropelColumnTypes::TIMESTAMP:
|
||||
return new TypeGuess('datetime', array(), Guess::HIGH_CONFIDENCE);
|
||||
case \PropelColumnTypes::DATE:
|
||||
return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE);
|
||||
case \PropelColumnTypes::TIME:
|
||||
return new TypeGuess('time', array(), Guess::HIGH_CONFIDENCE);
|
||||
case \PropelColumnTypes::FLOAT:
|
||||
case \PropelColumnTypes::REAL:
|
||||
case \PropelColumnTypes::DOUBLE:
|
||||
case \PropelColumnTypes::DECIMAL:
|
||||
return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
|
||||
case \PropelColumnTypes::TINYINT:
|
||||
case \PropelColumnTypes::SMALLINT:
|
||||
case \PropelColumnTypes::INTEGER:
|
||||
case \PropelColumnTypes::BIGINT:
|
||||
case \PropelColumnTypes::NUMERIC:
|
||||
return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
|
||||
case \PropelColumnTypes::CHAR:
|
||||
case \PropelColumnTypes::VARCHAR:
|
||||
return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE);
|
||||
case \PropelColumnTypes::LONGVARCHAR:
|
||||
return new TypeGuess('textarea', array(), Guess::MEDIUM_CONFIDENCE);
|
||||
default:
|
||||
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function guessRequired($class, $property)
|
||||
{
|
||||
if ($column = $this->getColumn($class, $property)) {
|
||||
return new ValueGuess($column->isNotNull(), Guess::HIGH_CONFIDENCE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function guessMaxLength($class, $property)
|
||||
{
|
||||
if (($column = $this->getColumn($class, $property)) && $column->isText()) {
|
||||
return new ValueGuess($column->getSize(), Guess::HIGH_CONFIDENCE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function guessMinLength($class, $property)
|
||||
{
|
||||
}
|
||||
|
||||
protected function getTable($class)
|
||||
{
|
||||
if (isset($this->cache[$class])) {
|
||||
return $this->cache[$class];
|
||||
}
|
||||
|
||||
if (class_exists($queryClass = $class.'Query')) {
|
||||
$query = new $queryClass();
|
||||
|
||||
return $this->cache[$class] = $query->getTableMap();
|
||||
}
|
||||
}
|
||||
|
||||
protected function getColumn($class, $property)
|
||||
{
|
||||
if (isset($this->cache[$class.'::'.$property])) {
|
||||
return $this->cache[$class.'::'.$property];
|
||||
}
|
||||
|
||||
$table = $this->getTable($class);
|
||||
|
||||
if ($table && $table->hasColumn($property)) {
|
||||
return $this->cache[$class.'::'.$property] = $table->getColumn($property);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@
|
|||
<parameter key="propel.build_properties.class">Propel\PropelBundle\DependencyInjection\Properties</parameter>
|
||||
<parameter key="propel.form.type.model.class">Propel\PropelBundle\Form\Type\ModelType</parameter>
|
||||
<parameter key="propel.twig.extension.syntax.class">Propel\PropelBundle\Twig\Extension\SyntaxExtension</parameter>
|
||||
<parameter key="form.type_guesser.propel.class">Propel\PropelBundle\Form\PropelTypeGuesser</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
|
@ -38,5 +39,9 @@
|
|||
<service id="propel.twig.extension.syntax" class="%propel.twig.extension.syntax.class%">
|
||||
<tag name="twig.extension" />
|
||||
</service>
|
||||
|
||||
<service id="form.type_guesser.propel" class="%form.type_guesser.propel.class%">
|
||||
<tag name="form.type_guesser" />
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
||||
|
|
Loading…
Reference in a new issue