我使用symfony 4.4postgresql
作为数据库系统,我进行了搜索并发现了该捆绑包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没有设置Tag的$ title属性,因此,当主义想将Tag插入数据库时,它将为null。
你可以删除该属性,将其@Groups标记移至方法Tag :: getTitle的doc块中:
/**
* @Groups({"Category_read","Tag_read","Image_read"})
*/
public function getTitle(): ?string
{
return $this->getTranslation()->getTitle();
}
如果应用了序列化组之一,则Apip仍应将:: getTitle的结果序列化为结果json的“ title”:..,如果需要,将retitles主体中的json的“ title”:..反序列化为:: setTitle。为其指定序列化组。
如果它不起作用,或者你想保留Tag :: $ title,则使$ title在数据库中为空:
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"Category_read","Tag_read","Image_read"})
*/
private $title;
(这里不能有unique = true,因为$ title的所有值都将为null。FurhermoreTagTranslation :: $ title已经是唯一的,并且是存储数据的地方)
不错,很有效