首页 > 解决方案 > 更新产品删除错误刚刚创建的数量

问题描述

我对 API 平台有疑问。我有一个产品实体和一个数量实体。我为我的 Angular 应用程序使用 API Platform 一个 API。

一个产品可以有多个数量,一个数量只能有一个产品。

在我的角度形式中,如果我创建一个链接到它工作的产品的数量(它是在数据库中创建的)。之后,当我更新此产品时,这些先前的数量被删除(即使我已将先前添加的数量的 IRI 添加到更新前的产品数量中)。

这是一些代码:

数量.php

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;

/**
 * @ApiResource()
 * @ApiFilter(OrderFilter::class, properties={"packing": "ASC"})
 * @ORM\Entity(repositoryClass="App\Repository\QuantityRepository")
 */
class Quantity
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Product", inversedBy="quantities")
     * @ORM\JoinColumn(nullable=false)
     */
    private $product;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Ingredient")
     * @ORM\JoinColumn(nullable=false)
     */
    private $ingredient;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Packing")
     * @ORM\JoinColumn(nullable=false)
     */
    private $packing;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $value;

产品.php

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Annotation\ApiSubresource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Dompdf\Dompdf;
use Dompdf\Options;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
 * @ORM\HasLifecycleCallbacks()
 * @ApiResource(
 *      collectionOperations={
 *          "get",
 *          "post"
 *     },
 *     itemOperations={
 *          "get",
 *          "put",
 *          "delete",
 *          "api_products_get_pdf_by_category" = {
 *              "method"="GET",
 *              "controller"="ProductController::class",
 *          },
 *          "api_products_get_pdf_by_list" = {
 *              "method"="GET",
 *              "controller"="ProductController::class",
 *          },
 *          "duplicate" = {
 *              "method"="GET",
 *              "controller"="ProductController::class",
 *          },
 *          "api_products_make_pdf" = {
 *              "method"="GET",
 *              "controller"="ProductController::class",
 *          }
 *     },
 *     normalizationContext={"groups"={"product"}}
 *     )
 * @ApiFilter(OrderFilter::class, properties={"name", "id", "modifiedAt"})
 * @ApiFilter(SearchFilter::class, properties={"id": "exact", "name": "partial", "productCategories.name": "exact", "enable": "exact", "bakeryProductsOverride.bakery.id":"exact"})
 */
class Product
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"product"})
     */
    private $id;

    /* ... OTHERS PROPERTIES CUTTED ... */

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Quantity", mappedBy="product", orphanRemoval=true)
     * @Groups({"product"})
     */
    private $quantities;
}

标签: phpsymfonydoctrine-ormapi-platform.com

解决方案


我的错。与 API 平台无关。Angular 中的调用顺序有误。对不起,噪音。


推荐阅读