Patryk Karczmarczyk
Patryk Karczmarczyk

Reputation: 55

OneToMany Doctrine Symfony2 relationship issue

I have got a problem with OneToMany relation, I have user which can donate blood so he can have many blood donations, my application works fine but in my profiler I have 2 errors.

From Users Entity:

The association AppBundle\Entity\Users#userDonation refers to the owning side field AppBundle\Entity\UserDonates#id which is not defined as association, but as field.
The association AppBundle\Entity\Users#userDonation refers to the owning side field AppBundle\Entity\UserDonates#id which does not exist.

and from UserDonates:

The mappings AppBundle\Entity\UserDonates#userId and AppBundle\Entity\Users#userDonation are inconsistent with each other.

Here are my entities:

UserDonates:

 /**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @ORM\ManyToOne(targetEntity="Users", inversedBy="userDonation")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
protected $userId;
/**
 * @var string
 *
 * @ORM\Column(name="place", type="string", length=255)
 */
private $place;

/**
* @var date
* @ORM\Column(name="donation_date", type="date")
*/
private $donation_date;

   /**
* @var string
* @ORM\Column(name="donation_type", type="string", length=255)
*/
private $donation_type;


/**
* @var integer
* @ORM\Column(name="blod_donated", type="integer")
*/

private $blood_donated;

Users:

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

    /**
     * @ORM\OneToMany(targetEntity="UserDonates", mappedBy="id", cascade={"persist","remove"})
     */
    protected $userDonation;

    /**
    * @ORM\OneToOne(targetEntity="UserInfo", cascade={"persist","remove"})
    */
    private $profil;  

//__construct() from FOSUserBundle
  public function __construct(){
       parent::__construct();
   }

Users entity is also related to UserInfo with OneToOne relation.

Upvotes: 1

Views: 234

Answers (1)

helmbert
helmbert

Reputation: 38034

I see two issues here.

The association AppBundle\Entity\Users#userDonation refers to the owning side field AppBundle\Entity\UserDonates#id which is not defined as association, but as field.

The inverse side of your User::$userDonation association is not the id field, but the userId field in the UserDonation entity. The mapping should look like this:

/**
 * Here! -----------------------------------------------v
 * @ORM\OneToMany(targetEntity="UserDonates", mappedBy="userId", cascade={"persist","remove"})
 */
protected $userDonation;

As a side note, I'd suggest naming the userId attribute user instead; after all it will contain an actual user object, and not just a user's ID.

The association AppBundle\Entity\Users#userDonation refers to the owning side field AppBundle\Entity\UserDonates#id which does not exist.

Your UserDonates::$id attribute is private. All properties managed by Doctrine need to be protected in order for Doctrine to be able to populate them with data.

Upvotes: 3

Related Questions