【问题标题】:Integrity constraint violation in my api platform relation我的 api 平台关系中的完整性约束违规
【发布时间】:2020-07-27 18:51:16
【问题描述】:
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 的行为就像我创建客户端时一样 => 位置已创建 => 使用此代码对我来说没问题。 而且我希望当我放置一个客户端时,它的旧位置被删除,它会创建一个附加到这个客户端的新位置,但我在放置时出错。

动作 放置 /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": []
    },

【问题讨论】:

  • 很奇怪。我注意到您的代码和我的代码之间的唯一区别是 @ApiSubResource 注释和 fetch="EXTRA_LAZY"。你可以试着不放吗?此外,当您更新 locations 之外的其他字段时会发生什么?
  • @rugolinifr 好的,我做到了,没有任何改变,出现同样的错误,当我从位置字段中删除“client:put”组时,请求通过并且位置以外的其他字段正确更新.

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2016-11-22
    • 2022-01-23
    • 1970-01-01
    • 2015-11-03
    相关资源
    最近更新 更多