dongyun3805 2014-10-21 09:40 采纳率: 0%
浏览 19
已采纳

Symfony2 n-m与额外字段的关系

I have two tables (test and question) and the middle table (n-m). In this point all works fine. But now, I need to put extra information in the (n-m) table, the order of this question in this test

I need this:

id | test_id | question_id | order
1  |    1    |    1        |  3
2  |    1    |    2        |  2
3  |    1    |    3        |  1
4  |    1    |    4        |  4

All these relationship have made with doctrine annotation...

Test Entity

/**
 * @ORM\ManyToMany(targetEntity="Question", inversedBy="tests")
 */

private $questions;

Question Entity

/**
 * @ORM\ManyToMany(targetEntity="Test", mappedBy="questions"))
 */

private $tests;

Any help will be appreciated

EDIT

Hi again! Thanks a lot to @DonCallisto

my entities at the end:

Test

/**
 * @ORM\OneToMany(targetEntity="RTestQuestion", mappedBy="question")
 */

private $questions;

Question

/**
 * @ORM\OneToMany(targetEntity="RTestQuestion", mappedBy="test"))
 */

private $tests;

My new entity "RTestQuestion"

/**
 * ET\BackendBundle\Entity\RTestQuestion
 * 
 * @ORM\Table(name="rtest_question")
 * @ORM\Entity
 */
class RTestQuestion {

    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Question", inversedBy="questions", cascade={"persist", "remove"})
     */
    private $question;

    /**
     * @ORM\ManyToOne(targetEntity="Test", inversedBy="tests", cascade={"persist", "remove"})
     */
    private $test;

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

I had to make two changes:

  • The properties need the cascade on persist and remove actions (doctrine console show errors without this)

  • And the word "order" are restricted for mysql, and now become a question_order.

And, again, thanks to @DonCallisto!

  • 写回答

1条回答 默认 最新

  • douhan6738 2014-10-21 09:43
    关注

    Split the relationship into a 1-n and m-1 as follows

    Test Entity --- (1 - m) ---> RTestQuestion Entity <--- (m - 1) --- Question

    So your code will be

    Test Entity

    /**
     * @ORM\OneToMany(targetEntity="RTestQuestion", inversedBy="question")
     */
    
    private $questions;
    

    Question Entity

    /**
     * @ORM\OneToMany(targetEntity="RTestQuestion", mappedBy="test"))
     */
    
    private $tests;
    

    RTestQuestion Entity

    /**
     * @ORM\ManyToOne(targetEntity="Question", mappedBy="questions"))
     */
    
    private $question;
    
    /**
     * @ORM\ManyToOne(targetEntity="Test", mappedBy="tests"))
     */
    
    private $test;
    
    /**
     * EXTRA ATTRIBUTES HERE
     */
    

    Remember that an association with extra fields isn't an association anymore but a new entity!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器