实体/用户
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
我是刚刚执行文档的阶段。
捆绑包的显示是正常的。但是,如果我提交注册表,则它已成为错误。
有什么帮助吗?
编辑:
实际上,我一开始就设置了“inversedBy”。
一个前置问题。
Symfony2 FOSUserBundle Invitation : 'inversedBy' 映射错误
从表面上看,它确实有效。但是,分析器会显示映射错误。
My\SampleBundle\Entity\Invitation# 不包含所需的“inversedBy”属性。
所以,我根据建议改变了它。我不知道该怎么想。