Symfony Doctrine entity property with array of objects

Viewed 685

I have a survey form that is mapped to an entity. Within this form, there is a multiple-choice field. Choices/Options are Entity Type.

Survey Form Entity property:

    /**
     * @var App\Entity\Definition[]|ArrayCollection
     *
     * @ORM\Column(type="simple_array")
     */
    protected $dissatisfactionReasons;

What I want to do is save the selected choices without using the relation table (ManyToMany). I simply want to save $dissatisfactionReasons property into the table as an array of IDs. I also defined __toString() method for Definition entity:

/**
     * @return string
     */
    public function __toString()
    {
        return (string)$this->getId();
    }

Using this code I was able to save $dissatisfactionReasons property values as a string of IDs separated by a comma. However, on retrieving survey form Entity $dissatisfactionReasons property contains an array of integers and not converted/mapped back to an array of Definition entities.

Using @ORM\Column(type="array") instead of simple_array works as expected, but type array serializes whole Definition entity data whereas I only want to save IDs

0 Answers
Related