【问题标题】:doctrine schema not creating inherited properties学说模式不创建继承的属性
【发布时间】:2023-04-09 02:30:01
【问题描述】:

我有两个类,AbstractProject 和 Project。 AbstractProject 是一个抽象类,由 Project 扩展而来。当我运行教义:架构:验证时,它说我的架构很好,即使我的项目表只有一个 Id 字段。一切工作正常,直到几天前我经历并重命名了几个类,但现在我似乎无法获得原则来为继承的属性生成数据库列。我的树枝模板毫无怨言地访问了这些属性(它们是空白的,但不会抱怨属性或方法不存在),我可以在父类中使用继承的属性,但学说拒绝识别它们。下面的类和映射,感谢另一双眼睛,也许我错过了一些简单的东西。

*如果有帮助,当我尝试为 dpProjectBundle(AbstractProject 所在的位置)生成实体时,我收到错误消息:“Bundle “dpProjectBundle”不包含任何映射的实体。”我不确定它是否应该将 mappedSuperclasses 视为映射实体,但这也许是问题的征兆?

*在粘贴代码时,我注意到我缺少“使用 Doctrine\ORM\Mapping 作为 ORM;”在 Project 类中,添加该行后我的架构仍然有效,但也许这是个问题?我还尝试清除缓存,以及教义:缓存:清除元数据命令

已解决* 在我的 config.yml 中,我使用了多个实体管理器,并且我还没有将 dpProjectBundle(AbstractProject) 与实体管理器相关联。一旦我这样做了,我就意识到我的架构是无效的,当我运行更新时,我的列现在就存在了。

orm:
    default_entity_manager: dp
    entity_managers:
        dp:
            connection: dp
            mappings:
                dpProjectBundle: ~ // <-- this line was missing, and caused my problems
                devUserBundle: ~
                devSecurityBundle: ~
                devProjectBundle: ~
                devWebsiteBundle: ~
        local:
            connection: local
            mappings:

项目:

namespace dev\ProjectBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use dp\ProjectBundle\Entity\AbstractProject;
/**
 * Project
 */
class Project extends AbstractProject
{
    /**
     * @var integer
     */
    protected $id;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    protected $tasks;

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
        $this->tasks = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add tasks
     *
     * @param \dev\ProjectBundle\Entity\Task $tasks
     * @return Project
     */
    public function addTask(\dev\ProjectBundle\Entity\Task $tasks)
    {
        $this->tasks[] = $tasks;

        return $this;
    }

    /**
     * Remove tasks
     *
     * @param \dev\ProjectBundle\Entity\Task $tasks
     */
    public function removeTask(\dev\ProjectBundle\Entity\Task $tasks)
    {
        $this->tasks->removeElement($tasks);
    }

    /**
     * Get tasks
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getTasks()
    {
        return $this->tasks;
    }
}

抽象项目:

namespace dp\ProjectBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * AbstractProject
 */
abstract class AbstractProject
{

    public function __construct()
    {
        $this->createdAt = new \DateTime();
    }
    /**
     * @var string
     */
    protected $name;

    /**
     * @var \DateTime
     */
    protected $createdAt;

    /**
     * @var \DateTime
     */
    protected $completedAt;


    /**
     * Set name
     *
     * @param string $name
     * @return AbstractProject
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return AbstractProject
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set completedAt
     *
     * @param \DateTime $completedAt
     * @return AbstractProject
     */
    public function setCompletedAt($completedAt)
    {
        $this->completedAt = $completedAt;

        return $this;
    }

    /**
     * Get completedAt
     *
     * @return \DateTime 
     */
    public function getCompletedAt()
    {
        return $this->completedAt;
    }
    /**
     * @var string
     */
    protected $description;


    /**
     * Set description
     *
     * @param string $description
     * @return AbstractProject
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }
}

项目映射:

dev\ProjectBundle\Entity\Project:
    type: entity
    table: dev_projects
    oneToMany:
        tasks:
            targetEntity: Task
            mappedBy: project
            cascade: [persist, remove]
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    lifecycleCallbacks: {  }

抽象项目映射:

dp\ProjectBundle\Entity\AbstractProject:
    type: mappedSuperclass
    fields:
        name:
            type: string
            length: 255
        description:
            type: text
            nullable: true
        createdAt:
            type: datetime
        completedAt:
            type: datetime
            nullable: true
    lifecycleCallbacks: {  }

【问题讨论】:

  • 不要链接代码,请直接粘贴。粘贴你的两个实体:我有一个想法
  • 帖子中包含的课程,感谢您的评论
  • 造成问题的属性是什么?
  • 废话,我想通了,在我的 config.yml 中我使用了多个实体管理器,我还没有将 dpProjectBundle(AbstractClass) 与实体管理器相关联。一旦我这样做了,我就意识到我的架构是无效的,当我运行更新时,我的列现在就存在了。

标签: php symfony orm doctrine


【解决方案1】:

在我的 config.yml 中,我使用了多个实体管理器,但我还没有将 dpProjectBundle(AbstractProject) 与实体管理器相关联。一旦我这样做了,我就意识到我的架构是无效的,当我运行更新时,我的列现在就存在了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多