简体   繁体   English

使用EasyAdminBundle中的联接表进行多对多

[英]Many to Many with Join Table in EasyAdminBundle

I'm trying to get a smooth admin interface similar as if when two entities are associated by a many-to-many relationship. 我试图获得一个平滑的管理界面,就像两个实体通过多对多关系关联时一样。 I need the join table to define additional information like rank. 我需要联接表来定义其他信息,例如等级。 I don't want to display the jointable entity on the backend, it should be writeable in the edit-menu of at least one entity. 我不想在后端显示可连接的实体,它应该可在至少一个实体的编辑菜单中写入。 My example: 我的例子:

class Question{
    /**
     * @Assert\NotBlank()
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Options2Questions", mappedBy="question", cascade={"persist"})
     */
    private $optionQuestion;

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

    public function addOptionQuestion(\AppBundle\Entity\Options2Questions $optionQuestion){
        $this->optionQuestion[] = $optionQuestion;
        return $this;
    }

    public function removeOptionQuestion(\AppBundle\Entity\Options2Questions $optionQuestion){
        $this->optionQuestion->removeElement($optionQuestion);
    }

    public function getOptionQuestion(){
        return $this->optionQuestion;
    }
}

class Options{

    /**
     * @Assert\NotBlank()
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Options2Questions", mappedBy="options")
     */
    private $optionQuestion;

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

    public function addOptionQuestion(\AppBundle\Entity\Options2Questions $optionQuestion){
        $this->optionQuestion[] = $optionQuestion;
        return $this;
    }

    public function removeOptionQuestion(\AppBundle\Entity\Options2Questions $optionQuestion)
    {
        $this->optionQuestion->removeElement($optionQuestion);
    }

    public function getOptionQuestion(){
        return $this->optionQuestion;
    }
}

class Options2Questions
{    

    /**
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Options", inversedBy="optionsQuestions")
     * @ORM\JoinColumn(name="options_id", referencedColumnName="id", nullable=false)
     */
    private $options;

    /**
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Question", inversedBy="optionsQuestions")
     * @ORM\JoinColumn(name="question_id", referencedColumnName="id", nullable=false)
     */
    private $question;

    /**
     * @var int
     *
     * @ORM\Column(name="rank", type="integer", nullable=true)
     */
    private $rank;



    /**
     * Set rank
     *
     * @param integer $rank
     *
     * @return Options2Questions
     */
    public function setRank($rank)
    {
        $this->rank = $rank;

        return $this;
    }

    /**
     * Get rank
     *
     * @return int
     */
    public function getRank()
    {
        return $this->rank;
    }

    /**
     * Set options
     *
     * @param \AppBundle\Entity\Options $options
     *
     * @return Options2Questions
     */
    public function setOptions(\AppBundle\Entity\Options $options)
    {
        $this->options = $options;

        return $this;
    }

    /**
     * Get options
     *
     * @return \AppBundle\Entity\Options
     */
    public function getOptions()
    {
        return $this->options;
    }

    /**
     * Set question
     *
     * @param \AppBundle\Entity\Question $question
     *
     * @return Options2Questions
     */
    public function setQuestion(\AppBundle\Entity\Question $question)
    {
        $this->question = $question;

        return $this;
    }

    /**
     * Get question
     *
     * @return \AppBundle\Entity\Question
     */
    public function getQuestion()
    {
        return $this->question;
    }
}

I could not find any documentation on this. 我找不到与此有关的任何文档。 Where should I look specifically? 我应该在哪里看?

  1. First of all you have to create the admin class - service for the all of these three entities. 首先,您必须创建admin类-为所有这三个实体提供服务。 Therefore you will have the next 3 classes: 因此,您将获得以下3个课程:

    a) QuestionAdmin a)QuestionAdmin

    b) OptionAdmin b)OptionAdmin

    c) Options2QuestionsAdmin c)Options2QuestionsAdmin

  2. If you want to remove from you admin services list the Options2QuestionsAdmin - you can add show_in_dashboard: false line to the services.yml: 如果要从管理服务中删除,请列出Options2QuestionsAdmin-您可以将show_in_dashboard: false行添加到services.yml:

     app.admin.options2questions: class: AppBundle\\Options2questionsAdmin arguments: [~, AppBundle\\EntityOptions2questions, SonataAdminBundle:CRUD] tags: - { name: sonata.admin, manager_type: orm, group: 'your_group', label: 'your_label', show_in_dashboard: false } 
  3. In your QuestionAdmin class in formmapper method you can add this: 在formmapper方法中的QuestionAdmin类中,可以添加以下内容:

     class QuestionAdmin extends AbstractAdmin { // ... protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('optionQuestion', 'sonata_type_collection', [ 'required' => true, 'label' => 'option_question', 'by_reference' => false, 'btn_add' => 'add_button', 'type_options' => [ 'delete' => true, ], ], [ 'edit' => 'inline', 'inline' => 'standard', 'sortable' => 'id', 'allow_delete' => true, ]) ; } 

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

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