Assign many items to one category - OneToMany Relation

纵饮孤独 提交于 2019-12-06 16:03:13
public function updateAction(Request $request, $id) {
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('YourBundle:Category')->find($id);

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

    $editForm = $this->createEditForm($entity);
    $editForm->bind($request);

    if ($editForm->isValid()) {
        foreach ($editForm->get('items')->getData()->getValues() as $u)
            $u->setCategory($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('category_show', array('id' => $id)));
    }

    return $this->render('YourBundle:Category:edit.html.twig', array(
                'entity' => $entity,
                'edit_form' => $editForm->createView(),
    ));
}

See, In Symfony2 the entity with the property with the inversedBy doctrine comment is the one that is supposed to take action when you want to create a new link between the two tables. That is why you can assign a category to an item but not add items to a category.

The above code is a standard CRUD-generated Symfony2 updateAction function. The only tweak is the foreach, thus forcibly assigning a category to every item you selected in the form.

It's rudimentary but it works.

NOTE: I did not include a workaround for REMOVING items from a category, but a similar approach would do it. Hope it helps.

EDIT: FOR REMOVING ITEMS:

public function updateAction(Request $request, $id) {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('YourBundle:Category')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Category entity.');

        }
        //new line
        $before = $entity->getItems()->getValues();

        $editForm = $this->createEditForm($entity);
        $editForm->bind($request);

        //new line
        $after = $entity->getItems()->getValues();

        if ($editForm->isValid()) {
            //new lines
            $UNselected = array_diff($before, $after);
            foreach ($UNselected as $u) {
                $u->setCategory(null);
            }

            foreach ($after as $u) {
                $u->setCategory($entity);
            }
            //new lines - end

            $em->flush();

            return $this->redirect($this->generateUrl('category_show', array('id' => $id)));
        }

        return $this->render('YourBundle:Category:edit.html.twig', array(
                    'entity' => $entity,
                    'edit_form' => $editForm->createView(),
        ));
    }

Same function, just include the new lines.

array_diff will return the items that were linked to the category entity before the submit and are not after the submit, then with the foreach again you can assign null as each of those items' category, i.e.: break the link between them.

The second foreach does the same as the original answer's. Just try it like this now and tell me if it worked.

Again, rudimentary, again, should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!