skip to Main Content

In Symfony Api Platform application, using Maker Bundle to add a relationship field "Owner" to "Community" Entity coused all "Get" api calls to return the same error. This is a new project I use all the newest available version.

Request:
curl -X ‘GET’
‘http://127.0.0.1:8000/api/communities?page=1’
-H ‘accept: 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 AppEntity;

use ApiPlatformMetadataApiResource;
use AppRepositoryCommunityRepository;
use DoctrineDBALTypesTypes;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use SymfonyComponentSerializerAnnotationGroups;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineORMMapping as ORM;
use AppEntityUser;


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

    #[Groups(['community:read'])]
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    private ?int $id = null;

    #[AssertNotBlank]
    #[Groups(['community:read', 'community:create', 'community:update'])]
    #[ORMColumn(length: 50)]
    
    private ?string $name = null;

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

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

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

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

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

    //getters and setters
// User Entity

namespace AppEntity;

use ApiPlatformMetadataApiResource;
use ApiPlatformMetadataDelete;
use ApiPlatformMetadataGet;
use ApiPlatformMetadataGetCollection;
use ApiPlatformMetadataPatch;
use ApiPlatformMetadataPost;
use ApiPlatformMetadataPut;
use DoctrineDBALTypesTypes;
use AppRepositoryUserRepository;
use AppStatePasswordHasher;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use SymfonyComponentSecurityCoreUserPasswordAuthenticatedUserInterface;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentSerializerAnnotationGroups;
use DoctrineCommonCollectionsCollection;
use DoctrineCommonCollectionsArrayCollection;

use SymfonyComponentValidatorConstraints as Assert;
use DoctrineORMMapping as ORM;
use AppEntityCommunity;

#[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']],
)]
#[ORMEntity(repositoryClass: UserRepository::class)]
#[ORMTable(name: '`user`')]
#[UniqueEntity('email')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[Groups(['user:read'])]
    #[ORMId]
    #[ORMColumn(type: 'integer')]
    #[ORMGeneratedValue]
    private ?int $id = null;

    #[AssertNotBlank]
    #[AssertEmail]
    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORMColumn(length: 80, unique: true)]
    private ?string $email = null;

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

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

    #[ORMColumn(type: 'json')]
    private array $roles = [];

    #[AssertNotBlank]
    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORMColumn(length: 25)]
    private ?string $nickname = null;

    #[AssertNotBlank]
    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORMColumn(length: 30)]
    private ?string $login = null;

    #[AssertNotBlank]
    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORMColumn(type: Types::DATETIMETZ_MUTABLE)]
    private ?DateTimeInterface $birthday = null;

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

    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORMOneToMany(targetEntity: Community::class, mappedBy: 'owner')]
    private Collection $owned_communites;
//getters and setters

2

Answers


  1. Something is completely broken in Relationships in the newest version of the stack. The error appears everytime you add connection between entities. You can either try downgrading each component of Doctrine one by one via composer or simply move to Laravel and create your API there. It’s much less broken. Cheers!

    Login or Signup to reply.
  2. I had the same issue. I solved it by changing the version of doctrine/orm from 3.0 to version 2.18.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search