将许多项目分配到一个类别 - OneToMany关系 [英] Assign many items to one category - OneToMany Relation

查看:118
本文介绍了将许多项目分配到一个类别 - OneToMany关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多个项目分配给一个类别。我正在使用Symfony 2.6.7。



我不在这边。如果我打开这个URI:



/ category / 2 / assign

  [x]项目1 
[x]项目2
(保存按钮)

我有可能选择许多项目与复选框。



这是我的数据库表项目,我想连接两者:

  id | category_id 

这是一个OneToMany关系,其中One = Category和Many = 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。
}

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

在Symfony2中,具有inversedBy原则注释的属性的实体是当您要在两个表之间创建新链接时,应采取行动。这就是为什么你可以为一个项目分配一个类别,但不能将项目添加到一个类别。



上面的代码是一个标准的CRUD生成的Symfony2 updateAction <强>功能。唯一的调整是 foreach ,因此强制将类别分配给您在表单中选择的每个项目。



这是基本的,但它的工作原理/ p>

注意:我没有为类别中的REMOVING项目添加解决方法,但是类似的方法也是如此。希望它有帮助。



编辑:删除项目:

  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。

}
//新行
$ before = $ entity-> getItems() - > getValues();

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

//新行
$ after = $ entity-> getItems() - > getValues();

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

foreach($ after 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(),
));
}

相同的功能,只包括新行。



array_diff 将在提交之前返回链接到类别实体的项目,而不是在提交之后,然后使用 foreach 再次可以将 null 分配为每个这些项目的类别,即:中断它们之间的链接。



第二个 foreach 与原始答案相同。现在就这样尝试一下,告诉我是否有效。



再次,基本的,再次应该有效。


I want to assign many items to one category. I'm using Symfony 2.6.7.

I'm not at the owning side here. If I open this URI:

/category/2/assign

[x] Item 1
[x] Item 2
(Save Button)

I have the possibility to chose many items with checkboxes.

This is my db table "Item", where I want to connect the both:

id | category_id

It is an OneToMany relation, where One = Category and Many = Items.

How can I assign the both here?

It is already working, when I edit one item and than select a category to this item. Now I'm on the category side and here I want select many items to this one category. Can someone help, please? :-)

解决方案

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.

这篇关于将许多项目分配到一个类别 - OneToMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆