0

I've two entities: Act and Birth with Birth which extends Act like that

/**
* Act
*
* @ORM\InheritanceType("JOINED")
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\ActRepository")
*/
class Act
{
   /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
   private $id;

   /**
   * @var string
   *
   * @ORM\Column(name="type", type="string", length=30, nullable=true)
   */
   private $type;
}


/**
* Birth
*
* @ORM\Table(name="birth")
* @ORM\Entity(repositoryClass="AppBundle\Repository\BirthRepository")
*/
class Birth extends Acte
{
   /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
   private $id;
}

And when I create a birth instance

new Birth();

I would like to recover the instance of its super class ie Act Entity ! I have to assign it to another class (Individual) with individual->setAct() and not individu->setBirth() ...

I do not know if it's possible but I want some advice for another approach Thanks !

b-user
  • 97
  • 1
  • 3
  • 10
  • so you want to make an instance of a class to something which it isn't? – LBA Jul 12 '18 at 08:48
  • 0 down vote accept From the birth instance so $birth = new Birth(), I want to recover the instance Act (the super birth class) so $act with a style thing $act = $birth->getSuperClass() or $birth->get(superclass::Act)... – b-user Jul 12 '18 at 08:51
  • please read [this](https://stackoverflow.com/questions/1147109/type-casting-for-user-defined-objects/1147377#1147377) why I don't think that's possible. and it sounds like bad design, too. – LBA Jul 12 '18 at 08:51
  • check this "solution" which tries to [cast an object into a parent class](https://gist.github.com/duaiwe/960035) - I won't propose it as an answer because it sounds too hacky to me. – LBA Jul 12 '18 at 09:00
  • Possible duplicate of [Type casting for user defined objects](https://stackoverflow.com/questions/1147109/type-casting-for-user-defined-objects) – LBA Jul 12 '18 at 09:01
  • Have you try single table inheritance from doctrine ? [Inheritance documentation](http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html) – Charlie Lucas Jul 12 '18 at 13:49

1 Answers1

0

Just do no redefine the $id in the child class, and declare the $id in Act protected.

Then

$birth = new Birth();
if($birth instanceof Act){
    // TRUE!
    // you could access $birth->type if it was public
}
olidem
  • 1,961
  • 2
  • 20
  • 45
  • Yes but I wanted to assign the instance Act generate with the instance of Birth to an individual like this: $ individu-> setAct (Act) $ act) and not $ individu-> setActe ($ birth) – b-user Jul 12 '18 at 13:29
  • 1
    Finally, I change my data model ! – b-user Jul 12 '18 at 13:29