i use symfony 4.4 and postgresql
as database system ,I searched and I discovered this bundle https://github.com/Locastic/ApiPlatformTranslationBundle
for Translation with ApiPlatform so i followed their example
but when i try to insert a new value using POST method , I get this error :
"hydra:description": "An exception occurred while executing 'INSERT INTO tag (id, title) VALUES (?, ?)' with params [5, null]:\n\nSQLSTATE[23502]: Not null violation
Tag.php
<?php
/**
* @ORM\Entity(repositoryClass="App\Repository\TagRepository")
* @ApiResource(
* attributes={
* "filters"={"translation.groups"},
* "normalization_context"={"groups"={"Tag_read"}},
* "denormalization_context"={"groups"={"Tag_write"}}
* },
* collectionOperations={
* "get",
* "post"={
* "normalization_context"={"groups"={"translations"}}
* }
* },
* itemOperations={
* "get",
* "put"={
* "normalization_context"={"groups"={"translations"}}
* },
* "delete"
* }
* )
*/
class Tag extends AbstractTranslatable
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"Category_read","Tag_read","Image_read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255,unique=true)
* @Groups({"Category_read","Tag_read","Image_read"})
*/
private $title;
/**
* @ORM\ManyToMany(targetEntity=Image::class)
* @Groups({"Tag_read"})
*/
private $images;
/**
* @ORM\ManyToMany(targetEntity=Category::class, mappedBy="tags")
* @Groups({"Tag_read"})
*/
private $categories;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Translation\TagTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
* @Groups({"Tag_write", "translations"})
*/
protected $translations;
public function __construct()
{
parent::__construct();
$this->images = new ArrayCollection();
$this->categories = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->getTranslation()->getTitle();
}
public function setTitle(string $title)
{
$this->getTranslation()->setTitle($title);
}
protected function createTranslation(): TranslationInterface
{
return new TagTranslation();
}
}
and TagTranslation.php
<?php
class TagTranslation extends AbstractTranslation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Tag", inversedBy="translations")
*/
protected $translatable;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Groups({"Tag_read","Tag_write"})
*/
private $title;
/**
* @ORM\Column(type="string")
*
* @Groups({"Tag_write", "translations"})
*/
protected $locale;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
}
Is this a common issue ? someone could help meor give me some hints if you know another method
Tag::setTitle does not set the $title property of a Tag, so it will be null when doctrine tries to instert the Tag into the database.
You can remove that property moving its @Groups tag to the doc block of method Tag::getTitle:
/**
* @Groups({"Category_read","Tag_read","Image_read"})
*/
public function getTitle(): ?string
{
return $this->getTranslation()->getTitle();
}
Apip should still serialize the result of ::getTitle into "title": .. of the resulting json if one of the serialization groups apply and unserialize "title": .. from the json in the reques body into ::setTitle if the you specify serialization groups for it.
If it does not work, or you want to keep Tag::$title, make $title nullable in the database:
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"Category_read","Tag_read","Image_read"})
*/
private $title;
(you can not have unique=true here because all values of $title will be null. Furhermore TagTranslation::$title already is unique and that is where the data is stored)