KnpLabs / DoctrineBehaviors

Doctrine2 behavior traits that help handling Blameable, Loggable, Sluggable, SoftDeletable, Uuidable, Timestampable, Translatable, Tree behavior
http://knplabs.com
MIT License
911 stars 286 forks source link

[Translation] collectionType with Translatable : field not found from twig (error : 'Neither the property "name" nor one the methods...') #720

Closed kegilko closed 1 year ago

kegilko commented 1 year ago

Hello and thanks firstly for your lib !

I have a similar issue described in this closed post.

I have two entities named 'content' and 'criteredetest' through a oneToMany relation (a content can handle several 'criteredetest'). The entity 'criteredetest' has some fields and two of them are translatables fields

All works like a charm when I create some 'contents' (and attached 'criteresdetest') with symfony fixtures. But when I want to show a 'content' from my browser ( with a twig template ), I get this pretty error :

image

Here some code of my project :

// the entity 'content'

#[ORM\Entity(repositoryClass: ContentRepository::class)]
class Content implements TranslatableInterface
{
    use TranslatableTrait; // I don't know if its important to show this line in this post (the entity 'Content' contains some another field I need to translate (like the title of the content) )

    // ...

    #[ORM\OneToMany(mappedBy: 'content', targetEntity: Criteredetest::class, cascade: ["persist", "remove"], orphanRemoval: true)]
    private Collection $criteresdetest;

    // ...
}
// the entity 'criteredetest'

#[ORM\Entity(repositoryClass: CriteredetestRepository::class)]
class Criteredetest implements TranslatableInterface
{
    use TranslatableTrait;

    // ... 

    // some fields are defined, but not the fields 'name' and 'description' I need to translate in my app

// use ...

// the entity 'criteredetestTranslation'
#[ORM\Entity(repositoryClass: CriteredetestRepository::class)]
class CriteredetestTranslation implements TranslationInterface
{
    use TranslationTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 200, nullable: true)]
    private ?string $name = null;

    #[ORM\Column(type: Types::TEXT, length: 200, nullable: true)]
    private ?string $description = null;

    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;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }
}

Did i make a mistake somewhere ? Thanks for any help !

kegilko commented 1 year ago

I just missed this part of the documentation ;)