使用Symfony 6 /学说优化问题:疑问太多
该项目是由Symfony 6.0.8开发的。
我有2个实体:Ultilisateur(用户)& compte_client(客户) - 通过OneToOne关系链接,因为用户可能不是客户。用户可以是:员工,合作伙伴或客户。 我想在客户表中(compte_client-lutilisateur_id)中的外键
在这里,我也想从客户的用户那里恢复信息。我使用QueryBuilder,对此恢复了用户的信息:
$queryBuilder->andWhere('u.type_utilisateur = :val');
$queryBuilder->setParameter('val', $type);
$queryBuilder->join('u.compte_client','c')
->addSelect('c');
$queryBuilder->orderBy('u.login', 'ASC');
$query = $queryBuilder->getQuery();
$results = $query->getResult();
return $results;
我会加入以限制查询数量,但是在这里,要恢复2个用户的数据,它实现了7个查询。 如果我删除加入请求,它会实现3个查询,但我认为可以将其简化为1个查询(正如我在在线课程中看到的 - 也使用加入请求)。
我试图在我的entites上添加一些属性,例如“ fetch:急切”或“ fetch:extra_lazy”,但它无效。
在所有这些之前,我也有类似的问题,可以从不是客户的用户中恢复数据。当我通过学说检查仅加载员工的用户而实现的查询时,尽管我没有试图恢复任何客户的信息,但他们对客户表的无用查询很多,而且预期他们的结果也不是。为了减少查询的数量,我必须添加这样的加入请求:
$queryBuilder
->Where('u.type_utilisateur LIKE :type')
->setParameter('type', 'Entreprise%')
->leftjoin('u.compte_client','c')
->addSelect('c')
->orderBy('u.login', 'ASC');
$query = $queryBuilder->getQuery();
$results = $query->getResult();
return results;
这仅在1中减少查询
。因此,是否有一些很好的pratice来优化学说的查询生成数量?
The project is develop with symfony 6.0.8.
I have 2 entitys : utilisateur (user) & compte_client (customer) - link by OneToOne relation because an user may be not a customer. A user can be : an employee, a partner or a customer. The foreign-key is in the customer table (compte_client -> utilisateur_id)
Here i want to recover informations from users who are customers too. I use the queryBuilder, i recover user's informations with this :
$queryBuilder->andWhere('u.type_utilisateur = :val');
$queryBuilder->setParameter('val', $type);
$queryBuilder->join('u.compte_client','c')
->addSelect('c');
$queryBuilder->orderBy('u.login', 'ASC');
$query = $queryBuilder->getQuery();
$results = $query->getResult();
return $results;
I do a join to limit the number of querys but here, to recover 2 user's datas, it realized 7 querys.
If I delete the join request, it realise 3 querys, but i think it can be reduce to 1 query (as i see in online courses - that used join request too).
I tried to add some attributs on my entites like "fetch:EAGER" or "fetch:EXTRA_LAZY" but it's ineffective.
Before all of it, I had similar problem to recover data's from users that aren't customers. When i checked querys realized by doctrine for loading only users who are employee, their was so many useless querys to customer's table despite of I didn't try to recover any customer's information, and their was no result expected. To reduce the number of query's i had to add a join request like that :
$queryBuilder
->Where('u.type_utilisateur LIKE :type')
->setParameter('type', 'Entreprise%')
->leftjoin('u.compte_client','c')
->addSelect('c')
->orderBy('u.login', 'ASC');
$query = $queryBuilder->getQuery();
$results = $query->getResult();
return results;
This, reduce query at only 1.
So is there some good pratice to optimize the number of querys generat by doctrine ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将理论视为继承的一种形式:反向“继承”拥有方的属性(提醒: ONETOONE关联的自有侧是带有包含外键的表的实体辅助学说始终提供自有的一方。
这意味着这里的好习惯是改变自有方面,因为
customer
从用户
继承更有意义。You have to think of Doctrine OneToOne as a form of inheritance: the inverse side "inherits" the owning side's properties (reminder: The owning side of a OneToOne association is the entity with the table containing the foreign key) so when you fetch the inverse side Doctrine always fetches the owning side.
That means the good practice here would be to change the owning side as it makes more sense for
customer
to inherit fromuser
.