Symofny2 n-m与外场的关系 [英] Symofny2 n-m relationship with extra field

查看:150
本文介绍了Symofny2 n-m与外场的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表(测试和问题)和中间表(n-m)。在这一点上,一切工作正常。
但现在,我需要在(nm)表中添加额外的信息,这个测试中的这个问题的顺序



我需要这个:

  id | test_id | question_id |订单
1 | 1 | 1 | 3
2 | 1 | 2 | 2
3 | 1 | 3 | 1
4 | 1 | 4 | 4

所有这些关系都与原则注释...



测试实体

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

private $ questions;

问题实体

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

private $ tests;

任何帮助将不胜感激



<强>编辑



再次嗨!
非常感谢@DonCallisto



我的实体结束:



测试

  / ** 
* @ ORM\OneToMany(targetEntity =RTestQuestion,mappedBy = 问题)
* /

private $ questions;

问题

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

private $ tests;

我的新实体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 =AUTO)
* /
private $ id;

/ **
* @ ORM\ManyToOne(targetEntity =Question,inversedBy =questions,cascade = {persist,remove})
* /
私人$问题;

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

/ **
* @var整数$ order
*
* @ ORM\Column(name =question_order,type =integer,可空= true)
* /
private $ question_order;

我不得不进行两个更改:




  • 属性需要对持久化和删除操作进行级联(doctrine控制台显示错误没有这个)


  • order对于mysql是有限制的,现在成为一个question_order。




再次感谢@DonCallisto!

解决方案

将关系拆分为1-n和m-1,如下所示



测试实体---(1 - m)---> RTestQuestion实体<---(m - 1)---问题



所以你的代码将是



测试实体

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

private $ questions;

问题实体

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

private $ tests;

RTestQuestion实体

 code> / ** 
* @ ORM\ManyToOne(targetEntity =Question,mappedBy =questions))
* /

private $ question;

/ **
* @ ORM\ManyToOne(targetEntity =Test,mappedBy =tests))
* /

private $试验;

/ **
* EXTRA ATTRIBUTES HERE
* /

记住,与额外字段的关联不再是一个关联,而是一个新的实体!


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!

解决方案

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!

这篇关于Symofny2 n-m与外场的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆