dongwuxie7976 2013-11-04 16:18
浏览 30

Doctrine批量删除连接表记录

I have a normal many to many relationship between two entities : User and Object.

________        _________________        ____________
| User |        |   User_Object |        |  Object  |
|------|        |---------------|        |----------|
|  id  |        |     user_id   |        |    id    |
| .... |        |    object_id  |        |   ....   |
|______|        |_______________|        |__________|

I want to batch delete pretty big sets of Users (and the records associated with them in the User_Object table). Removing the entities one by one isn't fast enough for my needs (for > 1000 entities it takes a loooonnnng time).

//This method is far too slow for my needs
$qb = $this->doctrine->em->createQueryBuilder();
$qb->select('u')
    ->from('Entities\User', 'u')
    ->where("u.whatever= ?1")
    ->setParameter(1, $whatever);
$users = $qb->getQuery()->getResult();

foreach($users as $user)
{
    $this->doctrine->em->remove($user);
    //...

The doctrine docs say that the most efficient to bulk delete entities is DQL, which would give me something like :

$qb = $this->doctrine->em->createQuery('delete from Entities\User u where u.whatever = ?1');
$qb->setParameter(1, $whatever);
$numDeleted = $qb->execute(); //This will throw because of User_Object records

This will throw an exception because of the records in the User_Object join table (referential integrity exception).

So, my question is : how do I delete the records in the join table efficiently in a bulk delete scenario.

I would really like to avoid throwing raw SQL at it, the rest of my code uses entities everywhere and I would like to keep it that way if at all possible.

EDIT : The relationship is marked as such (I use yml) :

manyToMany:
  objects:
  targetEntity: Object
  inversedBy: users
  cascade: ["remove"]
  joinTable:
    name: User_Object
    joinColumns:
      user_id:
        referencedColumnName: id
    inverseJoinColumns:
      object_id:
        referencedColumnName: id
  • 写回答

1条回答 默认 最新

  • dousi2553 2013-11-04 16:42
    关注

    You haven't setup the behavior of doctrine if you are deleting the user for the object. You have to says doctrine that if the user is delete then all related object must be deleted too.

    /**
     * @ORM\ManyToMany(targetEntity="User", mappedBy="objects", cascade={"remove"})
     */
    protected $users;
    
    
    /**
     * @ORM\ManyToMany(targetEntity="Object", inversedBy="users", cascade={"persist", "remove"})
     * @ORM\JoinTable(name="user_object",
     * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     * inverseJoinColumns={@ORM\JoinColumn(name="object_id", referencedColumnName="id")}
     * )
     */
    protected $objects;
    

    This is an example. I didn't tested it.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看