Symfony2实体集合默认数据和填充选择框

时间:2013-06-29 12:53:15

标签: php jquery forms symfony populate

至少对我来说,我有一个小小的挑战,但我还没有解决。你能帮我解决一下吗?

实际上我的问题包括两个,我不能将它们分成不同的问题。

我安装了Symfony2(2.3.1)。这是我的实体:

类别:

// src/Acme/DemoBundle/Entity/Category.php

namespace Acme\DemoBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * @Gedmo\Tree(type="nested")
 * @ORM\Table(name="categories")
 * use repository for handy tree functions
 * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
 */
class Category
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

    /**
     * @Gedmo\Translatable
     * @ORM\Column(name="name", type="string", length=64)
     */
    private $name;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(name="lft", type="integer")
     */
    private $lft;

    /**
     * @Gedmo\TreeLevel
     * @ORM\Column(name="lvl", type="integer")
     */
    private $lvl;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(name="rgt", type="integer")
     */
    private $rgt;

    /**
     * @Gedmo\TreeRoot
     * @ORM\Column(name="root", type="integer", nullable=true)
     */
    private $root;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    private $children;

档案:

// src/Acme/DemoBundle/Entity/Item.php
namespace Acme\DemoBundle\Entity;

use DoctrineExtensions\Taggable\Taggable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;

/**
 * Item entity
 *
 * @ORM\Table(name="items")
 * @ORM\HasLifecycleCallbacks
 * @ORM\Entity
 */
class Item implements Taggable
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="name", type="string", length=64, nullable=true)
     */
    protected $name;

    /**
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="items")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id"),
     *      inverseJoinColumn=(name="item_id", referencedColumnName="id")
     *
     **/
    protected $categories;

     public function getCategories(){
        return $this->categories;
    }

    public function setCategories($categories){
        $this->categories = $categories;

        return $this->categories;
    }

,我的表单AddItemForm:

// src/Acme/DemoBundle/Controller/ItemController.php
namespace Acme\DemoBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Validator\Constraints\Collection;

/**
 * Add item form
 *
 */
class AddModelForm extends AbstractType
{    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('categories', 'collection', array(
                                                        'type' => 'entity',
                                                        'allow_add' => true,
                                                        'allow_delete' => true,
                                                        'prototype' => true,
                                                        'show_legend' => false,
                                                        'data' => array(''),
                                                        'widget_add_btn' => array('label' => _('Добавить категорию')),
                                                        'options' => array(
                                                                        'widget_control_group' => false,
                                                                        'label_render' => false,
                                                                        'class' => 'AcmeDemoBundle:Category',
                                                                        'query_builder' => function(EntityRepository $er) {
                                                                            return $er->createQueryBuilder('c')
                                                                                ->where('c.lvl = 0')
                                                                                ->orderBy('c.id', 'ASC');
                                                                        },
                                                                        'property' => 'name',
                                                                        'empty_value' => _('Choose category'),
                                                                    ),

                                                    )
                              );
    }
}

和带有简单代码的控制器ItemController:

// src/Acme/DemoBundle/Controller/ItemController.php
...
public function editAction($itemId) {
    $item= $em->getRepository('AcmeDemoBundle:Item')
                  ->findOneById($itemId);

    $form = $this->createForm(new AddItemForm(), $item);
}

public function addAction() {
    $item = new Item();

    $form = $this->createForm(new AddItemForm(), $item);
}
...

我的类别包含子类别和子子类别等...因为它是在嵌套树教条扩展中实现的。 我想要实现的目标是什么:

1)当我添加新项目时,我会看到此视图并出现此行为:http://clip2net.com/s/5jcaix(镜像:https://dl.dropboxusercontent.com/u/31477552/symfony2_form_challenge.png

现在我只实现了一个类别,其中父类加载为{{ form.widget(form.categories) }},子类加载了jquery。我不太喜欢我的临时解决方案。这就是我在这种情况下要求最佳实践的原因。可能我不需要重新发明轮子。

2)当我去编辑现有项目时,我想将类别数据传输到表单,以显示我们在添加步骤中的结果。使用单个“实体”类型或只是普通类型的映射很容易,并且在Symfony2中自动完成(或者您可以在默认选项中指定“数据类”)。但是如何在我的情况下实现它呢?通过实体集合+正确的视图显示?

对某些人来说,这对我来说既有趣也有挑战,你可以帮我解决这个问题。

0 个答案:

没有答案