I
I
idkiberlord0772016-05-02 11:29:52
symfony
idkiberlord077, 2016-05-02 11:29:52

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();
            }
        }

I'm trying to delete a category (CategoryEntity) that is associated with (ProductEntity) If you just delete a category, then the object (ProductEntity) that requests data from CategoryEntity throws an error.
Therefore, I try to implement using this part and then delete it:
$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));

and then remove it here. But it says:
EntityManager#remove() expects parameter 1 to be an entity object, array given.
I will be very grateful for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2016-05-02
@idkiberlord077

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();

2. You get the Product array . So delete everything one by one:
$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 question

Ask a Question

731 491 924 answers to any question