sonata-project / SonataDoctrinePhpcrAdminBundle

[Abandoned] Symfony Sonata / Integrate Doctrine PHPCR into the SonataAdminBundle
https://sonata-project.org
MIT License
34 stars 93 forks source link

type_collection with multiple types crashes #137

Closed dantleech closed 11 years ago

dantleech commented 11 years ago

If the type_collection field has mixed referrers it will crash. See the MultilangStaticContent admin in the cmf-sandbox to reproduce.

14:53:47    sjopet | I think the problem is that a type_collection requires 
                   | admin_code attribute but the admin that should be used to build
                   | the collection might change based on the type of Document you 
                   | editing                               
dantleech commented 11 years ago

/cc @sjopet

sjopet commented 11 years ago

Ok I found the problem and its a simple fix. This also means we don't have to set the admin_code attribute on sonata_type_collection fields manually any more.

The problem is caused by a naming conflict in the association mappings. The associationMapping of the sonata_type_collection field looks like this:

array(
  'fieldName' => 'menus',
  'referencedBy' => 'weakContent',
  'referringDocument' => 'Symfony\Cmf\Bundle\MenuBundle\Document\MenuNode',
  'cascade' => 1,
  'sourceDocument' => 'Symfony\Cmf\Bundle\ContentBundle\Document\MultilangStaticContent',
  'type' => 'referrers',
  'strategy' => 'weak',
  'declared' => 'Symfony\Cmf\Bundle\ContentBundle\Document\StaticContent' 
);

The admin responsable for the target document is attached to the fieldDescription based on the getTargetEntity function in Sonata/DoctrinePHPCRAdminBundle/Admin/FieldDescription.php

    public function getTargetEntity()
    {
        if (isset($this->associationMapping['targetDocument'])) {
            return $this->associationMapping['targetDocument'];
        }

        return null;
    }

and it seems the key 'targetDocument' is not used anymore. Changing it to 'referringDocument' will fix the problem.

This is a collection based on a referrers relation. I'm not sure what the associationMapping of a referenceOne or referenceMany looks like so could not do the pr yet.

sjopet commented 11 years ago

because targetDocument attribute is used for referenceOne etc just adding a condition for referringDocument should do.

sjopet commented 11 years ago

PR merged. Issue can be closed