douzi9211 2014-11-18 06:39
浏览 39
已采纳

doctrine模式不创建继承属性

I have two classes, AbstractProject and Project. AbstractProject is an abstract class, and is extended by Project. When I run doctrine:schema:validate it says my schema is fine, even though my Project table only has an Id field. Everything was working fine until a couple days ago I went through and renamed a few classes, but now I can't seem to get doctrine to generate the database columns for the inherited properties. My twig templates access the properties without complaint (they come up blank, but dont complain about the property or method not existing), and I can use the inherited properties in the parent class, yet doctrine refuses to recognize them. Classes and mappings below, appreciate another set of eyes on it maybe I'm missing something simple.

*If it helps, when I try to generate entities for dpProjectBundle (where the AbstractProject lives) I get an error: "Bundle "dpProjectBundle" does not contain any mapped entities." I am not sure if it should consider the mappedSuperclasses as mapped entities, but maybe that is a sign of the problem?

*while pasting the code I noticed I was missing "use Doctrine\ORM\Mapping as ORM;" in the Project class, still says my schema is valid after adding that line, but maybe that was an issue? I also tried clearing the cache, and the doctrine:cache:clear-metadata command

RESOLVED* In my config.yml I am using multiple entity managers, and I had not yet associated the dpProjectBundle(AbstractProject) with an entity manager. Once I did that doctrine realized my schema was invalid and when I ran the update my columns are present now.

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:

Project:

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;
    }
}

AbstractProject:

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;
    }
}

Project Mapping:

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: {  }

AbstractProject Mapping:

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: {  }
  • 写回答

1条回答 默认 最新

  • douzhang8033 2014-11-19 02:25
    关注

    In my config.yml I am using multiple entity managers, and I had not yet associated the dpProjectBundle(AbstractProject) with an entity manager. Once I did that doctrine realized my schema was invalid and when I ran the update my columns are present now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答