Skip to content

Implementing with Symfony, which ORM type ? #26

@Mopster

Description

@Mopster

Which ORM Column type should I be using to save this to the database ? I tried text and simple_array, but I can't get it to work. When using text, it saves as 'Array', when using simple_array, it only saves the value of the last item in the collection.

Entity :

/**
     * Store key-value options
     * @ORM\Column(type="simple_array", name="options", nullable=true)
     */
    protected $options;

public function getOptions()
    {
        return $this->options;
    }

    public function setOptions($options)
    {
        $this->options = $this->convertToArray($options);
    }

    private function convertToArray($data)
    {
        if (is_array($data)) {
            return $data;
        }

        if ($data instanceof KeyValueContainer) {
            return $data->toArray();
        }

        if ($data instanceof \Traversable) {
            return iterator_to_array($data);
        }

        throw new \InvalidArgumentException(sprintf('Expected array, Traversable or KeyValueContainer, got "%s"', is_object($data) ? getclass($data) : get_type($data)));
    }

Form :

$builder->add('options', KeyValueType::class, [
            'required' => false,
            'value_type' => TextType::class,
            'use_container_object' => true,
        ]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions