2012-07-09 119 views
2

我有一些麻烦与多级继承:Doctrine2多层次继承

第一级:

/** 
* @ORM\Table(name="request") 
* @ORM\Entity 
* @ORM\InheritanceType("JOINED") 
* @ORM\DiscriminatorColumn(name="discr", type="string") 
* @ORM\DiscriminatorMap({"base" = "Base", 
*      "delete" = "Delete", 
*      "contact" = "Contact"}) 
*/ 
class Requete 
{ 

第二级:

/** 
* @ORM\Table(name="base") 
* @ORM\Entity 
* @ORM\InheritanceType("JOINED") 
* @ORM\DiscriminatorColumn(name="discr", type="string") 
* @ORM\DiscriminatorMap({"base" = "Base", 
*      "prosante" = "ProSante", 
*      "pharmacie" = "Pharmacie", 
*      "hopital" = "Hopital"}) 
*/ 

abstract class Base extends Requete 
{ 

第三级:

/** 
* @ORM\Table(name="prosante") 
* @ORM\Entity 
*/ 

class ProSante extends Base 
{ 

当我t时记录日志ry插入一个新的“ProSante”:

INSERT INTO request (discr) VALUES (?) ({"1":"prosante"}) 
INSERT INTO prosante (id) VALUES (?) ({"1"}) 

它应该“插入基地...”之前,但它没有。 字段discr只在请求表中,不在基表中,我不知道为什么。

如果有人有想法。

在此先感谢,

回答

4

我不知道什么是错。在试图复制你的问题,我不能。

进入一个symfony的2.0.15项目,我用下面的实体

<?php 

namespace Peter\SandboxBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Table(name="request") 
* @ORM\Entity 
* @ORM\InheritanceType("JOINED") 
* @ORM\DiscriminatorColumn(name="discr", type="string") 
* @ORM\DiscriminatorMap({"base" = "Base"}) 
*/ 
class Requete 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    protected $discr; 

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

    /** 
    * Set discr 
    * 
    * @param string $discr 
    */ 
    public function setDiscr($discr) 
    { 
     $this->discr = $discr; 
    } 

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

/** 
* @ORM\Table(name="base") 
* @ORM\Entity 
* @ORM\InheritanceType("JOINED") 
* @ORM\DiscriminatorColumn(name="discr", type="string") 
* @ORM\DiscriminatorMap({"base" = "Base", 
*      "prosante" = "ProSante"}) 
*/ 
abstract class Base extends Requete {} 

/** 
* @ORM\Table(name="prosante") 
* @ORM\Entity 
*/ 

class ProSante extends Base {} 

然后安装了DDL,它看起来像这样(由doctrine:schema:update生产)

CREATE TABLE request (id INT AUTO_INCREMENT NOT NULL, discr VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; 
CREATE TABLE base (id INT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; 
CREATE TABLE prosante (id INT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; 
ALTER TABLE base ADD CONSTRAINT FK_C0B4FE61BF396750 FOREIGN KEY (id) REFERENCES request(id) ON DELETE CASCADE; 
ALTER TABLE prosante ADD CONSTRAINT FK_420DF702BF396750 FOREIGN KEY (id) REFERENCES request(id) ON DELETE CASCADE 

再发一个简单的命令来测试插入

// ... 

protected function execute(InputInterface $input, OutputInterface $output) 
{ 
    $p = new ProSante(); 
    $em = $this->getContainer()->get('doctrine')->getEntityManager(); 

    $em->persist($p); 
    $em->flush(); 

    $output->writeln('All done'); 
} 

// ... 

当我看到“全部完成”控制台,然后我检查了数据库直接,其结果被粘贴下面

mysql> select * from prosante; 
+----+ 
| id | 
+----+ 
| 1 | 
+----+ 
1 row in set (0.00 sec) 

mysql> select * from base; 
+----+ 
| id | 
+----+ 
| 1 | 
+----+ 
1 row in set (0.00 sec) 

mysql> select * from request; 
+----+----------+ 
| id | discr | 
+----+----------+ 
| 1 | prosante | 
+----+----------+ 
1 row in set (0.00 sec) 

不知道在哪里可以从这里走。

+0

好吧,这很奇怪,我将删除我的模式并重新创建它们。 – nlaille 2012-07-10 07:53:33

+1

好的,问题解决了,当我更新我的架构原则并没有改变prosante外键。我删除了我的数据库并重新创建它。非常感谢。 – nlaille 2012-07-10 08:05:35