使用两个实体之间的额外属性编辑关系 n:m 的表单 [英] Editing form for relationship n:m with extra attributes between two entities

查看:28
本文介绍了使用两个实体之间的额外属性编辑关系 n:m 的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体之间的映射:

I have this mapping betwenn two entities:

class Orders {
    // Other attributes 

    /**
     * @ORM\OneToMany(targetEntity="OrderHasProduct", mappedBy="order") 
     */
    protected $orderProducts;

    // Other set/get methods

    public function getOrderProducts()
    {
        return $this->orderProducts;
    }
}

class Product {
    // Other attributes

    /**
     * @ORM\OneToMany(targetEntity="\Tanane\FrontendBundle\Entity\OrderHasProduct", mappedBy="product")
     */
    protected $orderProducts;

    // Other set/get methods

    public function getOrderProducts()
    {
        return $this->orderProducts;
    }
}

当然,由于许多订单可以有许多产品,而且还需要一个额外的属性,因此需要其他实体:

And of course since many Orders can have many products but also there is an extra attribute this other entity is needed:

class OrderHasProduct
{
    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="\Tanane\FrontendBundle\Entity\Orders")
     * @ORM\JoinColumn(name="general_orders_id", referencedColumnName="id")
     */
    protected $order;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="\Tanane\ProductBundle\Entity\Product")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    protected $product;

    /**
     * @ORM\Column(type="integer", nullable=false)
     */
    protected $amount;

    public function setOrder(\Tanane\FrontendBundle\Entity\Orders $order)
    {
        $this->order = $order;
    }

    public function getOrder()
    {
        return $this->order;
    }

    public function setProduct(\Tanane\ProductBundle\Entity\Product $product)
    {
        $this->product = $product;
    }

    public function getProduct()
    {
        return $this->product;
    }

    public function setAmount($amount)
    {
        $this->amount = $amount;
    }

    public function getAmount()
    {
        return $this->amount;
    }
}

当我编辑订单时,我应该能够添加/删除该订单上的产品,但我不知道如何实现.我知道我必须使用表单集合,但如何使用?我的意思是一个集合应该嵌入如下:

When I edit a order I should able to add/remove the products on that order but I don't know how to achieve this. I knew that I must use a form collection but how? I mean a collection should be embed as follow:

$builder->add('product', 'collection', array(
    'type' => new OrderHasProductType(),
    'allow_add' => true,
    'allow_delete' => true
));

当我应该创建一个新的 OrderHasProductType 表单时,我想我明白了这一点,但我现在的问题是,订单的 ID 会发生什么变化?处理带有额外参数的嵌入表单关系 n:m 的正确方法是什么?

When I should create a new OrderHasProductType form and I think I understand until this point but my question now is, what happens to the ID of the order? What is the proper way to handle an embedded form a relationship n:m with extra parameters?

谁能给我一些代码示例来排序我的想法?

Can any give me some code example to order my ideas?

额外资源

推荐答案

我认为您的情况有点复杂,因为与两个实体之间没有标准的 Doctrine 多对多关系,而是两个单独的一对多和多对多关系一对一关系,与三个实体.

I think your situation is slightly complicated by having not a standard Doctrine many-to-many relationship with two Entities, but two separate one-to-many and many-to-one relationships, with three Entities.

通常,对于一个完整的多对多,流程是有一个 OrderType 表单,其中包含一个充满 ProductType 的 Collection 字段,代表分配给订单的产品.

Normally, with a full many-to-many, the process is to have, for example, an OrderType form, containing a Collection field full of ProductTypes representing the Products assigned to the Order.

('allow_add' => true 意味着如果 Symfony 看到一个没有 ID 的条目,它会认为它是一个通过 Javascript 添加的全新项目,并且很乐意调用表单 Valid 并将新项目添加到实体中.'allow_delete' => true 相反意味着如果缺少其中一项,Symfony 会将其从实体中删除.)

('allow_add' => true means that if Symfony sees an entry with no ID it expects it to be a brand new item added via Javascript, and is happy to call the form Valid and add the new item to the Entity. 'allow_delete' => true conversely means that if one of the items is missing then Symfony will remove it from the Entity.)

但是,您还有一个更高级别的实体,它是 Order->OrderHasProduct->Product.因此,从逻辑上讲,您的 OrderType 表单包含一个 OrderHasProductType 表单集合(如您在上面所述),而后者又包含一个 ProductType 表单.

However, you have one further level of Entities, it goes Order->OrderHasProduct->Product. So logically your OrderType form contains a Collection of OrderHasProductType forms (as you've put above), which in turn contains a ProductType form.

所以你的 OrderType 变得更像这样:

So your OrderType becomes more like this:

$builder->add('orderHasProducts', 'collection', array(
    'type' => new OrderHasProductType(),
    'allow_add' => true,
    'allow_delete' => true
));

您还有另一个级别的产品:

And you also have another level for the Products:

OrderHasProductType

$builder->add('product', 'collection', array(
    'type' => new ProductType(),
    'allow_add' => true,
    'allow_delete' => true
));

还有 ProductType:

And a ProductType as well:

$builder->add('product', 'entity', array(
    'class' => 'ProductBundle:Product'
));

Symfony 应该很乐意将您的实体映射到正确的类型级别.在您的视图中,您需要包含 Javascript,它会理解将产品添加到订单还涉及 ProductHasOrder 级别 - 最好放入一些数据并查看 Symfony 如何将其转换为表单,然后模仿 Javascript 中的结构.

Symfony should be happy to map your Entities to the correct level of Types. In your View you will need to include Javascript which will understand that adding a Product to an Order also involves the ProductHasOrder level - best to put some data in and see how Symfony turns that into a form, and then mimic the structure in the Javascript.

这篇关于使用两个实体之间的额外属性编辑关系 n:m 的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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