Skip to content

Commit

Permalink
[DoctrineBridge] fixed field guesser
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jun 8, 2011
1 parent 6881f01 commit 2d91183
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php
Expand Up @@ -34,15 +34,17 @@ public function __construct(RegistryInterface $registry)
*/
public function guessType($class, $property)
{
if (!$metadata = $this->getMetadata($class)) {
if (!$ret = $this->getMetadata($class)) {
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
}

list($metadata, $name) = $ret;

if ($metadata->hasAssociation($property)) {
$multiple = $metadata->isCollectionValuedAssociation($property);
$mapping = $metadata->getAssociationMapping($property);

return new TypeGuess('entity', array('em' => $this->em, 'class' => $mapping['targetEntity'], 'multiple' => $multiple), Guess::HIGH_CONFIDENCE);
return new TypeGuess('entity', array('em' => $name, 'class' => $mapping['targetEntity'], 'multiple' => $multiple), Guess::HIGH_CONFIDENCE);
}

switch ($metadata->getTypeOfField($property))
Expand Down Expand Up @@ -80,8 +82,9 @@ public function guessType($class, $property)
*/
public function guessRequired($class, $property)
{
if ($metadata = $this->getMetadata($class) && $metadata->hasField($property)) {
if (!$metadata->isNullable($property)) {
$ret = $this->getMetadata($class);
if ($ret && $ret[0]->hasField($property)) {
if (!$ret[0]->isNullable($property)) {
return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
}

Expand All @@ -94,8 +97,9 @@ public function guessRequired($class, $property)
*/
public function guessMaxLength($class, $property)
{
if ($metadata = $this->getMetadata($class) && !$metadata->hasAssociation($property)) {
$mapping = $metadata->getFieldMapping($property);
$ret = $this->getMetadata($class);
if ($ret && !$ret[0]->hasAssociation($property)) {
$mapping = $ret[0]->getFieldMapping($property);

if (isset($mapping['length'])) {
return new ValueGuess($mapping['length'], Guess::HIGH_CONFIDENCE);
Expand All @@ -110,21 +114,16 @@ public function guessMinLength($class, $property)
{
}

/**
* Returns whether Doctrine 2 metadata exists for that class
*
* @return Boolean
*/
protected function getMetadata($class)
{
if (array_key_exists($class, $this->cache)) {
return $this->cache[$class];
}

$this->cache[$class] = null;
foreach ($this->registry->getEntityManagers() as $em) {
foreach ($this->registry->getEntityManagers() as $name => $em) {
if ($em->getConfiguration()->getMetadataDriverImpl()->isTransient($class)) {
return $this->cache[$class] = $em->getClassMetadata($class);
return $this->cache[$class] = array($em->getClassMetadata($class), $name);
}
}
}
Expand Down

0 comments on commit 2d91183

Please sign in to comment.