phpsymfonydoctrinetranslationsonata

No mapping file found when implementing TranslationInterface


I am using Ez-Extends to extend the Sonata Classification Bundle's Category entity.

Now I want to use the Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait to make my Category translatable. I apply the trait in my Category class and have it implement TranslatableInterface. Then I create a new CategoryTranslation class:

<?php

namespace App\Application\Sonata\ClassificationBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity
 */
class CategoryTranslation implements TranslationInterface
{
    use TranslationTrait;

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string | null
     * @ORM\Column(type="string")
     * @Groups("filter")
     */
    private $name;

    private $trans;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     * @return CategoryTranslation
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getName(): ?string
    {
        return $this->name;
    }

    /**
     * @param string|null $name
     * @return CategoryTranslation
     */
    public function setName(?string $name): CategoryTranslation
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getTrans()
    {
        return $this->trans;
    }

    /**
     * @param mixed $trans
     * @return CategoryTranslation
     */
    public function setTrans($trans)
    {
        $this->trans = $trans;
        return $this;
    }



}

However, when I try to do anything in the app, I now get this message:

In MappingException.php line 65:

  No mapping file found named 'CategoryTranslation.orm.xml' for class 'App\Application\Sonata\ClassificationBundle\E
  ntity\CategoryTranslation'.

As far as I know, this new entity should be using annotations for mapping, not XML. What am I doing wrong?

EDIT: Here is the content of my doctrine.yaml file:

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(DATABASE_URL): ''

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.6'
        charset: utf8

        # With Symfony 3.3, remove the `resolve:` prefix
        url: '%env(resolve:DATABASE_URL)%'

        types:
            json: Sonata\Doctrine\Types\JsonType
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
            SonataMediaBundle: ~
            ApplicationSonataMediaBundle: ~
        metadata_cache_driver: array
        result_cache_driver: array
        query_cache_driver: array

Solution

  • I guess the sonata mapping configuration for entities is xml. By creating a new sonata entity in your directory App\Application\Sonata\ClassificationBundle\Entity\CategoryTranslation, it will use the configuration of the mapping defined byin your doctrine.yaml:

    SonataMediaBundle: ~

    ApplicationSonataMediaBundle: ~

    Which use the default one (xml). Only mapping in your App mapping (i.e. in your directory dir: '%kernel.project_dir%/src/Entity') will use the annotation as you specify it.

    You can either specifiy type: annotation for your sonata bundle (but it may will fail existing entity from bundle, so not recommanded, but to test) or generate the xml mapping file for this specific entity.