|
@@ -12,32 +12,21 @@
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Guesser;
|
|
|
|
|
|
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
|
|
|
-use Symfony\Bridge\Doctrine\RegistryInterface;
|
|
|
use Symfony\Component\Form\Guess\Guess;
|
|
|
use Symfony\Component\Form\Guess\TypeGuess;
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
|
|
-use Doctrine\ORM\Mapping\MappingException;
|
|
|
+use Sonata\AdminBundle\Model\ModelManagerInterface;
|
|
|
|
|
|
-class FilterTypeGuesser implements TypeGuesserInterface
|
|
|
+class FilterTypeGuesser extends AbstractTypeGuesser
|
|
|
{
|
|
|
- protected $registry;
|
|
|
-
|
|
|
- private $cache;
|
|
|
-
|
|
|
- public function __construct(RegistryInterface $registry)
|
|
|
- {
|
|
|
- $this->registry = $registry;
|
|
|
- $this->cache = array();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* @param string $class
|
|
|
* @param string $property
|
|
|
* @return TypeGuess
|
|
|
*/
|
|
|
- public function guessType($class, $property)
|
|
|
+ public function guessType($class, $property, ModelManagerInterface $modelManager)
|
|
|
{
|
|
|
- if (!$ret = $this->getMetadata($class)) {
|
|
|
+ if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -47,11 +36,12 @@ class FilterTypeGuesser implements TypeGuesserInterface
|
|
|
'options' => array(),
|
|
|
);
|
|
|
|
|
|
- list($metadata, $name) = $ret;
|
|
|
+ list($metadata, $propertyName, $parentAssociationMappings) = $ret;
|
|
|
+
|
|
|
+ $options['parent_association_mappings'] = $parentAssociationMappings;
|
|
|
|
|
|
- if ($metadata->hasAssociation($property)) {
|
|
|
- $multiple = $metadata->isCollectionValuedAssociation($property);
|
|
|
- $mapping = $metadata->getAssociationMapping($property);
|
|
|
+ if ($metadata->hasAssociation($propertyName)) {
|
|
|
+ $mapping = $metadata->getAssociationMapping($propertyName);
|
|
|
|
|
|
switch ($mapping['type']) {
|
|
|
case ClassMetadataInfo::ONE_TO_ONE:
|
|
@@ -66,6 +56,7 @@ class FilterTypeGuesser implements TypeGuesserInterface
|
|
|
$options['field_options'] = array(
|
|
|
'class' => $mapping['targetEntity']
|
|
|
);
|
|
|
+
|
|
|
$options['field_name'] = $mapping['fieldName'];
|
|
|
$options['mapping_type'] = $mapping['type'];
|
|
|
|
|
@@ -73,9 +64,9 @@ class FilterTypeGuesser implements TypeGuesserInterface
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $options['field_name'] = $metadata->fieldMappings[$property]['fieldName'];
|
|
|
+ $options['field_name'] = $metadata->fieldMappings[$propertyName]['fieldName'];
|
|
|
|
|
|
- switch ($metadata->getTypeOfField($property)) {
|
|
|
+ switch ($metadata->getTypeOfField($propertyName)) {
|
|
|
case 'boolean':
|
|
|
$options['field_type'] = 'sonata_type_boolean';
|
|
|
$options['field_options'] = array();
|
|
@@ -110,20 +101,4 @@ class FilterTypeGuesser implements TypeGuesserInterface
|
|
|
return new TypeGuess('doctrine_orm_string', $options, Guess::LOW_CONFIDENCE);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- protected function getMetadata($class)
|
|
|
- {
|
|
|
- if (array_key_exists($class, $this->cache)) {
|
|
|
- return $this->cache[$class];
|
|
|
- }
|
|
|
-
|
|
|
- $this->cache[$class] = null;
|
|
|
- foreach ($this->registry->getEntityManagers() as $name => $em) {
|
|
|
- try {
|
|
|
- return $this->cache[$class] = array($em->getClassMetadata($class), $name);
|
|
|
- } catch (MappingException $e) {
|
|
|
- // not an entity or mapped super class
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|