使用 ApiPlatformTranslationBundle 不违反 null

Not null violation using ApiPlatformTranslationBundle

我使用 symfony 4.4 和 postgresql 作为数据库系统,我搜索并发现了这个包 https://github.com/Locastic/ApiPlatformTranslationBundle 用于使用 ApiPlatform 进行翻译,所以我以他们为榜样 但是当我尝试使用 POST 方法插入一个新值时,出现此错误:

"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();
    }
}

和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;
    }
}

这是一个常见问题吗?如果你知道另一种方法,有人可以帮助我或给我一些提示

Tag::setTitle 不设置标签的 $title 属性,因此当 doctrine 尝试将标签插入数据库时​​它将为空。

您可以删除 属性 将其 @Groups 标记移动到方法的文档块 Tag::getTitle:

/** 
* @Groups({"Category_read","Tag_read","Image_read"})
*/
public function getTitle(): ?string
{
    return $this->getTranslation()->getTitle();
}

Apip 仍应将 ::getTitle 的结果序列化为“标题”:.. 结果 json 如果序列化组之一适用 并反序列化“标题”:如果您为其指定序列化组,则从请求 body 中的 json 到 ::setTitle。

如果它不起作用,或者您想保留 Tag::$title,请在数据库中使 $title 可为空:

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 * @Groups({"Category_read","Tag_read","Image_read"})
 */
private $title;

(这里不能有 unique=true,因为 $title 的所有值都将为空。此外,TagTranslation::$title 已经是唯一的,这就是存储数据的地方)