I'm trying to build a custom module that contains an EAV entity in Magento 2 for learning purposes.
I have troubles performing a proper save from an admin controller.
I'm trying to avoid code like:
$data = get data from post here; $myObj->addData($data)->save();
Because the save method is deprecated and because I'm trying to work with interfaces instead of their implementations.
For a flat entity I could do this:
$entity = either get it from a repository or via factory; $data = get data from post $this->dataObjectHelper->populateWithArray($entity, $data, EntityInterface::class);
dataObjectHelper is an instance of
\Magento\Framework\Api\DataObjectHelper that reads the setters of the interface passed as the 3rd parameter and maps the corresponding to the methods from the
$data array to my entity.
$data looks like this:
$data = [ 'title'=>'Some title', 'description' => 'Some description' ]
will just call
$entity->setTitle('Some title')->setDescription('Some description').
But I cannot do that for the EAV entities because my entity interface will contain only the system attributes and not the custom ones.
I need to get the data from POST and pass it to the
$entity instance and then call
$entityRepository->save($entity) but I don't want to lose any custom attributes.
How can I proceed in this case?
I looked in the core and for products and categories the controllers still call
$entity->save() which is not OK.