I'm trying to add KnpLabs Doctrine Behaviors - and precisely, the Translatable Behavior - on one of the entity in my API Platform project.
Here's what I've done so far :
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
* @ApiResource
*/
class Article
{
use ORMBehaviors\Translatable\Translation,
ORMBehaviors\Timestampable\Timestampable
;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
}
And here's the Entity translation :
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Traits as CustomTraits;
/**
* @ORM\Entity(repositoryClass="App\Repository\ArticleTranslationRepository")
* @ApiResource
*/
class ArticleTranslation
{
use ORMBehaviors\Translatable\Translatable;
/**
* @var string
*
* @ORM\Column(name="someFieldToTranslate", type="string", length=255, nullable=true)
*/
private $someFieldToTranslate;
public function getSomeFieldToTranslate(){...}
public function setSomeFieldToTranslate($someFieldToTranslate){...}
}
Here's the basic "configuration" for getting Translatable Behavior working according to the doc.
Issues start when I try to update the DB schema : I got this error:
No identifier/primary key specified for Entity "App\Entity\ArticleTranslation". Every Entity must have an identifier/primary key in . (which is being imported from "/Sites/bookshop-api/config/routes/api_platform.yaml"). Make sure there is a
loader supporting the "api_platform" type.
However in Translatable Traits, there's already an ID and documentation precise that Translation Entity should only have fields we want to translate...
Anyway, I've put an ID to this ArtcleTranslation Entity to get rid of the error :
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Traits as CustomTraits;
/**
* @ORM\Entity(repositoryClass="App\Repository\ArticleTranslationRepository")
* @ApiResource
*/
class ArticleTranslation
{
use ORMBehaviors\Translatable\Translatable;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="content", type="string", length=255, nullable=true)
*/
private $content;
public function getContent(){...}
public function setContent($someContent){...}
}
From here, no error when I update the DB schema. Perfect ! Now I can take a look at the Swagger Documentation :
Everything looks fine ! But when I do take a look at the DB :
In Article table :
- no "local" field
- no "empty" field
In ArticleTranslation table :
- no "translatable_id" field
- no "currentLocal"
- no "defaultLocal"
I guess it must be linked but in the swagger POST Tab, the model is different too.
I only tried /GET and /POST method on both entities, they're working (I can see it in DB) but no relation between the 2 of them.
I hope my post is not too long but I tried to be the more specific !
Thanks in advance