Symfony2 FOSUserBundle邀请:仅在拥有侧方关联的情况下工作 [英] Symfony2 FOSUserBundle Invitation : only work on owning side associations
问题描述
实体/用户
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;
}
}
错误
您无法搜索关联字段 'My \ SampleBundle \ Entity \ Invitation#user',因为它是反函数 关联的一面.查找方法仅在所有权方面起作用 协会. 500内部服务器错误-ORMException
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.
有帮助吗?
实际上,我一开始就设置了"inversedBy".
预先提问.
Symfony2 FOSUserBundle邀请:"inversedBy"映射错误
从表面上看,它确实起作用.但是,探查器会显示映射错误.
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.
我的\ SampleBundle \ Entity \ Invitation#不包含必需的 "inversedBy"属性.
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.
推荐答案
有解决方案.
https://github.com/FriendsOfSymfony/FOSUserBundle/issues/800
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
));
}
这篇关于Symfony2 FOSUserBundle邀请:仅在拥有侧方关联的情况下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!