Thursday 15 January 2015

php - Symofny2 n-m relationship with extra field -



php - Symofny2 n-m relationship with extra field -

i have 2 tables (test , question) , middle table (n-m). in point works fine. now, need set info in (n-m) table, order of question in 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 doctrine annotation...

test entity

/** * @orm\manytomany(targetentity="question", inversedby="tests") */ private $questions;

question entity

/** * @orm\manytomany(targetentity="test", mappedby="questions")) */ private $tests;

any help appreciated

edit

hi again! lot @doncallisto

my entities @ 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 create 2 changes:

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

and word "order" restricted mysql, , become question_order.

and, again, @doncallisto!

split relationship 1-n , m-1 follows

test entity --- (1 - m) ---> rtestquestion entity <--- (m - 1) --- question

so code 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; /** * attributes here */

remember association fields isn't association anymore new entity!

php symfony2 doctrine

No comments:

Post a Comment