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.

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)