首页 > 解决方案 > 如何修复“类 XYZ 不是有效实体或映射的超类”。用教义加入列?

问题描述

我有以下实体,它是另一个实体的 joinColumn 的一部分。特别是这个实体给出了错误:

类“App\Entity\BankAccount”不是有效的实体或映射的超类。

实体BankAccount.php

<?php

namespace App\Entity;

use Sylius\Component\Resource\Model\ResourceInterface;

class BankAccount implements ResourceInterface
{
    protected ?int $id = null;

    protected ?string $externalId = null;

    protected ?\DateTime $takenAt = null;

    protected string $IBAN = '';

    protected bool $used = false;

    protected $customer;
    protected ?Purchase $purchase = null;

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

    /**
     * @param int $id
     * @return BankAccount
     */
    public function setId(int $id): BankAccount
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getExternalId(): ?string
    {
        return $this->externalId;
    }

    /**
     * @param string|null $externalId
     * @return BankAccount
     */
    public function setExternalId(?string $externalId): BankAccount
    {
        $this->externalId = $externalId;
        return $this;
    }


    /**
     * @return string
     */
    public function getIBAN(): string
    {
        return $this->IBAN;
    }

    /**
     * @param string $IBAN
     * @return BankAccount
     */
    public function setIBAN(string $IBAN): BankAccount
    {
        $this->IBAN = $IBAN;
        return $this;
    }

    /**
     * @return bool
     */
    public function isUsed(): bool
    {
        return $this->used;
    }

    /**
     * @param bool $used
     * @return BankAccount
     */
    public function setUsed(bool $used): BankAccount
    {
        $this->used = $used;
        return $this;
    }

    /**
     * @return ?\DateTime
     */
    public function getTakenAt(): ?\DateTime
    {
        return $this->takenAt;
    }

    /**
     * @param \DateTime $takenAt
     * @return BankAccount
     */
    public function setTakenAt(?\DateTime $takenAt): BankAccount
    {
        $this->takenAt = $takenAt;
        return $this;
    }

    public function getCustomer()
    {
        return $this->customer;
    }

    /**
     * @return BankAccount
     */
    public function setCustomer($customer): BankAccount
    {
        $this->customer = $customer;
        return $this;
    }

    /**
     * @return Purchase|null
     */
    public function getPurchase(): ?Purchase
    {
        return $this->purchase;
    }

    /**
     * @param Purchase|null $purchase
     * @return BankAccount
     */
    public function setPurchase(?Purchase $purchase): BankAccount
    {
        $this->purchase = $purchase;
        return $this;
    }

}

教义配置src/Resources/config/BankAccount.orm.yml

App\Entity\BankAccount:
  type: entity
  table: bank_account
  id:
    id:
      type: bigint
      generator:
        strategy: AUTO
  indexes:
    idx_used:
      columns: [ used ]
    idx_external_id:
      columns: [ external_id ]
  fields:
    externalId:
      type: string
      column: external_id
      length: 255
      nullable: true
    IBAN:
      column: iban
      type: string
      length: 255
      nullable: false
    used:
      type: boolean
      nullable: false
      default: false
    takenAt:
      type: datetime
      nullable: true
      column: taken_at
  manyToOne:
    customer:
      targetEntity: App\Entity\Customer
      joinColumn:
        name: customer_id
        referencedColumnName: id
        nullable: true
        unique: false

    purchase:
      targetEntity: App\Entity\Purchase
      joinColumn:
        name: purchase_id
        referencedColumnName: id
        nullable: true
        unique: false

该实体引发此类错误的根本原因是因为它是joinColumnas的一部分targetEntity

App\Entity\Booking:
  type: entity
  table: booking
  id:
    id:
      type: bigint
      generator:
        strategy: AUTO

  manyToOne:
    bankAccount:
      targetEntity: App\Entity\BankAccount
      joinColumn:
        name: bank_account_id
        referencedColumnName: id
        nullable: true
        unique: false

为了使这个manyToOne -Relation 起作用,还需要做什么?

根据“ composer show -l”的版本:

doctrine/annotations                            1.13.1    1.13.1    Docblock Annotations Parser
doctrine/cache                                  2.0.3     2.0.3     PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.
doctrine/collections                            1.6.7     1.6.7     PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.
doctrine/common                                 3.1.2     3.1.2     PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better reflection support, proxies and much more.
doctrine/data-fixtures                          1.5.0     1.5.0     Data Fixtures for all Doctrine Object Managers
doctrine/dbal                                   2.13.2    3.1.1     Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
doctrine/deprecations                           v0.5.3    v0.5.3    A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages.
doctrine/doctrine-bundle                        2.4.2     2.4.2     Symfony DoctrineBundle
doctrine/doctrine-migrations-bundle             3.1.1     3.1.1     Symfony DoctrineMigrationsBundle
doctrine/event-manager                          1.1.1     1.1.1     The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.
doctrine/inflector                              2.0.3     2.0.3     PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural forms of words.
doctrine/instantiator                           1.4.0     1.4.0     A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                                  1.2.1     1.2.1     PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.
doctrine/migrations                             3.2.0     3.2.0     PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily deploying changes to ...
doctrine/orm                                    2.9.3     2.9.3     Object-Relational-Mapper for PHP
doctrine/persistence                            2.2.1     2.2.1     The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share.
doctrine/sql-formatter                          1.1.1     1.1.1     a PHP SQL highlighting library
d

标签: phpdoctrine-ormdoctrine

解决方案


推荐阅读