Not null violation using ApiPlatformTranslationBundle

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]:nnSQLSTATE[23502]: Not null violation

Tag.php

<?php

/**
 * @ORMEntity(repositoryClass="AppRepositoryTagRepository")
 * @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
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     * @Groups({"Category_read","Tag_read","Image_read"})
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=255,unique=true)
     * @Groups({"Category_read","Tag_read","Image_read"})
     */
    private $title;

    /**
     * @ORMManyToMany(targetEntity=Image::class)
     * @Groups({"Tag_read"})
     */
    private $images;

    /**
     * @ORMManyToMany(targetEntity=Category::class, mappedBy="tags")
     * @Groups({"Tag_read"})
     */
    private $categories;


    /**
     * @ORMOneToMany(targetEntity="AppEntityTranslationTagTranslation", 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
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="AppEntityTag", inversedBy="translations")
     */
    protected $translatable;

    /**
     * @ORMColumn(type="string", length=255, unique=true)
     * @Groups({"Tag_read","Tag_write"})
     */
    private $title;

    /**
     * @ORMColumn(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

Source: Symfony4 Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *