问题描述
我使用两个DQL查询.在我的网站上,我有用户和Formations.我在User上定义了ManyToMany关系,如下所示:
I use two DQL queries. On my website I have users and Formations. I have defined a ManyToMany relation on User like this:
/**
* Many User have Many Phonenumbers.
* @ManyToMany(targetEntity="Formation")
* @JoinTable(name="users_favorites",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="formation_id", referencedColumnName="id", unique=true)}
* )
*/
private $favorites;
用户可以将队形添加到自己的收藏夹中.
Users can add Formations to their favorite.
第一个查询,我想获取所有的队形,还要获得将这个队形添加到自己的收藏夹的用户.
The first query, I would like to get all the Formations but also, the users that added this formation to their favorite.
public function findAllWithFormation(){
return $this->createQueryBuilder('user')
->join('user.favorites', 'formation')
->addSelect('formation')
->addSelect('user')
->getQuery()
->getOneOrNullResult()
;
}
这什么也不返回,不是一个单一的形式.
This return nothing, not a single formation.
我的查询结果是这样:
FormationController.php on line 22:
App\Entity\User {#768 ▼
-id: 1
-email: "mail@mail.fr"
-roles: array:2 [▶]
-password: ""
-favorites: Doctrine\ORM\PersistentCollection {#765 ▼
-snapshot: array:1 [ …1]
-owner: App\Entity\User {#768}
-association: array:19 [ …19]
-em: Doctrine\ORM\EntityManager {#563 …11}
-backRefFieldName: null
-typeClass: Doctrine\ORM\Mapping\ClassMetadata {#703 …}
-isDirty: false
#collection: Doctrine\Common\Collections\ArrayCollection {#766 ▼
-elements: array:1 [▼
0 => App\Entity\Formation {#763 ▼
-id: 1
-name: "PHP "
-url: "http://ulrt"
}
]
}
#initialized: true
}
-pseudo: null
}
如您所见,我只收到用户已添加到收藏夹中的表格,而我既要获取所有表格,又要获得有关哪个用户已将此表格添加到他们的收藏夹中的信息.在User.php中,我在ManyToMany中具有字段收藏夹,这将创建一个表users_favorites,这就是我想要的.但是我的队形实体上的队形与users_favorites之间没有任何联系.如何在两者之间添加链接?我需要添加一个字段吗?如果是这样,该如何定义它,以使其不会像在用户实体上使用收藏夹创建的表那样创建另一个表?
As you can see, I receive only the formation that the user has added in favorites, while I would like to get all the formation but also have the information of which user has added this formation to their favorite.In User.php I have the field favorites in ManyToMany and this creates a table users_favorites, and this is what I want. But I don't have any link between the formation and the users_favorites on my Formation Entity.How can I add a link between those? Do I need to add a field? If so, how do I define it so it doesn't create another table like the one it created with favorites on User Entity?
推荐答案
所以我假设您正在UserRepository中进行dql查询,您应该将查询重点放在FormationRepository上(假设正确的多对多定义是User.favorites具有匹配的Formation.users:...):
So I assume you're doing your dql query in the UserRepository, you should focus the query on the FormationRepository instead (assuming proper many-to-many definitions with User.favorites has a matching Formation.users: ...):
/**
* @ORM\ManyToMany(targetEntity=User:class, mappedBy="favorites")
*/
private $users;
您可能需要在User上添加 inversedBy =" users"
.喜欢多对多注释
you might need to add inversedBy="users"
on your User.favorites many-to-many annotation
$qb = $this->createQueryBuilder('formation');
return <$qb->leftJoin('formation.users', 'user')
->addSelect('user')
->getQuery()->getResult();
这篇关于DQL查询未返回我想要的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!