Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine issue (mappings inconsistent)

I'm busy with a project in Symfony and I'm just checking the profiler tab and seeing 2 errors continuously popping up - they are below.

The mappings MyBundle\MainBundle\Entity\School#provinceId and MyBundle\MainBundle\Entity\Province#schools are incosistent with each other.

The association MyBundle\MainBundle\Entity\School#grades refers to the owning side field MyBundle\MainBundle\Entity\Grade#school_id which does not exist.

I'm getting a couple more of these and I can't understand why? What does it mean by "incosistent" (see what I did there)? Parts of my code is below if it's helpful.

In Province.php

/**
  * @ORM\OneToMany(targetEntity="School", mappedBy="provinceId")
 */ 
private $schools;

and in my Schools.php

/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="Province", inversedBy="schools")
 * @ORM\JoinColumn(name="province_id", referencedColumnName="id")
 */
private $provinceId;

And for the second error...

School.php

/**
 * @ORM\OneToMany(targetEntity="Grade", mappedBy="school_id")
 */
private $grades;

and Grade.php

/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="School", inversedBy="grades")
 * @ORM\JoinColumn(name="school_id", referencedColumnName="id")
 */
private $schoolId;

I just want to know what these errors mean exactly and why these entities aren't right - I tried following the docs off the doctrine page but apparently I went wrong somewhere!

Thanks for any help!

like image 515
iLikeBreakfast Avatar asked Jan 12 '23 22:01

iLikeBreakfast


1 Answers

I don't have your entire configuration, so I'm just going to make an educated guess here... (forgive me if I'm wrong!)

Regarding the first one, you say the mapping looks like this:

# Province.php
/**
  * @ORM\OneToMany(targetEntity="School", mappedBy="provinceId")
 */ 
private $schools;

# School.php
/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="Province", inversedBy="schools")
 * @ORM\JoinColumn(name="province_id", referencedColumnName="id")
 */
private $provinceId;

I imagine it's the types that are throwing things off here. You see, the purpose of the mappings is so that you can treat these things like objects, without having to worry about how they're persisted/connected in the database. Specifically, in your case, a School entity should not have a member $provinceId of type integer; rather, it should have a $province of type Province.

Try this:

# Province.php
/**
  * @ORM\OneToMany(targetEntity="School", mappedBy="province")
 */ 
private $schools;

# School.php
/**
 * @var Province
 *
 * @ORM\ManyToOne(targetEntity="Province", inversedBy="schools")
 * @ORM\JoinColumn(name="province_id", referencedColumnName="id")
 */
private $province;

(Again, this is wholly untested, and I only have a part of what you have... but I think this will get you closer.)

like image 59
Thomas Kelley Avatar answered Jan 16 '23 22:01

Thomas Kelley