M
M
Mike Evstropov2016-07-10 21:06:42
symfony
Mike Evstropov, 2016-07-10 21:06:42

How to make Sonata Admin Bundle work with FieldType Entity in simple_array format?

There are two entities "Category" and "Product". We need to make it possible to edit product categories in multiple mode and store the value as a simple_array.

class Product
. . .
/**
 * @var array
 *
 * @ORM\Column(name="categories", type="simple_array", nullable=true)
 */
private $categories;

public function __construct()
{
    $this->categories = new ArrayCollection();
}

class Category
. . .
public function __toString()
{
    return (string) $this->id;
}

$formMapper->add('categories', 'entity', array(
    'class' => 'Site\AppBundle\Entity\Category',
    'choice_label' => 'name',
    'multiple' => true
));

The trouble is that, in fact, the product categories are saved as I need (I see "2,5,7,9" in the database).
But! If you re-enter the product, then the category field is clear. How to fix it?
p.s. Symfony 3

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2016-07-10
@Aroused

You need to hang `categories`
Data Transformers
on the form field . The problem is that the form expects that in Product::getCategories it will return objects of the Site\AppBundle\Entity\Category class, and you have an array of their id there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question