学说:指拥有不存在的副场(再次)

blu*_*isk 4 symfony doctrine-orm

无法绕过它.我或多或少地从教程中复制了,但是分析器抛出了两个错误:

AppBundle\Entity\Brand关联AppBundle\Entity\Brand#devices是指不存在的拥有方字段AppBundle\Entity\Device#brands.

AppBundle\Entity\Device关联AppBundle\Entity\Device #brade是指不存在的反面字段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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

lch*_*ski 7

没有测试过,但根据文档,它看起来应该是这样的

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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 不,这是内部学说,请查看http://stackoverflow.com/a/34583366/3275814 (2认同)