Doctrine2多级继承 [英] Doctrine2 Multiple level inheritance

查看:128
本文介绍了Doctrine2多级继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



第一级:

 

code> / **
* @ ORM\Table(name =request)
* @ ORM\Entity
* @ ORM\InheritanceType(JOINED)
* @ ORM\DiscriminatorColumn(name =discr,type =string)
* @ ORM\DiscriminatorMap({base=Base,
*delete =删除,
*contact=联系人})
* /
class Requete
{
pre>

第二级:

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

abstrac t类基础扩展Requete
{

第三级:

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

class ProSante extends Base
{

我试图插入一个新的ProSante:

  INSERT INTO请求(discr)VALUES(?)({1 pros})
INSERT INTO pros ante(id)VALUES(?)({1})

之前应该做INSERT INTO base ...,但它不会。
字段discr仅在请求表中,不在基表中,我不知道为什么。



如果有人有想法。



提前感谢

解决方案

我不知道是怎么回事。在尝试复制你的问题我不能。



在symfony 2.0.15项目中,我使用了以下实体

 <?php 

命名空间Peter \SandboxBundle\Entity;

使用Doctrine\ORM\Mapping作为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;

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

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

/ **
*获取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})
* /
抽象类Base扩展Requete {}

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

class ProSante extends Base {}

然后安装DDL,看起来像这样(由 doctrine:schema:update

  CREATE TABLE请求(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函数execute(InputInterface $ input,OutputInterface $ output)
{
$ p = new ProSante();
$ em = $ this-> getContainer() - > get('doctrine') - > getEntityManager();

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

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

// ...

当我看到所有完成在控制台中,然后我直接检查数据库,其结果粘贴在

  mysql>选择* from prosante; 
+ ---- +
| id |
+ ---- +
| 1 |
+ ---- +
1行集(0.00秒)

mysql>从基地选择*
+ ---- +
| id |
+ ---- +
| 1 |
+ ---- +
1行集(0.00秒)

mysql>从请求中选择*
+ ---- + ---------- +
| id | discr |
+ ---- + ---------- +
| 1 | pros ante |
+ ---- + ---------- +
1行集(0.00秒)

不知道从哪里去。


I am having some troubles with multiple level inheritance :

First level :

/**
 * @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
{

Second level :

/**
 * @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
{ 

Third level :

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

class ProSante extends Base
{

Logs when I try to insert a new "ProSante":

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

It should do "INSERT INTO base ..." before but it doesn't. The field discr is only in request table, not in base table, I don't know why.

If anyone has an idea.

Thanks in advance,

解决方案

I'm not sure what's wrong. In trying to duplicate your problem I couldn't.

Into a symfony 2.0.15 project, I used the following entities

<?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 {}

And then installed the DDL, which looked like this (as produced by 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

And then made a simple command to test the insert

// ...

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

// ...

When I saw "All done" in the console, I then checked the database directly, the results of which are pasted below

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)

Not sure where to go from here.

这篇关于Doctrine2多级继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆