简体   繁体   English

Symfony 2.1 Sonata Admin Bundle OneToMany

[英]Symfony 2.1 Sonata Admin Bundle OneToMany

Let's say I have two entities: 假设我有两个实体:

1. Product 1.产品

/**
 * @ORM\Table()
 * @ORM\Entity
 */
class Product
{
    /*   
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity="Catalog", mappedBy="product")
     */
    public $catalogs;

    public function __construct()
    {
        $this->catalogs = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

2.Catalog 2.Catalog

/**
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Catalog
{
    /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="catalogs") 
     */
    private $product;

    /**
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;
}

My ProductAdmin : 我的ProductAdmin

class ProductAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
        ->add('name')
        ->add('catalogs', 'sonata_type_model')
        ;
    }
}

I can't get catalogs to be working (something like user=>groups association here: http://demo.sonata-project.org/admin/sonata/user/user/create credentials: admin/admin). 我无法使catalogs工作(例如user => groups association: http//demo.sonata-project.org/admin/sonata/user/user/create credentials:admin / admin)。 I only get errors: No entity manager defined for class Doctrine\\Common\\Collections\\ArrayCollection 我只得到错误: No entity manager defined for class Doctrine\\Common\\Collections\\ArrayCollection

Try with multiple option: 尝试使用多个选项:

 protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
        ->add('name')
        ->add('catalogs', 'sonata_type_model', array('multiple' => true)
        ;
    }

You have to add a seperate Admin class for the Catalog Entity. 您必须为Catalog Entity添加单独的Admin类。

You can only use the Catalog if you have an CatalogAdmin same as the ProductAdmin. 如果您的CatalogAdmin与ProductAdmin相同,则只能使用目录。 After that you can use the sonata_type_model or sonata_type_model_list formtypes. 之后,您可以使用sonata_type_model或sonata_type_model_list formtypes。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM