[英]In Doctrine, do I need do create a class only to make a join?
I'm trying to make a join in my repository class (Symfony 3 with Doctrine). 我正在尝试加入我的存储库类(Symfony 3和Doctrine)。
This is how it looks like: 它是这样的:
public function findByRole($roles){
$qb = $this->createQueryBuilder('u')
->join('user_role', 'ur', Join::ON, 'ur.id = u.customerId');
$q = $qb->getQuery();
$users = $q->getArrayResult();
dump($users);
}
And I've got this error: 我有这个错误:
[Semantical Error] line 0, col 49 near 'user_role ur': Error: Class 'user_role' is not defined.
[语义错误]第0行,'user_role ur'附近的第49列:错误:未定义类'user_role'。
There are two entity classes defined - User and Role. 定义了两个实体类-用户和角色。 And Role is a property of User (ManyToMany).
角色是用户(ManyToMany)的属性。
There is also a joining table - user_role - do I need to create a class for every joining table even if I don't need it for my own purposes? 还有一个联接表-user_role-即使我不需要出于自己的目的也需要为每个联接表创建一个类吗?
ps I know this error can be already found on Stackoverflow, but people having this issue probably have different problems. ps我知道在Stackoverflow上已经可以找到此错误,但是遇到此问题的人可能会有不同的问题。
Update: 更新:
Here's the User entity class (shortened): 这是用户实体类(已缩短):
/**
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
//irrelevant code removed
/**
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Role", cascade = {"persist"}, inversedBy="users")
* @ORM\JoinTable(name="user_role",
* joinColumns={@ORM\JoinColumn(name="user_id",
* referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id",
* referencedColumnName="id")}
* )
*/
private $roles;
//////////////////////////////////////////////////////////////////////
public function __construct() {
$this->roles = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
////////////////////////////////
//roles
/**
* Add role
* @param \AppBundle\Entity\Role $role
*
* @return User
*/
public function addRole(\AppBundle\Entity\Role $role) {
$this->roles[] = $role;
return $this;
}
public function setRoles(\AppBundle\Entity\Role $role){
$this->addRole($role);
return $this;
}
/**
* Remove role
* @param \AppBundle\Entity\Role $role
*/
public function removeRole(\AppBundle\Entity\Role $role) {
$this->roles->removeElement($role);
}
/**
* I know it probably should return simply $this->roles, but the interface I'm implementing requires an array of strings... But everything works fine, except joining user+roles
* @return \Doctrine\Common\Collections\Collection
*/
public function getRoles() {
return $this->roles->toArray();
}
/**
* Get roles
* @return \Doctrine\Common\Collections\Collection
*/
public function getRoleEntities() {
return $this->roles;
}
}
I doctrine, all joined objects must be entities. 我认为,所有连接的对象必须是实体。
But you can map a many-to-many relationship between the entities User and Role, using user_role as a linkage table 但是您可以使用user_role作为链接表来映射实体User和Role之间的多对多关系
In user table you might have a property $roles
在用户表中,您可能具有属性
$roles
/**
* @ManyToMany(targetEntity="Role", inversedBy="users")
* @JoinTable(name="user_role",
* joinColumns={@JoinColumn(name="user_id",
* referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="role_id",
* referencedColumnName="id")}
* )
*/
private $roles;
and a property $users
in role table 和角色表中的属性
$users
/**
* @ManyToMany(targetEntity="User", inversedBy="roles")
* @JoinTable(name="user_role",
* joinColumns={@JoinColumn(name="role_id",
* referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="user_id",
* referencedColumnName="id")}
* )
*/
private $users;
I've not tested this code. 我尚未测试此代码。 Surely It gonna need some tweeking.
当然,这需要一些周游。 You also need to created the getters and setters for these properties.
您还需要为这些属性创建getter和setter。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.