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 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制