1
votes

I want to save some data in a serialized array. The following

->add('consumptionData', 'sonata_type_native_collection', array(
            'label' => 'Verbrauchsdaten',
            'entry_type' => ConsumptionDataType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'entry_options' => array(
                'label' => false
            )
        ))

works fine with symfony but with sonata admin I get

A new entity was found through the relationship 'AppBundle\Entity\Document#meterPoints' that was not configured to cascade persist operations for entity ...

How can I tell sonata that it should be a serialized array and not an Entity?

1
IMO in general: this is not a sonata issue, the sonata_type_native_collection field type extends the CollectionType field. Regarding the info you have posted: the error is about meterPoints but the sonata admin definition is related to consumptionData Please provide all necessary and correct code samples if you want some help.lordrhodos

1 Answers

0
votes

If you want to save your data as array you can "cast" it to needed type in getter for your field in entity.

If it ruins logic of your code, you can add some to new getter/setter methods for "fake" entity. Form field names are legal when getter and setter are specified for them even if field with this name don't exist in your entity.

So, you can write something like this in your builder:

->add('consumptionDataForArray', 'sonata_type_native_collection', [...])) 

And something like this in your entity:

public function setConsumptionDataForArray($data)
{
    // change your data here as you need

    $this->consumptionData = $neededData;
}

public function getConsumptionDataForArray()
{
    // cast your data back to array

    return $castedBackData;
}

Hope this is the answer to your question.