【发布时间】:2019-03-25 03:31:01
【问题描述】:
我对教义有疑问。我有两个实体,想要实现一对多关联。
用户
/**
* @Entity
*/
class User
{
/** @Id @Column(type="integer") @GeneratedValue */
protected $id;
/** @Column(type="string") */
protected $name;
/**
* @OneToMany(targetEntity="role", mappedBy="userId", cascade={"ALL"})
*/
private $roles;
}
角色
/**
* @Entity
*/
class Role
{
/** @Id @Column(type="integer")
*
* @ManyToOne(targetEntity="user", inversedBy="roles")
*
*/
protected $userId;
/** @Column(type="string") */
protected $name;
}
但是当我尝试访问用户的角色时:
var_dump($user->getRoles()->first()->getName());
注意:未定义索引:userId 在 /home/ubuntu/workspace/atlas/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php 1794 行警告:为
中的 foreach() 提供的参数无效 /home/ubuntu/workspace/atlas/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php 在第 1798 行 string(10) "ROLE_ADMIN" 警告:在 /home 中为 foreach() 提供的参数无效/ubuntu/workspace/atlas/Controller/LoginController.php 第 15 行"
这意味着我得到了正确的值,但有一些警告,我不确定是否可以忽略它们。
【问题讨论】:
标签: php doctrine-orm one-to-many