Answer the question
In order to leave comments, you need to log in
EntityManager#remove() expects parameter 1 to be an entity object, array given. How to fix?
The code :
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('WindowsBundle:Category')->find($id);
$query = $em->createQuery(
'SELECT c.name FROM WindowsBundle:Category c
WHERE c.id=:id')
->setParameter('id',$id);
$entity_name = $query->getResult();
$entity_product=$em->getRepository('WindowsBundle:Product')->findBy(array('category'=>$entity_name));
if (!$entity ) {
throw $this->createNotFoundException('Unable to find Category entity.');
}
$em->remove($entity);
$em->flush();
if($entity_product!==null)
{
$em->remove($entity_product);
$em->flush();
}
}
$query = $em->createQuery(
'SELECT c.name FROM WindowsBundle:Category c
WHERE c.id=:id')
->setParameter('id',$id);
$entity_name = $query->getResult();
$entity_product=$em->getRepository('WindowsBundle:Product')->findBy(array('category'=>$entity_name));
Answer the question
In order to leave comments, you need to log in
1. Why is the name property mapping not configured for the category?
$entity = $em->getRepository('WindowsBundle:Category')->find($id);
// Чтобы вместо этого
$query = $em->createQuery('SELECT c.name FROM WindowsBundle:Category c WHERE c.id=:id')
->setParameter('id',$id);
$entity_name = $query->getResult();
// ...писать просто
$entity_name = $entity->getName();
$entity_product=$em->getRepository('WindowsBundle:Product')->findBy(array('category'=>$entity_name));
foreach ($entity_product as $product) {
$em->remove($product);
}
$em->flush();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question