有没有更好的方法来删除php数组中的ID? 下面是我的代码(zf2控制器动作),但我认为这不是好方法......
// $ _POST [' id'] =" 1,5,6,82,99"
public function deleteAllAction() {
$array = explode(",", $this->getRequest()->getPost('id'));
foreach ($array as $id) {
$feature = $this->getEntityManager()->find('Product\Entity\Feature', $id);
if ($feature) {
$this->getEntityManager()->remove($feature);
$this->getEntityManager()->flush();
}
}
return new JsonModel(array("success"));
}
答案 0 :(得分:1)
使用querying Doctrine repository稍微好一点的方法是首先获取所有实体,然后删除它们。请查看以下修改后的代码:
public function deleteAllAction() {
$array = explode(",", $this->getRequest()->getPost('id'));
$features = $this->getEntityManager()->getRepository('Product\Entity\Feature')->findBy( array( 'id' => $array ) );
foreach ($features as $feature) {
$this->getEntityManager()->remove($feature);
}
$this->getEntityManager()->flush();
return new JsonModel(array("success"));
}