Dynamic Relationships and SonataAdminBundle

I am using SonataAdminBundle for the first time in my life, and I have some problems with it.

First I had a PageBundle which has a Page and Author object. Then I started using SonataAdminBundle and perfectly used sonata_type_model display Author in Page :

 // ... protected function configureFormFields(FormMapper $mapper) { $mapper ->add('title') ->add('slug', null, array('required' => false)) ->add('published', null, array( 'label' => 'publish', 'required' => false, )) ->add('author', 'sonata_type_model') ->add('content') ; } 

But then I discovered SonataUserBundle. I started using it, and when I finally started working, I thought it would be nice to use this User object rather than the Author object in the PageBundle. To make this possible, I used the technique described in the documentation on How to Define Relationships with Abstract Classes and Interfaces .

This worked, but not with the SonataAdminBundle. It seems that sonata_type_model does not work with ResolveTargetEntityListener , and I cannot make it work.

The corresponding Page object code in PageBundle:

 namespace Wj\PageBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table * @ORM\Entity * @ORM\HasLifeCycleCallbacks */ class Page { /** * @var integer $authorId * * @ORM\ManyToOne(targetEntity="Wj\PageBundle\Model\AuthorInterface") */ private $author; } 

Wj\PageBundle\Model\AuthorInterface :

 namespace Wj\PageBundle\Model; interface AuthorInterface { public function getId(); } 

And the User object in the UserBundle:

 namespace Wj\UserBundle\Entity; use Sonata\UserBundle\Entity\BaseUser; use Wj\PageBundle\Model\AuthorInterface; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="fos_user") */ class User extends BaseUser implements AuthorInterface { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; public function __toString() { return $this->getFirstName().' '.$this->getLastName(); } } 

The configuration of FormMapper is the same as I posted above.

How to use the ResolveTargetEntityListener method in combination with SonataAdminBundle and its sonata_type_model ?

+7
source share

All Articles