Phil Thomas
Phil Thomas

Reputation: 1237

Translatable doctrine array fieds

I have a working entity with a field (of type ArrayCollection) that maps with doctrine to a column of type array.

Now I need this field to be translatable (doctrine behaviors) but I'm getting a Could not convert database value "" to Doctrine Type array ConversionException when fetching an untranslated locale.

How can I keep the array functionality and make it translatable?


The property declaration within the entity Product:

/**
 * @var ArrayCollection
 *
 * @Gedmo\Translatable
 * @ORM\Column(name="categories", type="array")
 */
private $categories;


The controller action:

// ... Controller ProductController nitty gritty

public function showAction(Request $request, $id, $locale)
{
    // omitted stuff, load $product by $id

    $product->setTranslatableLocale($locale); // load translations
    $em->refresh($product);

    return $this->render('AcmeExampleBundle:Product:show.html.twig', [
        'product' => $product
    ]);
}


The exception:

ConversionException

Could not convert database value "" to Doctrine Type array


The exception stack trace: pastebin

Upvotes: 2

Views: 510

Answers (2)

blackbishop
blackbishop

Reputation: 32640

It looks like you're trying to convert an empty string from the database to an array. The problem comes from the categories column. Instead of a serialized empty array, this column contains empty values.

A possible solution is to replace the empty string with the expected array as follow :

UPDATE `product` SET categories="a:0:{}" WHERE categories= "";

Upvotes: 1

Phil Thomas
Phil Thomas

Reputation: 1237

I was wrong. I was trying to set my own {locale} route segment when I should have been using native {_locale} (notice the underscore - documentation).

If you let symfony handle locales through routing, translatable will transparently handle entity translations (including form submissions), and there is no need for calling $entity::setTranslatableLocale() nor EntityManager::refresh().

No need for special treatment to arrays fields, they translate seamlessly out of the box.

Upvotes: 0

Related Questions