Doctrine Association Mapping - undefined index
I have users and products. I want the user to be able to add his favourite
products, but when he adds a new product to his favourites, I don't want
to change anything for the user, what I want is the Product entity to has
a variable, called usersWhoLikedMe and it to be an array, so each product
to keep an array to the users, who liked the product.
If possible I want the User class to know nothing about this, so I want to
change only the Product class. The problem is that I don't know how. :(
I guess it should be OneToMany relation, bacause one product can have many
users who like it.
/**
* @ORM\OneToMany(targetEntity="User", mappedBy="userId")
*/
protected $usersWhoLike;
I added this, but I don't know what to write in the mappedBy option.
Whatever I write, I get an error, saying
An exception has been thrown during the rendering of a template ("Notice:
Undefined index: userId in
C:\xampp\htdocs\MyProject\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php
line 1753")
that's because I wrote a function for Twig, using the new added fields.
I also added there functions to the Product class:
public function addUserWhoLike($user)
{
$this->usersWhoLike[] = $user;
return $this;
}
public function removeUserWhoLike($user)
{
$this->usersWhoLike->removeElement($user);
}
public function getUsersWhoLike()
{
return $this->usersWhoLike;
}
and one more that check if a user is in the array of userswholike a product.
Can you plase help me to fix the relation?
Although the error is in the template, I'm almost sure that it's comming
because of the word after mappedBy
No comments:
Post a Comment