1

我在使用 Symfony2 从我的数据库中删除记录时遇到问题。希望有人可以帮助我。

这是我的代码:

// Get user's account
$account = $this->getUser()->getAccount();

// Get manager
$em = $this->getDoctrine()->getManager();

// Get entity
$entity = $em->getRepository('WICPurchaseOrderLineItemBundle:PurchaseOrderLineItem')->findBy(array('account'=>$account->getId(), 'id'=>$id));

// If not entity
if (!$entity) {
throw $this->createNotFoundException('Unable to find this entity.');
}

// Remove the record...
$em->remove($entity);
$em->flush();

// Go to this url...
return $this->redirect($this->generateUrl('purchaseOrder_view', array('id' => '8')));

运行此程序时,我收到此错误:

 EntityManager#remove() expects parameter 1 to be an entity object, array given.

我的网址如下所示:

 {{ path('purchase_order_remove_line_item', { 'id': purchaseOrderLineItem.id }) }}

我的“id”号码需要先变成一个对象吗?不知道如何解决这个问题,仍在学习 Symfony。

有人有什么建议吗?

4

1 回答 1

3

您只需要使用findOneBy方法而不是findBy方法。

$entity = $em->getRepository('WICPurchaseOrderLineItemBundle:PurchaseOrderLineItem')->findOneBy(array('account'=>$account->getId(), 'id'=>$id));
于 2013-04-02T00:23:16.040 回答