【问题标题】:doctrine: refers to owning side field which does not exist (again)学说:指拥有不存在的侧场(再次)
【发布时间】:2016-07-12 20:36:33
【问题描述】:

我无法理解它。我或多或少地复制了教程,但分析器抛出了两个错误:

AppBundle\Entity\Brand 关联 AppBundle\Entity\Brand#devices 指拥有方字段 AppBundle\Entity\Device#brands 哪个 不存在。

AppBundle\Entity\Device 关联 AppBundle\Entity\Device#brand 指逆边字段 AppBundle\Entity\Brand#brands 不存在。

class Brand {

    /**
     * @var int
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

...

    /**
     * @ORM\OneToMany(targetEntity="Device", mappedBy="brands")
     */
    private $devices;
}

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

...

    /**
     * @ORM\ManyToOne(targetEntity="Brand", inversedBy="devices")
     * @ORM\JoinColumn(name="brand_id", referencedColumnName="id", nullable=true)
     */
    private $brand;
}

【问题讨论】:

    标签: symfony doctrine-orm


    【解决方案1】:

    尚未测试,但根据文档,它应该看起来像这样

    http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional

    class Brand {
    
        /**
         * @var int
         * @ORM\Column(name="brand_id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
    ...
    
        /**
         * @ORM\OneToMany(targetEntity="Device", mappedBy="brand")
         */
        private $devices;
    }
    

    class Device {
        /**
         * @var int
         * @ORM\Id
         * @ORM\Column(name="id", type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
    ...
    
        /**
         * @ORM\ManyToOne(targetEntity="Brand", inversedBy="devices")
         * @ORM\JoinColumn(name="brand_id", referencedColumnName="id", nullable=true)
         */
        private $brand;
    }
    

    【讨论】:

    【解决方案2】:

    将关联定义为私有不起作用,但一旦我将它们更改为受保护,它就起作用了。尽管需要注意的最重要的事情是类中定义的关联应该在 inversedby 和 mappedby 属性中使用,而不是类名或表名。这是一个有用的帖子:What is the difference between inversedBy and mappedBy?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-01
      • 2023-03-14
      • 1970-01-01
      • 2013-05-22
      • 2015-11-04
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      相关资源
      最近更新 更多