Doctrine和Symfony2 ManyToMany:Association指的是不存在错误的反面

时间:2014-01-31 11:07:04

标签: php symfony doctrine-orm doctrine

我试图在两个实体之间建立多对多的关系 我只需要与此处记录的完全相同的代码: http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional

以下是您不想打开链接的示例代码

<?php
/** @Entity **/
class User
{
    // ...

    /**
     * @ManyToMany(targetEntity="Group", inversedBy="users")
     * @JoinTable(name="users_groups")
     **/
    private $groups;

    public function __construct() {
        $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

/** @Entity **/
class Group
{
    // ...
    /**
     * @ManyToMany(targetEntity="User", mappedBy="groups")
     **/
    private $users;

    public function __construct() {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

这是我的代码:

//My User entity
namespace Gabriel\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

class User extends BaseUser
{
    //...

    /**
     * @ORM\ManyToMany(targetEntity="Gabriel\UploadBundle\Entity\Image", mappedBy="imageowner")
     */
    protected $ownedimage;

    public function __construct()
    {
        $this->ownedimage = new \Doctrine\Common\Collections\ArrayCollection();
    }

    //...
}


//My Image entity

namespace Gabriel\UploadBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

class Image
{
    /**
     * @ORM\ManyToMany(targetEntity="Gabriel\UserBundle\Entity\User", inversedBy="ownedimage")
     * @ORM\JoinTable(name="imageowner_ownedimage")
     */
    protected $imageowner;

    public function __construct()
    {
        $this->imageowner = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

它会触发此错误:

  

关联Gabriel \ UploadBundle \ Entity \ Image#imageowner指的是   反面字段Gabriel \ UserBundle \ Entity \ User#ownedimage which   不存在。

我一直在寻找好几个小时如果有人有想法我会很感激

1 个答案:

答案 0 :(得分:0)

为什么是ManyToMany关系。对我而言,这是OneToMany的关系。