动态关系和SonataAdminBundle

Wou*_*r J 7 doctrine-orm symfony-sonata symfony-2.1 sonata-admin

我生命中第一次使用SonataAdminBundle,我遇到了一些问题.

起初,我有一个PageBundle,它有Page一个Author实体.然后我开始使用SonataAdminBundle并使用它sonata_type_model来很好地显示Authors 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')
    ;
}
Run Code Online (Sandbox Code Playgroud)

但后来我发现了SonataUserBundle.我开始使用它,当我最终使它工作时,我认为使用这个User 实体而不是AuthorPageBundle 中的实体会很好.为了实现这一点,我使用了文档中的 "如何定义与抽象类和接口的关系"的技术.

这有效,但不适用于SonataAdminBundle.看起来它 sonata_type_model不起作用ResolveTargetEntityListener,我无法使它工作.

PagePageBundle中的相关实体代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

Wj\PageBundle\Model\AuthorInterface:

namespace Wj\PageBundle\Model;

interface AuthorInterface
{
    public function getId();
}
Run Code Online (Sandbox Code Playgroud)

UserUserBundle中的实体:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

FormMapper配置是如我张贴以上相同.

如何将ResolveTargetEntityListener技术与SonataAdminBundle及其组合使用sonata_type_model