简体   繁体   English

嵌入各种形式-教义,symfony

[英]Embed a collection of forms - doctrine, symfony

I have two tables "RFQ" and "RFQitem". 我有两个表“ RFQ”和“ RFQitem”。 I can make form which can create RFQ with their title description and amount. 我可以制作一个表格,以其标题描述和金额创建询价单。 And I can create RFQitem form which can create RFQitem with their title, description and amount. 我可以创建RFQitem表单,该表单可以创建RFQitem及其标题,描述和金额。

Problems starts when I need to upgrade my RFQ form, so that I can make in it RFQitems which will saves in their table, but it need to be assigned to RFQ. 当我需要升级我的询价单时,问题就开始了,以便我可以在其中填写要保存在表中的询价单,但需要将其分配给询价单。

In symfony documentation is great example which actually works for me, but this is example is with task and their tags. 在symfony文档中,有一个很好的例子 ,实际上对我有用,但这是关于任务及其标签的例子。 So task there is with more than one attributes (name, description), but tags are only with one - name. 因此,任务具有多个属性(名称,描述),但标记仅具有一个属性。

My RFQ entity with RFQItems looks like this: 我的带有RFQItems的RFQ实体如下所示:

    /**
 * @ORM\ManyToMany(targetEntity="RFQItem", cascade={"persist"})
 * @ORM\JoinTable(name="rfq_item_title",
 *      joinColumns={@ORM\JoinColumn(name="rfq_item_title", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="id", referencedColumnName="id")}
 * )
 */
protected $rfq_item_title;

/**
 * @ORM\ManyToMany(targetEntity="RFQItem", cascade={"persist"})
 * @ORM\JoinTable(name="rfq_item_description",
 *      joinColumns={@ORM\JoinColumn(name="rfq_item_description", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="id", referencedColumnName="id")}
 * )
 */
protected $rfq_item_description;

/**
 * @ORM\ManyToMany(targetEntity="RFQItem", cascade={"persist"})
 * @ORM\JoinTable(name="rfq_item_amount",
 *      joinColumns={@ORM\JoinColumn(name="rfq_item_description", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="id", referencedColumnName="id")}
 * )
 */
protected $rfq_item_amount;

But I know that this is wrong, but how I make ManyToMany relation with RFQitem which have more than one attributes? 但是我知道这是错误的,但是我如何与具有多个属性的RFQitem建立ManyToMany关系呢?

The best way to go is to have this two entities as you are actually doing, the father and the collection childs with the attributtes you like, but do not get fixated to the Symfony example. 最好的方法是使这两个实体像您实际所做的那样,具有您喜欢的属性的父亲和孩子集合,但不要局限于Symfony示例。 It's a theoretical OOP, has not relations defined, so I'm going to make my best try to paste a coherent example based on a Theater->Works collection: 这是一个理论上的OOP,没有定义关系,因此我将尽最大努力根据Theatre-> Works集合粘贴一个连贯的示例:

class Theater
{
private $name;
private $id;

/**
 * Set name
 * @param string $name
 * @return Theater
 */
public function setName($name)
{
    $this->name = $name;
    return $this;
}

/**
 * Get name
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Get id
 * @return integer 
 */
public function getId()
{
    return $this->id;
}
/**
 * @var \Doctrine\Common\Collections\Collection
 */
private $work;

/**
 * Constructor
 */
public function __construct()
{
    $this->work = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add work
 *
 * @param \Acme\RelationBundle\Entity\Work $work
 * @return Theater
 */
public function addWork(\Acme\RelationBundle\Entity\Work $work)
{
    $this->work[] = $work;
    return $this;
}

/**
 * Remove work
 * @param \Acme\RelationBundle\Entity\Work $work
 */
public function removeWork(\Acme\RelationBundle\Entity\Work $work)
{
    $this->work->removeElement($work);
}

/**
 * Get work
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getWork()
{
    return $this->work;
}
}

Then the child entity Work: 然后子实体工作:

class Work
{
// took out some comments to make it shorter
private $name;
private $description;
private $id;


/**
 * Set name
 * @param string $name
 * @return Work
 */
public function setName($name)
{
    $this->name = $name;
    return $this;
}

/**
 * Set description :  And any others setters/getters with the attributes you want
 * @param string $description
 * @return Work
 */
public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

/**
 * Get name
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Get description
 * @return string 
 */
public function getDescription()
{
    return $this->description;
}

/**
 * Get id
 * @return integer 
 */
public function getId()
{
    return $this->id;
}


public function __toString()
{
    return (string) $this->getName();
}
}

The trick is to use this Collection in Doctrine and then make a two Form Types, the parent and the childs, something like this example below. 诀窍是在Doctrine中使用此Collection,然后创建两个Form Types,parent和childs,类似于下面的示例。

TheatherType Formtype includes the Work childs: TheatherType表单类型包括Work子项:

          $buider->add('rowswork', 'collection', array(
                'type' => new WorkChildType(),
                'allow_add' => true,
                'allow_delete' => true,
            )
        );

So there is one row with their Work childs that have their own WorkChildType with the attributes from the entity. 因此,一行带有其工作子项的Work子项具有自己的WorkChildType和来自实体的属性。 It's like a form, with an embedded array collection of items, in your case an "RFQ" father form and "RFQitem" childs. 它就像一个表单,具有嵌入式项目数组,在您的情况下是“ RFQ”父表单和“ RFQitem”子表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM