Doctrine fails with a simple bi-directional many-to-one relationship between FoodDes (many) and FoodGroup (one). Both entities are shown here:
/**
* @ORM\Entity
* @ORM\Table(name="FOOD_DES")
*/
class FoodDes
{
public function __construct()
{
$this->foodGroup = new ArrayCollection();
}
/**
* @ORM\Id
* @ORM\Column(name="NDB_No", type="string", length=10)
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="FoodGroup", inversedBy="fdGroupCode")
* @ORM\JoinColumn(name="FdGrp_Cd", referencedColumnName="FdGrp_CD")
*/
protected $foodGroup;
}
>
/**
* @ORM\Entity
* @ORM\Table(name="FD_GROUP")
*/
class FoodGroup
{
/**
* @ORM\Id();
* @ORM\GeneratedValue(strategy="NONE");
* @ORM\OneToMany(targetEntity="FoodDes", mappedBy="foodGroup")
*/
protected $fdGroupCode;
When I run doctrine orm:schema-tool:create, it fails with error:
No identifier/primary key specified for Entity 'Acme\Entities\FoodGroup'. Every Entity must have an identifier/primary key.
However, I labeled $fdGroupCode as my only identifier.
Next approach
I've also tried creating a new primary key $id on the FoodGroup entity and removing the primary key label from $fdGroupCode on FoodGroup. Below is the new FoodGroup entity.
/**
* @ORM\Entity
* @ORM\Table(name="FD_GROUP")
*/
class FoodGroup
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false)
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="FoodDes", mappedBy="foodGroup")
*/
protected $fdGroupCode;
When I run doctrine orm:schema-tool:create again, it results with a new error:
[Doctrine\ORM\ORMException]
Column nameFdGrp_CD
referenced for relation from Acme\Entities\FoodDes towards Acme\Entities\FoodGroup does not exist.
This error doesn't make any sense. Of course it wouldn't exist. I am running it against an empty database!
These error occur running from the command line, but they also occur when querying the entities against a database. Can somebody please help me?
I'd rather give you working example of OneToMany from one of my projects, so you can see the difference and format code in proper way. If it does not work, then try to get a new
Symfony
dist and start over.Related entity:
For reasons why your code does not work could be many (namespaces, folder structure, column names, etc.). This example works and tested. Give it a try : )