本文介绍了嵌入形式的集合 - 学说,symfony的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表RFQ和RFQitem。我可以制作形式,可以创建RFQ与他们的标题描述和金额。我可以创建RFQitem表单,可以创建RFQitem的标题,描述和金额。



当我需要升级我的RFQ表单时,问题开始,以便我可以在它的RFQitems将保存在表中,但需要分配给RFQ。



在symfony文档中是非常好的,其实际上适用于我,但这是例子是与任务和他们的标签。所以任务有多个属性(名称,描述),但是标签只能使用一个名字。



我的RFQ实体带有RFQItems,如下所示: p>

  / ** 
* @ 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;

但是我知道这是错误的,但是我如何使ManyToMany与RFQitem的关系有多个属性?

解决方案

最好的方法是让这两个实体按照你的实际做,父亲和收藏的孩子与你喜欢的属性,但不要被修改到Symfony的例子。这是一个理论上的OOP,没有定义关系,所以我将尽力尝试粘贴一个基于Theater-> Works集合的连贯示例:

  class Theater 
{
private $ name;
private $ id;

/ **
*设置名称
* @param string $ name
* @return Theater
* /
public function setName $ name)
{
$ this-> name = $ name;
return $ this;
}

/ **
*获取名称
* @return string
* /
public function getName()
{
return $ this-> name;
}

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

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

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

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

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

然后孩子实体工作:

  class Work 
{
//取出一些注释使其更短
private $ name;
private $ description;
private $ id;


/ **
*设定名称
* @param string $ name
* @return工作
* /
public function setName($ name)
{
$ this-> name = $ name;
return $ this;
}

/ **
*设置描述:还有任何其他设置器/ getter与你想要的属性
* @param string $ description
* @return Work
* /
public function setDescription($ description)
{
$ this-> description = $ description;

return $ this;
}

/ **
*获取名称
* @return string
* /
public function getName()
{
return $ this-> name;
}

/ **
*获取描述
* @return string
* /
public function getDescription()
{
return $ this-> description;
}

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


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

诀窍是在Doctrine中使用此集合,然后制作两个表单类型,父类和子类,如下面的示例。



TheatherType Formtype包括工作子项:

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

);

所以有一行他们的Work小孩有自己的WorkChildType与实体的属性。它就像一个表单,嵌入式数组的项目集合,在您的情况下,RFQ父亲表单和RFQitem子项。


I have two tables "RFQ" and "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.

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. 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:

    /**
 * @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?

解决方案

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. 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:

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.

TheatherType Formtype includes the Work childs:

          $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. It's like a form, with an embedded array collection of items, in your case an "RFQ" father form and "RFQitem" childs.

这篇关于嵌入形式的集合 - 学说,symfony的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 12:30