我的api平台关系中违反完整性约束

时间:2020-07-27 18:51:16

标签: symfony symfony4 api-platform.com symfony-4.4

Symfony : 4.4
API PLATFORM : 2.5 

我必须实体:客户和位置:一个客户有很多位置,一个位置与一个客户有关

客户实体:

    /**
 * @ORM\Entity(repositoryClass="App\Repository\ClientRepository")
 *
 *
 * @ApiResource(
 *     normalizationContext={"clients:get"},
 *     collectionOperations={
 *          "get"={
 *              "normalization_context": { "groups" = {"clients:get"} },
 *          },
 *          "post"={
 *               "security"="is_granted('IS_AUTHENTICATED_ANONYMOUSLY')",
 *               "normalization_context": { "groups" = {"client:get"} },
 *               "denormalization_context": { "groups" = {"client:create"} },
 *               "method"="POST",
 *               "controller"=ClientCreate::class
 *          }
 *     },
 *     itemOperations={
 *          "get"={
 *              "normalization_context": { "groups" = {"clients:get"} },
 *           },
 *          "put"={
 *               "security"="is_granted('IS_AUTHENTICATED_ANONYMOUSLY')",
 *               "normalization_context": { "groups" = {"client:get"} },
 *               "denormalization_context": { "groups" = {"client:put"} },
 *          },
 *     }
 * )
 *
 */
class Client implements UserInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     *
     * @Groups({"clients:get"})
     *
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Location", mappedBy="client", cascade={"persist", "remove"}), orphanRemoval=true)
     *
     *@Groups({"client:create","client:get","client:put"})
     *
     * @ApiSubresource()
     *
     */
    private $locations;

位置实体:

/**
 * @ORM\Entity(repositoryClass="App\Repository\LocationRepository")
 *
 * @ApiResource()
 *
 */
class Location
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     *
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     *
     * @Groups({"client:create","client:get","client:put"})
     *
     */
    private $address;

//... others attributes

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Groups({"client:create","client:get","client:put"})
     *
     */
    private $locationName;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Client", inversedBy="locations", fetch="EXTRA_LAZY")
     * @ORM\JoinColumn(nullable=false)
     */
    private $client;

我试图使关系Client-Location的行为类似于我创建客户端时的行为=>创建位置=>对我而言,使用此代码可以正常工作。 而且我还想在放置客户端时删除其旧位置,并创建一个附加到此客户端的新位置,但是放置错误。

操作 PUT / api / clients / 58 身体

{
    "locations": [
        {
            "address": "dfsdfaaaaaaaa",
            "locationName": "sdfsdf"
        }
    ]
}

响应:

{
"@context": "/api/contexts/Error",
"@type": "hydra:Error",
"hydra:title": "An error occurred",
"hydra:description": "An exception occurred while executing 'UPDATE location SET client_id = ? WHERE id = ?' with params [null, 24]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'client_id' cannot be null",
"trace": [
    {
        "namespace": "",
        "short_class": "",
        "class": "",
        "type": "",
        "function": "",
        "file": "/home/karimengineer/Desktop/SystemFood/api/system_food_api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php",
        "line": 103,
        "args": []
    },

1 个答案:

答案 0 :(得分:0)

您需要在您的 Client 实体中的 locations 变量中添加orphanRemoval=true and cascade={"persist", "remove"}

相关问题