I have a mapped superclass AbstractQuestion
with 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
and DropdownQuestion
inherit from this superclass.
/**
* Class SimpleQuestion.
* @ORM\Entity()
*/
class SimpleQuestion extends AbstractQuestion
I want to modify an existing SimpleQuestion
and make it a DropdownQuestion
.
When saving a question, I deserialise and merge the question, which contains an ID and the 'dtype' and other properties.
$dquestion = $this->serial->fromJson($request->getContent(), AbstractQuestion::class);
$question = $this->em->merge($dquestion);
$this->em->flush();
So I submit something like:
{ id: 12, dtype: 'dropdown', 'text': 'What is my favourite animal?'}
After the deserialisation, $dquestion
is a DropdownQuestion
object as I desired, but after the merge $question
is a SimpleQuestion
object as it was in the database previously, so any unique properties of DropdownQuestion are lost and the question is saved as a SimpleQuestion. Is there any way to work around this?