Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46198

Doctrine mapping import global namespace for associations

Trying to import a mapping from my database

$ php bin/console doctrine:mapping:import MyBundle annotation

Here is one of my associations generated from a foreign key by Doctrine

/**
 * @var \CustomerSite
 *
 * @ORM\Id
 * @ORM\OneToOne(targetEntity="CustomerSite")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="customer_site_id", referencedColumnName="id")
 * })
 */
private $customerSite;

As you can see the field is referenced in the global namespace

@var \CustomerSite

It should be

@var CustomerSite

Why does doctrine use the global namespace here ? How do I tell it not to ?

Upvotes: 1

Views: 181

Answers (2)

Ralf Jahr
Ralf Jahr

Reputation: 331

I use the following two commands before creating getters/setters:

sed -i'' -e 's/@var \\/@var /g' src/Entity/*.php
sed -i'' -e 's/@var Datetime/@var \\Datetime/g' src/Entity/*.php

Upvotes: 0

chalasr
chalasr

Reputation: 13167

There is a lot of such things that could be greatly improved in the EntityGenerator and related utilities. And no, there is nothing to get around this, you just have to change it manually.

An other common example, if you have the two following entities:

namespace AppBundle\Entity;

class Product
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Tag")
     */
    private $tags;
}

namespace AppBundle\Entity;

class Tag
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
}

Then, if you execute doctrine:generate:entities AppBundle:Product, the generated result will be:

/**
 * Add tag
 *
 * @param \App\SportBundle\Entity\Tag $tag
 *
 * @return Sport
 */
public function addTag(\App\SportBundle\Entity\Tag $tag)
{
    $this->tags[] = $tag;

    return $this;
}

/**
 * Remove tag
 *
 * @param \App\SportBundle\Entity\Tag $tag
 */
public function removeTag(\App\SportBundle\Entity\Tag $tag)
{
    $this->tags->removeElement($tag);
}

/**
 * Get tags
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getTags()
{
    return $this->tags;
}

But it should be:

/**
 * Add tag
 *
 * @param Tag $tag
 *
 * @return Sport
 */
public function addTag(Tag $tag)
{
    $this->tags[] = $tag;

    return $this;
}

/**
 * Remove tag
 *
 * @param Tag $tag
 */
public function removeTag(Tag $tag)
{
    $this->tags->removeElement($tag);
}

/**
 * Get tags
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getTags()
{
    return $this->tags;
}

I think that some simple checks are missing in the entity generation behavior.
I already proposed a PR about a totally different problem (methods naming of the generated entities for non-plural members) but that is part of the same behavior. After almost a month, nothing happens.

But I think this issue would be more considered because of it's a common and real problem that the generated typehints/phpdocs are not the expected.

Please keep us informed if you open one.

Upvotes: 1

Related Questions