symfony - Use Doctrine to merge entity with different subclass -
i have mapped superclass abstractquestion single-table-inheritance.
/** * @orm\entity * @orm\mappedsuperclass * @orm\table(name="question") * @orm\inheritancetype("single_table") * @orm\discriminatorcolumn(name="dtype", type="string") * @orm\discriminatormap({ * "simple": "simplequestion", * "dropdown": "dropdownquestion" * }) */ abstract class abstractquestion simplequestion , dropdownquestion inherit superclass.
/** * class simplequestion. * @orm\entity() */ class simplequestion extends abstractquestion i want modify existing simplequestion , make dropdownquestion.
when saving question, deserialise , merge question, contains id , 'dtype' , other properties.
$dquestion = $this->serial->fromjson($request->getcontent(), abstractquestion::class); $question = $this->em->merge($dquestion); $this->em->flush(); so submit like:
{ id: 12, dtype: 'dropdown', 'text': 'what favourite animal?'} after deserialisation, $dquestion dropdownquestion object desired, after merge $question simplequestion object in database previously, unique properties of dropdownquestion lost , question saved simplequestion. there way work around this?
you first have delete existing record (simplequestion) , insert new record (dropdownquestion). type casting not supported in doctrine 2.
note.
can change discriminator column pure sql query, is absolutely not recommended , sure give problems...
Comments
Post a Comment