Locastic / ApiPlatformTranslationBundle

Translation bundle for ApiPlatform based on Sylius translation
MIT License
85 stars 28 forks source link

Not null violation #34

Closed kevinG73 closed 3 years ago

kevinG73 commented 3 years ago

hello , i use symfony 4.4 and posgresql , 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

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Translation\TagTranslation;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Filter\FullTextSearchFilter;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @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);
    }

    /**
     * @return Collection|Image[]
     */
    public function getImages(): Collection
    {
        return $this->images;
    }

    public function addImage(Image $image): self
    {
        if (!$this->images->contains($image)) {
            $this->images[] = $image;
        }

        return $this;
    }

    public function removeImage(Image $image): self
    {
        if ($this->images->contains($image)) {
            $this->images->removeElement($image);
        }

        return $this;
    }

    /**
     * @return Collection|Category[]
     */
    public function getCategories(): Collection
    {
        return $this->categories;
    }

    public function addCategory(Category $category): self
    {
        if (!$this->categories->contains($category)) {
            $this->categories[] = $category;
            $category->addTag($this);
        }

        return $this;
    }

    public function removeCategory(Category $category): self
    {
        if ($this->categories->contains($category)) {
            $this->categories->removeElement($category);
            $category->removeTag($this);
        }

        return $this;
    }

    protected function createTranslation(): TranslationInterface
    {
        return new TagTranslation();
    }
}

and TagTranslation.php

<?php

namespace App\Entity\Translation;

use Doctrine\ORM\Mapping as ORM;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslation;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity()
 */
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 ?

paullla commented 3 years ago

hi @kevinG73 did you manage to find the problem?

kevinG73 commented 3 years ago

Hello @paullla , yes i removed unique=true on title but i get another issue i get this error : No locale has been set and current locale is undefined when i use get method

paullla commented 3 years ago

there are a few closed issues with that error, you can take a look if it helps

kevinG73 commented 3 years ago

fixed , i reinstalled packages rm -rf vendor/ and composer install