尝试从我的数据库导入映射
$ 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 在这里使用全局命名空间?我怎么告诉它不要?
最佳答案
在 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/phpdocs 不是预期的,这是一个常见且真实的问题。
如果您打开一个,请通知我们。
关于namespaces - Doctrine 映射为关联导入全局命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36788009/