Doctrine 2 deletes with the query builder

I have two Entities with relation OneToMany, Project
and Services
. Now i want to remove all the services by project_id.

First attempt:

$qb = $em->createQueryBuilder();
$qb->andWhere($qb->expr()->eq('s.project_id', ':id'));

This attempt fails with the Exception Entity Service does not have property project_id
. And it’s true, that property does not exists, it’s only in database table as foreign key.

Second attempt:

$qb = $em->createQueryBuilder();
$qb->andWhere($qb->expr()->eq('', ':id'));

This one generetate a non valid DQL query too.

Any ideas and examples will be welcome.

You’re working with DQL, not SQL, so don’t reference the IDs in your condition, reference the object instead.

So your first example would be altered to:

$qb = $em->createQueryBuilder();
$qb->delete('Services', 's');
$qb->where('s.project = :project');
$qb->setParameter('project', $project);
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Doctrine 2 deletes with the query builder

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录