Yoot
Yoot

Reputation: 657

Doctrine generation of OneToMany relations

EDIT

Let's take the following model :

enter image description here

The following entities are generated with the command doctrine:mapping:import :

class Toto
{
    /**
     * @var integer $idtoto
     *
     * @ORM\Column(name="idtoto", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idtoto;



    /**
     * Get idtoto
     *
     * @return integer 
     */
    public function getIdtoto()
    {
        return $this->idtoto;
    }
}

class Tata
{
    /**
     * @var integer $idtata
     *
     * @ORM\Column(name="idtata", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idtata;

    /**
     * @var Toto
     *
     * @ORM\ManyToOne(targetEntity="Toto")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="idtoto", referencedColumnName="idtoto")
     * })
     */
    private $idtoto;



    /**
     * Get idtata
     *
     * @return integer 
     */
    public function getIdtata()
    {
        return $this->idtata;
    }

    /**
     * Set idtoto
     *
     * @param Creatis\SaisieBundle\Entity\Toto $idtoto
     */
    public function setIdtoto(\Creatis\SaisieBundle\Entity\Toto $idtoto)
    {
        $this->idtoto = $idtoto;
    }

    /**
     * Get idtoto
     *
     * @return Creatis\SaisieBundle\Entity\Toto 
     */
    public function getIdtoto()
    {
        return $this->idtoto;
    }
}

Why is the relation OneToMany in class Toto NOT generated (had to type it by hand) :

/**
 * @ORM\OneToMany(targetEntity="Tata", mappedBy="idToto")
 */
private $tatas;

Is there an option to put in the command line ?

Upvotes: 0

Views: 1368

Answers (2)

Dolly Aswin
Dolly Aswin

Reputation: 2794

This is limitation from Doctrine 2. It is only solves about 70-80% of the necessary mapping information. Please check this http://doctrine-orm.readthedocs.org/en/2.0.x/reference/tools.html#convert-mapping-information

Upvotes: 0

Sgoettschkes
Sgoettschkes

Reputation: 13214

Within leftJoin, you just have to use the name of your relation attribute as defined in the Tata class. The second parameter only defines the key you can use in the statement. If your relations are correct, doctrine does the join automatically:

class TataRepository extends EntityRepository{
  public function getRelation(){
    $qb = $this->createQueryBuilder('tata')
      ->leftJoin('tata.idtoto', 'toto');
    return $qb->getQuery()->getResult();
  }
}

If you don't want to join with the foreign key but some other fields, you have to add a thrid parameter to leftJoin which does the matching, like toto.field1 = tata.field2.

Upvotes: 1

Related Questions