2

我在多级继承方面遇到了一些麻烦:

第一级 :

/**
 * @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仅在请求表中,不在基表中,我不知道为什么。

如果有人有想法。

提前致谢,

4

1 回答 1

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

// ...

当我在控制台看到“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)

不知道从这里去哪里。

于 2012-07-09T19:43:35.273 回答