Symfony2 FOSUserBundle邀请:只在拥有方协会工作[英] Symfony2 FOSUserBundle Invitation : only work on owning side associations

本文是小编为大家收集整理的关于Symfony2 FOSUserBundle邀请:只在拥有方协会工作的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

实体/用户

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

实体/邀请

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

错误

您无法搜索关联字段 "我的\ samplebundle \ entity \邀请#用户",因为它是倒数 协会的一面.查找仅在拥有方面起作用的方法 协会. 500内部服务器错误 - Ormexception

我是刚刚执行文档的阶段.

https://blob/master/resources/doc/adding_invitation_registration.md

捆绑包的显示是正常的.但是,如果我提交注册表,这已成为一个错误.

有帮助吗?

编辑:

实际上,我一开始就设置了"反向".
质疑.
symfony2 symfony2 fosuserbundle邀请函 从表面上看,它确实有效.但是,映射错误由Profiler显示.

我的\ samplebundle \ entity \邀请#不包含所需的 '反向'属性.

所以,我对建议进行了更改. 我不知道该怎么想.

推荐答案

有解决方案.
https://github.com/friendsofsymfony/fosususerbundle/500

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

    return $this->entityManager
        ->getRepository('My\SampleBundle\Entity\Invitation')
        ->findOneBy(array(
            'code' => $value,
            'user' => null,        <= Removing 'user' solves the issue
        ));
}

其他推荐答案

正如错误所说的那样.您应该在邀请实体上使用倒置,而不是Mappapedby,并在用户实体上使用Mappedby作为$邀请.

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

您还可以通过创建一种基于邀请的自定义存储库方法来克服此问题.

其他推荐答案

我最终将变压器修改为以下内容:

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

我剥离了'user' => null,但添加了检查以查看邀请是否已经使用

本文地址:https://www.itbaoku.cn/post/2090804.html

问题描述

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.

推荐答案

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

其他推荐答案

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.

其他推荐答案

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