向实体添加关系会导致奇怪的 ORM 映射错误

问题描述 投票:0回答:2

在 Symfony Api 平台应用程序中,使用 Maker Bundle 将关系字段“Owner”添加到“Community”实体导致所有“Get”api 调用返回相同的错误。这是一个新项目,我使用所有最新的可用版本。

要求: 卷曲-X'获取'
'http://127.0.0.1:8000/api/communities?page=1'
-H '接受:application/ld+json'

//Error

{
  "@id": "/api/errors/500",
  "@type": "hydra:Error",
  "title": "An error occurred",
  "detail": "Attempted to load class \"ClassMetadataInfo\" from namespace \"Doctrine\\ORM\\Mapping\".\nDid you forget a \"use\" statement for another namespace?",
  "status": 500,
  "type": "/errors/500",
  "trace": [
    {
      "file": "\\vendor\\api-platform\\core\\src\\Doctrine\\Orm\\Extension\\EagerLoadingExtension.php",
      "line": 98,
      "function": "joinRelations",
      "class": "ApiPlatform\\Doctrine\\Orm\\Extension\\EagerLoadingExtension",
      "type": "->"
    },
    {
      "file": "\\vendor\\api-platform\\core\\src\\Doctrine\\Orm\\Extension\\EagerLoadingExtension.php",
      "line": 53,
      "function": "apply",
      "class": "ApiPlatform\\Doctrine\\Orm\\Extension\\EagerLoadingExtension",
      "type": "->"
    },
// Community.php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use App\Repository\CommunityRepository;
use Doctrine\DBAL\Types\Types;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\User;


#[ORM\Entity(repositoryClass: CommunityRepository::class)]
#[ApiResource(
    normalizationContext: ['groups' => ['community:read']],
    denormalizationContext: ['groups' => ['community:create', 'community:update']],
)]
#[ORM\Table(name: '`community`')]
#[UniqueEntity('name')]
class Community
{
    const STATUS_PUBLIC = 'public';
    const STATUS_PRIVATE = 'private';
    const STATUS_RESTRICTED = 'restricted';

    #[Groups(['community:read'])]
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[Assert\NotBlank]
    #[Groups(['community:read', 'community:create', 'community:update'])]
    #[ORM\Column(length: 50)]
    
    private ?string $name = null;

    // Address and name of given community
    #[Groups(['community:read', 'community:create', 'community:update'])]
    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $description = null;

    // Amount of users of given community
    #[ORM\Column]
    #[Groups(['community:read'])]
    private ?int $numberOfUsers = 0;

    // Date of creation of given community
    #[Groups(['community:read'])]
    #[ORM\Column(type: Types::DATETIMETZ_MUTABLE)]
    private ?\DateTimeInterface $creation_time = null;

    // Status of given community; PUBLIC, PRIVATE, RESTRICTED
    #[Groups(['community:read', 'community:create', 'community:update'])]
    #[ORM\Column(type: 'string', length: 10)]
    private ?string $status = self::STATUS_PUBLIC;

    #[Groups(['community:read', 'community:create', 'community:update'])]
    #[ORM\ManyToOne(targetEntity: User::class ,inversedBy: 'owned_communites')]
    #[ORM\JoinColumn(nullable: false)]
    private ?User $owner = null;

    //getters and setters
// User Entity

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use Doctrine\DBAL\Types\Types;
use App\Repository\UserRepository;
use App\State\PasswordHasher;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;

use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Community;

#[ApiResource(
    operations: [
        new GetCollection(),
        new Post(processor: PasswordHasher::class, validationContext: ['groups' => ['Default', 'user:create']]),
        new Get(),
        new Put(processor: PasswordHasher::class),
        new Patch(processor: PasswordHasher::class),
        new Delete(),
    ],
    normalizationContext: ['groups' => ['user:read']],
    denormalizationContext: ['groups' => ['user:create', 'user:update']],
)]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
#[UniqueEntity('email')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[Groups(['user:read'])]
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[Assert\NotBlank]
    #[Assert\Email]
    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORM\Column(length: 80, unique: true)]
    private ?string $email = null;

    #[ORM\Column]
    //DEBUG ONLY:
    //#[Groups(['user:read', 'user:create', 'user:update'])]
    private ?string $password = null;

    #[Assert\NotBlank(groups: ['user:create'])]
    #[Groups(['user:create', 'user:update'])]
    private ?string $plainPassword = null;

    #[ORM\Column(type: 'json')]
    private array $roles = [];

    #[Assert\NotBlank]
    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORM\Column(length: 25)]
    private ?string $nickname = null;

    #[Assert\NotBlank]
    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORM\Column(length: 30)]
    private ?string $login = null;

    #[Assert\NotBlank]
    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORM\Column(type: Types::DATETIMETZ_MUTABLE)]
    private ?\DateTimeInterface $birthday = null;

    #[Groups(['user:read'])]
    #[ORM\Column(type: Types::DATETIMETZ_MUTABLE)]
    private ?\DateTimeInterface $creation_time = null;

    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORM\OneToMany(targetEntity: Community::class, mappedBy: 'owner')]
    private Collection $owned_communites;
//getters and setters
php symfony doctrine entity-relationship api-platform.com
2个回答
0
投票

最新版本的堆栈中的关系完全被破坏了。每次在实体之间添加连接时都会出现该错误。您可以尝试通过 Composer 将 Doctrine 的每个组件一一降级,或者直接迁移到 Laravel 并在那里创建您的 API。破损的情况要少得多。干杯!


0
投票

我也有同样的问题。我通过将doctrine/orm的版本从3.0更改为版本2.18来解决它。

© www.soinside.com 2019 - 2024. All rights reserved.