我有一个清单的OneToMany关系,并且设置了“答案”,以便消费者可以更新清单并向其中添加答案。
当答案表中没有答案,但是当出现问题并且API平台和/或Doctrine要更新所有答案时,此方法很好用,从而导致以下错误:
An exception occurred while executing 'UPDATE answer SET checklist_id = ?, updated = ? WHERE id = ?' with params [null, \"2019-08-18 12:44:55\", 96]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'checklist_id' cannot be null
将以下请求正文放入/api/checklists/2
{
"answers": [
{
"answer": "Ok",
"user": "/api/users/9",
"question": "/api/questions/3"
},
{
"answer": "NotK",
"user": "/api/users/9",
"question": "/api/questions/4"
}
]
}
这是两个实体的配置:
答案:
namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use AppBundle\Entity\Traits\DefaultTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
/**
* Class Answer
* @package AppBundle\Entity
* @ApiResource(
* attributes={
* "normalization_context"={"groups"={"answer:read"}, "enable_max_depth"=true},
* "denormalization_context"={"groups"={"answer:write", "checklist:write"}}
* }
* )
* @ORM\Entity()
* @ORM\Table(name="answer")
*/
class Answer
{
use DefaultTrait;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"answer:read", "answer:write", "checklist:read", "checklist:write"})
*/
private $answer;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* @Groups({"answer:read", "answer:write", "checklist:read", "checklist:write"})
* @MaxDepth(1)
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Checklist", inversedBy="answers")
* @ORM\JoinColumn(nullable=false)
* @Groups({"answer:read", "answer:write", "checklist:write"})
*/
private $checklist;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Question", inversedBy="answers")
* @ORM\JoinColumn(nullable=false)
* @Groups({"answer:read", "answer:write", "checklist:write"})
* @MaxDepth(1)
*/
private $question;
# Getters and setters omitted
}
清单:
<?php
namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use AppBundle\Annotation\CompanyAware;
use AppBundle\Entity\Traits\EnabledTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Traits\DefaultTrait;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(attributes={
* "force_eager"=false,
* "normalization_context"={"groups"={"checklist:read"}, "enable_max_depth"=true},
* "denormalization_context"={"groups"={"checklist:write"}, "enable_max_depth"=true}
* })
* @ORM\Entity
* @ORM\Table(name="checklist")
* @CompanyAware(companyFieldName="company_id")
*/
class Checklist
{
use DefaultTrait;
use EnabledTrait;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @Assert\NotBlank()
* @Groups({"checklist:read", "checklist:write"})
*/
private $name;
/**
* @ORM\Column(name="description", type="text", nullable=true)
*
* @var string
* @Groups({"checklist:read", "checklist:write"})
*/
private $description;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Checklist")
*/
private $parent;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="checklist")
* @Groups({"checklist:read", "checklist:write"})
*/
private $category;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\QuestionGroup", mappedBy="checklist", cascade={"persist", "remove"})
* @Groups({"checklist:read", "checklist:write"})
* @MaxDepth(1)
*/
private $groups;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Question", mappedBy="checklist", cascade={"persist", "remove"})
* @Groups({"checklist:read", "checklist:write"})
*/
private $questions;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Company", inversedBy="checklists")
* @Groups({"checklist:read", "checklist:write"})
*/
private $company;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Answer", mappedBy="checklist", cascade={"persist", "remove"})
* @Groups({"checklist:write"})
* @ApiSubresource()
*/
private $answers;
}