Doctrine映射导入关联的全局命名空间

时间:2016-04-22 07:50:53

标签: namespaces doctrine symfony

尝试从我的数据库导入映射

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

这是我通过Doctrine

从外键生成的关联之一
/**
 * @var \CustomerSite
 *
 * @ORM\Id
 * @ORM\OneToOne(targetEntity="CustomerSite")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="customer_site_id", referencedColumnName="id")
 * })
 */
private $customerSite;

如您所见,该字段在全局命名空间中被引用

@var \CustomerSite

应该是

@var CustomerSite

为什么doctrine在这里使用全局命名空间?我怎么告诉它不要?

1 个答案:

答案 0 :(得分:1)

EntityGenerator及相关实用程序中有很多可以大大改进的东西。 不,没有什么可以解决这个问题,你只需要手动更改它。

另一个常见示例,如果您有以下两个实体:

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;
}

然后,如果执行doctrine:generate:entities AppBundle:Product,生成的结果将为:

/**
 * 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;
}

但它应该是:

/**
 * 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;
}

我认为实体生成行为中缺少一些简单的检查 我已经提出了PR关于一个完全不同的问题(为非复数成员命名生成实体的方法),但这是同一行为的一部分。差不多一个月后,没有任何事情发生。

但我认为这个问题会更加考虑,因为生成的typehints / phpdoc不是预期的常见问题。

如果您打开一个,请随时通知我们。

相关问题