Symfony2 FOSUserBundle Invitation : only work on owning side associations

a 夏天 提交于 2019-12-08 10:44:14

问题


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.


回答1:


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.




回答2:


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
        ));
}



回答3:


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



来源:https://stackoverflow.com/questions/13156433/symfony2-fosuserbundle-invitation-only-work-on-owning-side-associations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!