【问题标题】:Doctrine doesn't generate inherited-class's propertiesDoctrine 不会生成继承类的属性
【发布时间】:2016-05-26 20:12:44
【问题描述】:

我有一个抽象的 MappedSuperClass 和另一个名为“User”的类,它是这个 MappedSuperClass 的子类。问题是教义不会生成 User 类的属性。只有 MappedSuperClass 的属性。为什么?

<?php
namespace IsCoconut\Models;
use DateTime;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\MappedSuperclass;


/** @MappedSuperclass */
abstract class BaseEntity
{
    /**
     * @var int
     * @Id 
     * @Column(type="integer") 
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @Column(type="boolean")
     *  @var boolean
     */
    protected $deleted = false;

    /** @Column(name="creation_date_time", type="datetime")
     *  @var DateTime
     */
    protected $creationDateTime;

    protected function __construct()
    {
        $this->creationDateTime = new DateTime();
    }
}

这是我的实体,应该由 Doctrine 在数据库中生成

<?php
namespace IsCoconut\Models;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;

/**
 * @Entity
 * @Table(name="users")
 */
class User extends BaseEntity
{
    /**
     * @var string
     * @ORM\Column(type="string", length=512)
     */
    private $forename;

    /**
     * @var string
     * @ORM\Column(type="string", length=512)
     */
    private $surname;

    /**
     * @var string
     * @ORM\Column(type="string", length=512, unique=true, nullable=false)
     */
    private $email;

    /**
     * @var string
     * @ORM\Column(type="string", length=512, unique=true, nullable=false)
     */
    private $passwordHash;
}

这是doctrine orm:schema-tool:create --dump-sql的输出

CREATE TABLE users (id INT NOT NULL, deleted BOOLEAN NOT NULL, creation_date_time TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id));
CREATE SEQUENCE users_id_seq INCREMENT BY 1 MINVALUE 1 START 1;

缺少用户的类实体属性!为什么?

【问题讨论】:

  • 如果你将属性设为protected会发生什么?
  • 什么都没有。我已经试过了
  • 您在 User 类文件中的“使用”语句是什么?
  • 使用 Doctrine\ORM\Mapping 作为 ORM;使用 Doctrine\ORM\Mapping\Entity;使用 Doctrine\ORM\Mapping\Table;
  • 我还注意到在 User 类文件中你有 @ORM\Column'... Maybe you should change everything to @ORM` 以使其保持一致?某处可能存在错误。

标签: php postgresql symfony orm doctrine-orm


【解决方案1】:

试试这个

* @ORM\Entity
* @ORM\Table(name="User")

【讨论】:

  • 这也没有用
【解决方案2】:

实际上,这可能是解决方案:

改变:

abstract class BaseEntity

到:

class BaseEntity

看看这是否有效。可能不行,请尝试一下。

【讨论】:

  • 您是否也将 User 类 @Entity 更改为 @ORM\Entity
  • 是的,我做到了,但没有必要因为use Doctrine\ORM\Mapping\Entity;
【解决方案3】:

当我删除所有 @ORM\ 前缀后,它现在可以工作了

【讨论】:

    【解决方案4】:

    我遇到了同样的问题,发现是因为属性设置为私有。

    /**
     * @ORM\MappedSuperclass
     */
    class BaseClass
    {
    
        /**
         * @var string
         * @ORM\Column(type="string", length=250, nullable=false)
         */
        protected $channel;
        //private $channel;
     }
    

    【讨论】:

      【解决方案5】:

      抽象类上的映射应该是* @ORM\MappedSuperclass 而不是/** @MappedSuperclass */。映射是由 Doctrine 完成的,所以 ORM 注释在这里很重要。 您还需要确保抽象类中的属性是protected(您已经说过它们是)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-25
        • 1970-01-01
        • 2017-10-07
        • 1970-01-01
        • 2019-01-02
        • 2013-09-02
        • 2023-03-06
        • 2014-02-03
        相关资源
        最近更新 更多