2
votes

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,

1

1 Answers

4
votes

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.