我在多级继承方面遇到了一些麻烦:
第一级:
/**
* @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
{
当我尝试插入新的“ProSante”时记录:
INSERT INTO request (discr) VALUES (?) ({"1":"prosante"})
INSERT INTO prosante (id) VALUES (?) ({"1"})
它之前应该执行“INSERT INTO base ...”,但事实并非如此。 字段discr仅在请求表中,在基表中没有,我不知道为什么。
如果有人有想法。
提前致谢,
最佳答案
我不确定出了什么问题。在尝试复制你的问题时我做不到。
在 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)
不知道该去哪里。
关于php - Doctrine2 多级继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11397255/