Dhouard
Dhouard

Reputation: 175

Doctrine relations for parent and child classes

I'm trying to implement a doctrine relation for a symphony 3 app.

I have two different classes, one extending from the other, which are related to the same entity with a many to one relation.

Here are my classes.

Country.php

class Country
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Groups({"exposed"})
     */
     private $id;

     /**
      * @ORM\OneToMany(targetEntity="Link", mappedBy="country")
      */
      private $link;

     /**
      * @ORM\OneToMany(targetEntity="LinkChild", mappedBy="country")
      */
      private $linkChild;

      public function __construct()
      {
          $this->link = new ArrayCollection();
          $this->linkChild = new ArrayCollection();
      }
  }

Link.php

/**
 * Link
 *
 * @ORM\Table(name="link")
 * @ORM\Entity(repositoryClass="Decathlon\AppCollaboratorBundle\Reposito\LinkRepository")
 * @Vich\Uploadable
 * @ORM\HasLifecycleCallbacks()
 */
 class Link
 {
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Serializer\Groups({"link_list", "link_info"})
     * @Serializer\Expose()
     */
     protected $id;

    /**
     * @var Country
     *
     * @ORM\ManyToOne(targetEntity="Country", inversedBy="link", cascade={"persist"})
     * @JoinColumn(name="country_id", referencedColumnName="id")
     */
     protected $country;
}

LinkChild.php

/**
 * @ORM\Entity(repositoryClass="Decathlon\AppCollaboratorBundle\Repository\LinkChildRepository")
 */
class LinkChild extends Link
{

   /**
    * @var Country
    *
    * @ORM\ManyToOne(targetEntity="Country", inversedBy="linkChild", cascade={"persist"})
    * @JoinColumn(name="country_id", referencedColumnName="id")
    */
    protected $country;
}

I need to create a relation between both Link and LinkChild to Country but no country column is created in LinkChild table.

I've told not to use recursive classes so I must create Link and LinkChild.

Is there a way to acomplish what I'm tryng to do.

Thank you in advance.

Upvotes: 0

Views: 1122

Answers (2)

Marnix Bent
Marnix Bent

Reputation: 89

Try renaming your protected $country; variable to something like private $childCountry; to make it a variable that belongs specifically to LinkChild.

Your protected $country; override in LinkChild is ignored because it is exactly the same as the one in Link.

Upvotes: 0

delboy1978uk
delboy1978uk

Reputation: 12355

I think what you are looking for is single table inheritance?

<?php
namespace MyProject\Model;

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
 */
class Person
{
    // ...
}

/**
 * @Entity
 */
class Employee extends Person
{
    // ...
}

Take a look here:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#single-table-inheritance

Upvotes: 1

Related Questions