Symfony2 doctrine ManyToMany关联映射无效?

时间:2015-10-16 14:20:19

标签: symfony doctrine-orm mapping entity

我有两个与此问题相关的实体,我希望连接表能够在每个表中交叉引用值。

以下是解释:

实体ContainerType.php:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\Containers;

/**
 * ContainerType
 *
 * @ORM\Table(name="container_type")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 */
class ContainerType
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="type", type="string", length=255)
     */
    private $type;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_added", type="datetime")
     */
    private $dateAdded;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_modified", type="datetime", nullable=true)
     */
    private $dateModified;

    /**
     * @ORM\ManyToMany(targetEntity="Containers", inversedBy="type")
     * @ORM\JoinTable(name="container_type_containers")
     **/
    private $container;


    public function __construct()
    {
        $this->container = new ArrayCollection();
    }

实体Containers.php:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\ContainerType;

/**
 * Containers
 *
 * @ORM\Table(name="containers")
 * @ORM\Entity
 */
class Containers
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="number", type="string", length=255)
     */
    private $number;

    /**
     * @ORM\ManyToMany(targetEntity="Containers", mappedBy="container")
     */
    private $type;


    public function __construct()
    {
        $this->type = new ArrayCollection();
    }

虽然架构更新没有问题,但当我做一个学说:架构:验证我得到以下失败:

    [Mapping]  FAIL - The entity-class 'AppBundle\Entity\Containers' mapping is invalid:
* The association AppBundle\Entity\Containers#type refers to the owning side field AppBundle\Entity\Containers#container which does not exist.

但是$ Container字段存在于ContainerType中,所以我不明白为什么它试图在Containers实体中引用一个名为container的字段?

任何人都可以对此有所了解吗?

谢谢 迈克尔

2 个答案:

答案 0 :(得分:1)

我认为这段代码应该适合你:)

ContainerType.php

    /**
     * @ORM\ManyToMany(targetEntity="Containers", inversedBy="containersType")
     * @ORM\JoinTable(name="container_type_containers",
     *      joinColumns={@ORM\JoinColumn(name="container_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="container_type_id", referencedColumnName="id")})
     */
    protected $containers;

Containers.php

    /**
     * @ORM\ManyToMany(targetEntity="ContainerType", mappedBy="containers")
     */
    protected $containersType;

答案 1 :(得分:0)

我刚刚意识到我的错误!

我在Containers.php文件中使用了错误的目标实体,我应该使用ContainerType作为目标,而不是容器,这就是为什么它试图在错误的表中找到该字段的原因!