0

I am using KNP doctrine behaviours for translations. It works from fixtures but does not works in Sonata admin at all.

My last issue is "Unable to find the association target class of "translations" in ...".

Form form widget i am using A2lix\TranslationFormBundle. This is code from Sonata form builder:

$formMapper
        ->add('id', HiddenType::class)
        ->add('countOfDays')
        ->add('countOfAccounts')
        ->add('price')
        ->add('profileType')
        ->add('translations', TranslationsType::class, [
            'fields' => [
                'name' => [
                    'field_type' => TextType::class,
                    'label' => 'Заголовок'
                ],
            ],
        ]);

I configured entities, added fixtures with data for two languages and tested in twig. All works fine.

namespace App\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;

/**
 * @ORM\Entity()
 */
class Plan implements TranslatableInterface
{
    use TranslatableTrait;

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

    /**
     * Call
     *
     * @param string $method
     * @param array $args
     * @return string
     */
    public function __call($method, $args)
    {
        if (!method_exists(self::getTranslationEntityClass(), $method)) {
            $method = 'get' . ucfirst($method);
        }

        return $this->proxyCurrentLocaleTranslation($method, $args);
    }

    public function getId(): ?int
    {
        return $this->id;
    }

}


namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;

/**
 * @ORM\Entity()
 */
class PlanTranslation implements TranslationInterface
{
    use TranslationTrait;

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

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }
}

I also was trying to create form mapper, but it does not save data to DB and do not show translations.

And i get next error: issue in sonata form

1 Answers1

0

I solved the issue.

I used type Collection for this but you have to create new Admin for translation.

$formMapper
            ->add('id')
            ->add('countOfDays')
            ->add('countOfAccounts')
            ->add('price')
            ->add('profileType')
            ->add('translations', CollectionType::class,
                [
                    'btn_add' => false,
                    'type_options' => ['delete' => false,]
                ],
                [
                    'edit' => 'inline',
                    'inline'     => 'table',
                ]);