0
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": []
    },
4

1 回答 1

0

您需要在orphanRemoval=true and cascade={"persist", "remove"}您的客户实体中添加位置变量

于 2020-08-27T23:42:10.653 回答