Entity/User
namespace My\SampleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends \FOS\UserBundle\Entity\User
{
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO") */
protected $id;
/**
* @ORM\OneToOne(targetEntity="Invitation", inversedBy="user")
* @ORM\JoinColumn(referencedColumnName="code")
* @Assert\NotNull(message="Your invitation is wrong")
*/
protected $invitation;
public function setInvitation(Invitation $invitation)
{
$this->invitation = $invitation;
}
public function getInvitation()
{
return $this->invitation;
}
}
Entity/Invitation
namespace My\SampleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity */
class Invitation
{
/** @ORM\OneToOne(targetEntity="User", mappedBy="invitation", cascade={"persist", "merge"}) */
protected $user;
/** @ORM\Id @ORM\Column(type="string", length=6) */
protected $code;
/** @ORM\Column(type="string", length=256) */
protected $email;
/**
* When sending invitation be sure to set this value to `true`
*
* It can prevent invitations from being sent twice
*
* @ORM\Column(type="boolean")
*/
protected $sent = false;
public function __construct()
{
// generate identifier only once, here a 6 characters length code
$this->code = substr(md5(uniqid(rand(), true)), 0, 6);
}
public function getCode()
{
return $this->code;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function isSent()
{
return $this->sent;
}
public function send()
{
$this->sent = true;
}
public function getUser()
{
return $this->user;
}
public function setUser(User $user)
{
$this->user = $user;
}
/**
* Set code
*
* @param string $code
* @return Invitation
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Set sent
*
* @param boolean $sent
* @return Invitation
*/
public function setSent($sent)
{
$this->sent = $sent;
return $this;
}
/**
* Get sent
*
* @return boolean
*/
public function getSent()
{
return $this->sent;
}
}
Error
You cannot search for the association field 'My\SampleBundle\Entity\Invitation#user', because it is the inverse side of an association. Find methods only work on owning side associations. 500 Internal Server Error - ORMException
I am the stage which just performed the documentation.
The display of the bundle is normal. However, It has become an error if I submit registration form.
Any help?
EDIT:
Actually, I had set up 'inversedBy' at first.
A pre-question.
Symfony2 FOSUserBundle Invitation : 'inversedBy' mapping errors
On the surface, it does work. However, mapping errors is displayed by profiler.
My\SampleBundle\Entity\Invitation# does not contain the required 'inversedBy' attribute.
so, I changed it in response to advice. I don't know what to think of it.
It's just as the error says. Instead of mappedBy, you should use inversedBy on the Invitation entity and use mappedBy on the User entity for $invitation.
You can also overcome this problem by creating a custom repository method to find user based on invitation.
I ended up modifying my transformer to the following:
I stripped out the
'user' => null
but added a check to see if the invitation has already been usedThere was solution.
https://github.com/FriendsOfSymfony/FOSUserBundle/issues/800