【问题标题】:Doctrine2 Multiple level inheritanceDoctrine2 多级继承
【发布时间】:2012-07-09 14:35:41
【问题描述】:

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

第一级:

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

如果有人有想法。

提前致谢,

【问题讨论】:

    标签: php inheritance symfony doctrine-orm


    【解决方案1】:

    我不确定出了什么问题。在试图复制你的问题时,我做不到。

    进入一个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)
    

    不知道从这里去哪里。

    【讨论】:

    • 嗯,很奇怪,我要删除我的架构并重新创建它们。
    • 好的,问题解决了,当我更新我的架构原则时,并没有改变 prosante 外键。我删除了我的数据库并重新创建它。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多