Reputation: 2652
Can't wrap my head around it. I more or less copied from the tutorial, but the profiler throws two errors:
AppBundle\Entity\Brand The association AppBundle\Entity\Brand#devices refers to the owning side field AppBundle\Entity\Device#brands which does not exist.
AppBundle\Entity\Device The association AppBundle\Entity\Device#brand refers to the inverse side field AppBundle\Entity\Brand#brands which does not exist.
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;
}
and
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;
}
Upvotes: 5
Views: 9104
Reputation: 628
Defining the associations as private wasn't working but once I changed them to protected, it worked. Though the most important thing to note is that the associations defined in your classes is what should be used in the inversedby and mappedby attributes and not the class name or table name. Here is a useful post: What is the difference between inversedBy and mappedBy?
Upvotes: 1
Reputation: 1800
Haven't tested it, but according to docs, it should look something like this
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;
}
and
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;
}
Upvotes: 7