Doctrine 2 delete with query builder

前端 未结 2 1334
南旧
南旧 2020-12-17 08:20

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

First attempt:

2条回答
  •  清歌不尽
    2020-12-17 08:26

    If you really can't get project object and you have to handle only with id you can use this.

    Citation from doctrine documentation: There are two possibilities for bulk deletes with Doctrine. You can either issue a single DQL DELETE query or you can iterate over results removing them one at a time. (Below I paste only first solution)

    DQL Query The by far most efficient way for bulk deletes is to use a DQL DELETE query.

    Example that worked in my project

    $q = $em->createQuery('delete from AcmeMyTestBundle:TemplateBlock tb where tb.template = '.intval($templateId));
    $numDeleted = $q->execute();
    

    In entity TemplateBlock I have property called template which is mapped to template_id in db.

    But I agree that highly preferred way of doing it is using objects.

提交回复
热议问题