【问题标题】:Doctrine and Symfony2 ManyToMany: Association refers to inverse side that doesn't exist errorDoctrine and Symfony2 ManyToMany:关联是指不存在错误的反面
【发布时间】:2014-01-31 11:07:03
【问题描述】:

我试图在两个实体之间建立多对多关系 我所需要的只是此处记录的完全相同的代码: 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 其中 不存在。

我一直在寻找几个小时,如果有人有想法,我将不胜感激

【问题讨论】:

  • 我猜你试图清除缓存?您的捆绑包是否都在 AppKernel.php 中定义?在 config.yml 中?
  • 是的,是的,是的,你也不能清除缓存,因为控制台会触发错误。
  • 所以您手动删除了app/cache 中的dev 文件夹?
  • 触发app_dev.php时缓存不会自动重建吗?
  • 现在也试过了,同样的,但我找到了另一种解决问题的方法,所以没关系

标签: php symfony doctrine-orm doctrine


【解决方案1】:

为什么是多对多关系。对我来说,这是一种 OneToMany 关系。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2015-05-14
    • 1970-01-01
    相关资源
    最近更新 更多