我已尝试按照Doctrines手册上的说明操作: http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-self-referencing
我正在尝试设置一个页面实体,它有许多子页面,我也希望能够访问页面parentPage。
按照上面的说明,symfony2告诉我,由于语义错误,我需要设置“使用”。
有人可以告诉我该做些什么来让我这样做,因为我很困难。
示例代码是:
namespace Pages\Bundle\PageBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Page
*
* @ORM\Table(name="Page")
* @ORM\Entity(repositoryClass="Pages\Bundle\PageBundle\Entity\PageRepository")
*/
class Page
{
/**
* Constructor
*/
public function __construct()
{
$this->subPages = new \Doctrine\Common\Collections\ArrayCollection();
$this->parentPages = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @ManyToMany(targetEntity="Page", mappedBy="subPages")
**/
private $parentPages;
/**
* @ManyToMany(targetEntity="Page", inversedBy="parentPages")
* @JoinTable(name="sub_pages",
* joinColumns={@JoinColumn(name="parent_page_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="sub_page_id", referencedColumnName="id")}
* )
**/
private $subPages;
... (其他变量如下,但是是内容/元数)
运行时的错误响应是:
[Semantical Error] The annotation "@ManyToMany" in property
Pages\Bundle\PageBundle\Entity\Page::$parentPages was never imported. Did you maybe forget to add a "use" statement for this annotation?
答案 0 :(得分:16)
尝试使用@ORM\ManyToMany
而非@ManyToMany
(还有@ORM\JoinTable
)