api 中缺少 hydra:total 项

Missing hydra:total items in api

我目前遇到一个无法解释的问题。 我有一个这样定义的 symfony/api-platform 实体:

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity
 * @ApiResource(
 *     normalizationContext={"groups"={"node_read"}},
 *     denormalizationContext={"groups"={"node_write"}},
 *     collectionOperations={
 *          "get"={"method"="GET"},
 *          "post"={"method"="POST", "security"="is_granted('ROLE_ADMIN')"}
 *     },
 *     itemOperations={
 *          "get"={"method"="GET"},
 *          "put"={"method"="PUT", "security"="is_granted('ROLE_ADMIN')"},
 *          "patch"={"method"="PATCH", "security"="is_granted('ROLE_ADMIN')"},
 *          "delete"={"method"="DELETE", "security"="is_granted('ROLE_ADMIN')"}
 *     }
 * )
 * @ApiFilter(ExistsFilter::class, properties={"parent"})
 */
class Node
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"node_read"})
     */
    private ?int $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"node_read", "node_write"})
     */
    private string $key;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"node_read", "node_write"})
     */
    private string $label;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"node_read", "node_write"})
     */
    private string $type;

    /**
     * One Category has Many Categories.
     *
     * @OneToMany(targetEntity="Node", mappedBy="parent")
     * @Groups({"node_read"})
     */
    private Collection $children;

    /**
     * Many Nodes have One Node.
     *
     * @ManyToOne(targetEntity="Node", inversedBy="children")
     * @JoinColumn(name="parent_id", referencedColumnName="id")
     * @Groups({"node_write"})
     */
    private ?Node $parent;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    private string $description;

    public function __construct()
    {
        $this->children = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getParent(): ?Node
    {
        return $this->parent;
    }

    public function setParent(?Node $parent): self
    {
        $this->parent = $parent;

        return $this;
    }

    public function getChildren(): Collection
    {
        return $this->children;
    }

    public function setChildren(Collection $children): self
    {
        $this->children = $children;

        return $this;
    }

    public function getKey(): string
    {
        return $this->key;
    }

    public function setKey(string $key): self
    {
        $this->key = $key;

        return $this;
    }

    public function getLabel(): string
    {
        return $this->label;
    }

    public function setLabel(string $label): self
    {
        $this->label = $label;

        return $this;
    }

    public function getType(): string
    {
        return $this->type;
    }

    public function setType(string $type): self
    {
        $this->type = $type;

        return $this;
    }

    public function getDescription(): string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }
}

在对集合进行 get 调用时,我在开发环境和生产环境之间得到了不同的结果。

在产品环境中,api 缺少 hydra 元数据密钥 hydra:total

开发环境中的结果:

context: "/api/contexts/Node"
@id: "/api/nodes"
@type: "hydra:Collection"
hydra:member: [{@id: "/api/nodes/1", @type: "Node", id: 1, key: "risk-of-rain-2", label: "Risk Of Rain 2",…},…]
hydra:totalItems: 2
hydra:view: {@id: "/api/nodes?exists%5Bparent%5D=false", @type: "hydra:PartialCollectionView"}
hydra:search: {@type: "hydra:IriTemplate", hydra:template: "/api/nodes{?exists[parent]}",…}

产品环境中的结果:

@id: "/api/nodes"
@type: "hydra:Collection"
hydra:member: [{@id: "/api/nodes/12", @type: "Node", id: 12, key: "feedback", label: "Feedback", type: "Game",…},…]
hydra:view: {@id: "/api/nodes?exists%5Bparent%5D=false", @type: "hydra:PartialCollectionView"}
hydra:search: {@type: "hydra:IriTemplate", hydra:template: "/api/nodes{?exists[parent]}",…}

正如你在 prod 中看到的那样,我缺少密钥 hydra:totalItems: 2,它允许我进行 react-admin 工作。

我比较了 project composer 依赖和 PHP 版本,prod 是 ISO 到 dev env。

我发现我的问题与提供的源代码完全无关,但与部署有关。

Rsync 没有删除项目的已删除文件,我有一个覆盖 Dataprovider。

解决方案是在 rsync 命令中使用 --delete 选项。