【问题标题】:Symfony2 doctrine ManyToMany associations mapping invalid?Symfony2学说ManyToMany关联映射无效?
【发布时间】:2015-10-16 14:20:19
【问题描述】:

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

这里有一个解释:

实体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 的字段?

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

谢谢 迈克尔

【问题讨论】:

    标签: symfony doctrine-orm mapping entity


    【解决方案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;
    

    【讨论】:

      【解决方案2】:

      我刚刚意识到我的错误!

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-18
        • 1970-01-01
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多