KnpLabs / DoctrineBehaviors

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

Translateable entity needs to be abstract? #624

Closed christophrahe1 closed 3 years ago

christophrahe1 commented 3 years ago

Hi there,

I have implemented two entities for translation as described in the docs, however, Symfony throws the following error:

Error: Class App\Entity\BlogPost contains 11 abstract methods and must therefore be declared abstract or implement the remaining methods (Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface::getTranslations, Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface::getNewTranslations, Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface::addTranslation, ...)

When I declare that class "abstract", I am getting the following error: Trying to invoke abstract method Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface::getTranslationEntityClass()

How can I solve this problem?

Class BlogPost


declare(strict_types=1);

namespace App\Entity;

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

/**
 * @ORM\Entity
 */
abstract class BlogPost implements TranslatableInterface
{
    use TranslationTrait;

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

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $active;

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

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

    public function setActive(int $active): self
    {
        $this->active = $active;

        return $this;
    }

}

Class BlogPostTranslation

declare(strict_types=1);

namespace App\Entity;

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

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

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

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

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

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected  $slug;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    protected  $text;

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

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

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

    public function getTitle(): string
    {
        return $this->title;
    }

    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    public function getSlug(): string
    {
        return $this->slug;
    }

    public function setSlug(string $slug): void
    {
        $this->slug = $slug;
    }

    public function getText(): string
    {
        return $this->text;
    }

    public function setText(string $text): void
    {
        $this->text = $text;
    }
}
souidev commented 2 years ago

replace use TranslationTrait by TranslatableTrait in BlogPost entity