1

我试图获得一个流畅的管理界面,就像两个实体通过多对多关系关联一样。我需要连接表来定义附加信息,如排名。我不想在后端显示可连接实体,它应该可以在至少一个实体的编辑菜单中写入。我的例子:

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;
    }
}

我找不到任何关于此的文档。我应该具体看哪里?

4

1 回答 1

-2
  1. 首先,您必须为所有这三个实体创建管理类 - 服务。因此,您将参加接下来的 3 节课:

    a) 问题管理员

    b) 选项管理员

    c) Options2QuestionsAdmin

  2. 如果您想从管理服务列表中删除 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. 在 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,
                        ])
                  ;
              }
    
于 2017-08-04T22:46:03.540 回答