N
N
nepster-web2018-02-02 15:26:54
Doctrine ORM
nepster-web, 2018-02-02 15:26:54

How to store collection object data in doctrine2 custom type?

In general, there is an entity, it contains a collection object with a custom type. The problem is that when updating, convertToDatabaseValue does not work. As I understand it, the doctrine does not understand that the collection object has changed.

class Record
    {
    ...
        /**
         * @ORM\Column(type="ContentContext_RecordTranslationCollection")
         * @var RecordTranslationCollection
         */
        private $translations;
    
    ...
        public function __construct   ( )
        {
            $this->translations = new RecordTranslationCollection();
        }
    ....
        /**
         * @param RecordTranslation $translation
         */
        public function addTranslation(RecordTranslation $translation): void
        {
            /** @var RecordTranslation $translation */
            foreach ($this->translations as $currentTranslation) {
                if ($currentTranslation->getLocale() === $translation->getLocale()) {
                    $this->translations->removeElement($currentTranslation);
                    break;
                }
            }
            $this->translations->add($translation);
        }
    }

public function convertToDatabaseValue($collection, AbstractPlatform $platform)
    {
        // при апдейте, сюда не попадаем
    }


    public function convertToPHPValue($json, AbstractPlatform $platform)
    {
        $data = json_decode($json, true) ?? [];
        $collection = new RecordTranslationCollection();
        foreach ($data as $translate) {
            $collection->add($this->hydrator->hydrate($translate, RecordTranslation::class));
        }
        return $collection;
    }

If you somehow pervert with clone, then everything works. Any thoughts on how to resolve this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question