0

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.

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/adding_invitation_registration.md

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.

Community
  • 1
  • 1
JIGEN
  • 123
  • 4
  • 11
  • You could have explained the problem a bit better just spending a couple more words. It's totally unclear what you're trying to do or how this problem exactly occurred. – Aldo Stracquadanio Oct 31 '12 at 11:53
  • OK. I am the stage which just performed the documentation. [https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/adding_invitation_registration.md] The display of the bundle is normal. However, It has become an error if I submit registration form. – JIGEN Oct 31 '12 at 12:36

3 Answers3

2

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.

/** @ORM\OneToOne(targetEntity="User", inversedBy="invitation", cascade={"persist", "merge"}) */
    protected $user;

You can also overcome this problem by creating a custom repository method to find user based on invitation.

Hyunmin Kim
  • 931
  • 2
  • 8
  • 18
  • Actually, I had set up 'inversedBy' at first. A pre-question. [http://stackoverflow.com/questions/13149930/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. um...I don't know what to think of it. – JIGEN Oct 31 '12 at 15:52
  • There was solution. [https://github.com/FriendsOfSymfony/FOSUserBundle/issues/800] I thank you for your help in this matter. – JIGEN Oct 31 '12 at 17:29
  • I don't think this is really a solution. It may resolve the error however it allows the same invitation to be used over and over – Shawn Northrop Aug 06 '13 at 19:39
1

There was solution.
https://github.com/FriendsOfSymfony/FOSUserBundle/issues/800

public function reverseTransform($value)
{
    // ...

    return $this->entityManager
        ->getRepository('My\SampleBundle\Entity\Invitation')
        ->findOneBy(array(
            'code' => $value,
            'user' => null,        <= Removing 'user' solves the issue
        ));
}
JIGEN
  • 123
  • 4
  • 11
  • Note that this can also happen with constraints, e.g. `@UniqueEntity({"user", "file" "track"})` where the `file` is 1:1 mapping side – Tjorriemorrie Apr 08 '13 at 14:54
  • 1
    As mentioned in the github issue, when removing `'user' => null` you can have multiple users using the same invitation. – Shawn Northrop Aug 06 '13 at 19:37
0

I ended up modifying my transformer to the following:

public function reverseTransform($value)
{
    if (null === $value || '' === $value) {
        return null;
    }

    if (!is_string($value)) {
        throw new UnexpectedTypeException($value, 'string');
    }

    $invitation = $this->entityManager
        ->getRepository('SixString\PearBundle\Entity\Invitation')
        ->findOneBy(array(
            'code' => $value,
        ));
    if($this->entityManager->getRepository('SixString\PearBundle\Entity\User')->findOneBy(array("invitation" => $invitation))){
        return null;
    }

    return $invitation;
}

I stripped out the 'user' => null but added a check to see if the invitation has already been used

Shawn Northrop
  • 5,826
  • 6
  • 42
  • 80