我有两个实体。 Users和UserRights,由ManyToOne单向关联映射。当我尝试创建一个具有特定userRights的新用户(之前创建并已经存在于表中)时,我收到错误“注意:未定义的索引:name \ _(原始)\ orm \ lib \ Doctrine \ name(...) ORM \ Persisters \ Entity \ BasicEntityPersister.php在第670行“。这是我的实体:
用户
/**
* @ORM\Entity(repositoryClass = "AppBundle\Repository\UsersRepository")
* @ORM\Table(name = "users")
*/
class Users
{
/**
* @ORM\Column(type = "string", length = 4)
* @ORM\Id
* @ORM\GeneratedValue(strategy = "NONE")
*/
protected $id;
/**
* @ORM\Column(type = "string", length = 20)
*/
protected $name;
/**
* @ORM\Column(type = "string", length = 20)
*/
protected $surname;
/**
* @ORM\Column(type = "string", length = 30)
*/
protected $email;
/**
* @ORM\ManyToOne(targetEntity = "Departments")
* @ORM\JoinColumn(name = "department", referencedColumnName = "name")
*/
protected $department;
/**
* @ORM\Column(type = "boolean", options = {"default" = FALSE})
*/
protected $isActive;
/**
* @ORM\ManyToOne(targetEntity = "UserRights")
* @ORM\JoinColumn(name = "user_rights", referencedColumnName = "name")
*/
protected $userRights;
UserRights:
/**
* @ORM\Entity(repositoryClass = "AppBundle\Repository\UserRightsRepository")
* @ORM\Table(name = "user_rights")
*/
class UserRights
{
/**
* @ORM\Column(type = "integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy = "AUTO")
*/
protected $id;
/**
* @ORM\Column(type = "string", length = 20, unique = TRUE)
*/
protected $name;
控制器:
/**
* @Route("/create/user", name="create/user")
*/
public function createUserAction()
{
$em = $this->getDoctrine()->getManager();
$userRights = $em->getRepository('AppBundle:UserRights')->findOneByName('Admin');
$department = $em->getRepository('AppBundle:Departments')->findOneByName('CN');
$user = new Users();
$user->setId('josm');
$user->setName('John');
$user->setSurname('Smith');
$user->setEmail('cos@gmail.com');
$user->setIsActive(TRUE);
$user->setUserRights($userRights);
$user->setDepartment($department);
$em->persist($user);
$em->flush();
return new Response(dump($user));
}
使用dump($ user)时,一切看起来都很好。当我尝试使用persist + flush将新创建的用户放入数据库时,问题就开始了。
考虑语法没有错误,所以我相信我在这里做错了。我假设关联或unique = true
是问题。我已经阅读了很多主题,几乎所有主题都是关于错误的语法 - 产品而不是产品,客户而不是客户等。我检查了几次拼写,我确信它是正确的。