sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Admin : configureSideMenu & addChild -> Class does not exist #1943

Closed Jolg42 closed 10 years ago

Jolg42 commented 10 years ago

Hi,

I need help, I'm stuck ( ping @rande )

I want to do a side-menu like this screenshot 2014-02-04 11 39 33

I tried a lot of things and looked at the different bundles of Sonata (News Page Ecommerce) to find something, but i'm really stuck.... (Someone want a beer ?)

Thanks for your help :)

I have 2 Entities Fundraising and FundraisingEntities (and 2 Admin)


This is my code :

Entity/Fundraising.php

.....

    /**
     * @ORM\OneToMany(targetEntity="FundraisingEntity", mappedBy="idFundraising", cascade={"persist"}, orphanRemoval=true)
     */
    protected $fundraisings;

    /**
     * @param mixed $fundraisings
     */
    public function setFundraisings($fundraisings)
    {
        $this->fundraisings = $fundraisings;
    }

    /**
     * @return mixed
     */
    public function getFundraisings()
    {
        return $this->fundraisings;
    }

    public function __construct()
    {
        $this->fundraisingEntities = new ArrayCollection();
    }

    /**
     * @ORM\OneToMany(targetEntity="FundraisingEntity", mappedBy="idFundraising", cascade={ "persist"}, orphanRemoval=true)
     */
    protected $fundraisingEntities;

    public function getFundraisingEntities()
    {
        return $this->fundraisingEntities;
    }

    public function setFundraisingEntities($fundraisingEntities)
    {
        $this->fundraisingEntities = $fundraisingEntities;
        return $this;
    }

    public function addFundraisingEntitie(\Sonata\Bundle\DemoBundle\Entity\FundraisingEntity $fundraisingEntity = null)
    {
        $this->addFundraisingEntity($fundraisingEntity);
    }
    public function addFundraisingEntity(\Sonata\Bundle\DemoBundle\Entity\FundraisingEntity $fundraisingEntity = null)
    {
        $fundraisingEntity->setIdFundraising($this);

        $this->fundraisingEntities->add($fundraisingEntity);
    }

    public function removeFundraisingEntitie(\Sonata\Bundle\DemoBundle\Entity\FundraisingEntity $fundraisingEntity = null)
    {
        $this->removeFundraisingEntity($fundraisingEntity);
    }
    public function removeFundraisingEntity(\Sonata\Bundle\DemoBundle\Entity\FundraisingEntity $fundraisingEntity = null)
    {
            $this->fundraisingEntities->removeElement($fundraisingEntity);
    }

Entity/FundraisingEntity.php

class FundraisingEntity
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Fundraising", inversedBy="fundraisings")
     * @ORM\JoinColumn(name="idFundraising", referencedColumnName="id", nullable=false)
     */
    protected $idFundraising;
....

Admin/FundraisingAdmin.php

    /**
     * {@inheritdoc}
     *
     */
    protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {
        if (!$childAdmin && !in_array($action, array('edit'))) {
            return;
        }

        $admin = $this->isChild() ? $this->getParent() : $this;
        $id = $admin->getRequest()->get('id');

        $menu->addChild(
            "View",
            array('uri' => $admin->generateUrl('edit', array('id' => $id)))
        );

        $menu->addChild(
            "fundraisingentity",
            array('uri' => $admin->generateUrl('sonata.demo.admin.fundraisingentity.list', array('id' => $id)))
        );

    }

I think the $parentAssociationMapping is correct ?

Admin/FundraisingEntityAdmin.php

class FundraisingEntityAdmin extends Admin
{

    protected $parentAssociationMapping = 'Fundraising';

......

I added the addChild in the service

admin.xml

        <service id="sonata.demo.admin.fundraising" class="Sonata\Bundle\DemoBundle\Admin\FundraisingAdmin">
            <tag name="sonata.admin" manager_type="orm" audit="false" group="RudeList" label="Fundraisings" />

            <argument />
            <argument>Sonata\Bundle\DemoBundle\Entity\Fundraising</argument>
            <argument />

            <call method="addChild">
                <argument type="service" id="sonata.demo.admin.fundraisingentity" />
            </call>

        </service>
        <service id="sonata.demo.admin.fundraisingentity" class="Sonata\Bundle\DemoBundle\Admin\FundraisingEntityAdmin">
            <tag name="sonata.admin" manager_type="orm" audit="false" group="RudeList" label="Fundraisings Entities" />

            <argument />
            <argument>Sonata\Bundle\DemoBundle\Entity\FundraisingEntity</argument>
            <argument />

        </service>

Stack Trace (Plain Text)

[1] ReflectionException: Class  does not exist
    at n/a
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/doctrine/common/lib/Doctrine/Common/Persistence/AbstractManagerRegistry.php line 199

    at ReflectionClass->__construct('')
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/doctrine/common/lib/Doctrine/Common/Persistence/AbstractManagerRegistry.php line 199

    at Doctrine\Common\Persistence\AbstractManagerRegistry->getManagerForClass(null)
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php line 211

    at Sonata\DoctrineORMAdminBundle\Model\ModelManager->getEntityManager(null)
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php line 50

    at Sonata\DoctrineORMAdminBundle\Model\ModelManager->getMetadata(null)
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php line 287

    at Sonata\DoctrineORMAdminBundle\Model\ModelManager->getIdentifierFieldNames(null)
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php line 179

    at Sonata\DoctrineORMAdminBundle\Model\ModelManager->find(null, '1')
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/DataTransformer/ModelToIdTransformer.php line 47

    at Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer->reverseTransform('1')
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 1044

    at Symfony\Component\Form\Form->normToModel('1')
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 631

    at Symfony\Component\Form\Form->submit('1', true)
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 560

    at Symfony\Component\Form\Form->submit(array('value' => '1'), true)
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 560

    at Symfony\Component\Form\Form->submit(array('_sort_order' => 'ASC', '_sort_by' => object(FieldDescription), '_page' => '1', '_per_page' => '25', 'Fundraising' => array('value' => '1')))
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 667

    at Symfony\Component\Form\Form->bind(array('_sort_order' => 'ASC', '_sort_by' => object(FieldDescription), '_page' => '1', '_per_page' => '25', 'Fundraising' => array('value' => '1')))
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Datagrid/Datagrid.php line 113

    at Sonata\AdminBundle\Datagrid\Datagrid->buildPager()
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Datagrid/Datagrid.php line 243

    at Sonata\AdminBundle\Datagrid\Datagrid->getForm()
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Controller/CRUDController.php line 175

    at Sonata\AdminBundle\Controller\CRUDController->listAction()
        in  line 

    at call_user_func_array(array(object(CRUDController), 'listAction'), array())
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/app/bootstrap.php.cache line 2841

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(SiteRequest), '1')
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/app/bootstrap.php.cache line 2815

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(SiteRequest), '1', true)
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/app/bootstrap.php.cache line 2944

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(SiteRequest), '1', true)
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/app/bootstrap.php.cache line 2245

    at Symfony\Component\HttpKernel\Kernel->handle(object(SiteRequest))
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/web/bootstrap.php line 70

    at sonata_handle('dev', true)
        in /Users/j42/Sites/RudeList/sonata-sandbox-current/web/app_dev.php line 27
Jolg42 commented 10 years ago

It's working now,

I have removed

protected $parentAssociationMapping = 'Fundraising'; 

I can share my code if someone is interested ;)

webdevilopers commented 10 years ago

The $parentAssociationMapping value has to link to the name of your mapped column to the parent, NOT the parent Entity's name. If FundraisingEntity is your child I guess it should be:

protected $parentAssociationMapping = 'idFundraising';

But if this is a nested example and the child and parent are both the same kind of Entities resp. coming from the same table you should use table inheritance in Doctrine.