1

I have a Symfony project with the Entities: PurchaseOrder, Article, Supplier. An order can have multiple articles and an order can have multiple suppliers. You can set the amount of each article through the ArticleOrderReference (Entity).

I build a form, where I can add Articles to an order with the Amount (Entry in ArticleOrderReference). Furthermore I have another form, where I can add suppliers to the order.

But now my problem is, that I want to create a form, where i have an amount input field (for every Article related to this Order in the ArticleOrderReference), which should appear for every supplier related to the order. For example:

Order 1:

Article1     [amount InputField for Supplier1] [amount InputField for Supplier2] ...  
Article2     [amount InputField for Supplier1] [amount InputField for Supplier2] ...

So I build another Entity named ArticleOrderSupplierReference, which includes the supplier_id , ArticleOrderReference_id and the amount .

I have no clue, how to realize this. Some guys told me to use embedded Collections, but i don't know how to use them in such a situation. Any ideas?

ArticleOrderReference:

class ArticleOrderReference
{
   ...

/** @ORM\ManyToOne(targetEntity="Article", inversedBy="articles") */
protected $article;

/** @ORM\ManyToOne(targetEntity="PurchaseOrder", inversedBy="purchaseOrders") */
protected $purchaseOrder;

/**
 * @var integer
 *
 * @ORM\Column(name="amount", type="integer")
 */
private $amount;
...
}

Article:

class Article
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /** @ORM\OneToMany(targetEntity="ArticleOrderReference", mappedBy="article") */
    protected $articles;

    public function __construct() {
        $this->articles = new ArrayCollection();
    }...

Order:

class PurchaseOrder
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

    /** @ORM\OneToMany(targetEntity="ArticleOrderReference", mappedBy="purchaseOrder") */
    protected $purchaseOrders;

    /**
     * @ORM\ManyToMany(targetEntity="Supplier", inversedBy="purcaseOrders")
     * @ORM\JoinTable(name="purchaseOrders_suppliers")
     */
    private $suppliers;

    public function __construct()
    {
        $this->purchaseOrders = new ArrayCollection();
        $this->suppliers = new ArrayCollection();
    }...

Supplier:

class Supplier
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;


    /**
     * @ORM\ManyToMany(targetEntity="purchaseOrder", mappedBy="suppliers")
     */
    private $purchaseOrders;

    public function __construct() {
        $this->purchaseOrders = new ArrayCollection();
    }

ArticleOrderSupplierReference:

 class AOSupplierReference
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
         */
    private $id;

/** @ORM\ManyToOne(targetEntity="ArticleOrderReference", inversedBy="articleOrderReferences") */
protected $articleOrderReference;

/** @ORM\ManyToOne(targetEntity="Supplier", inversedBy="suppliers") */
protected $supplier;

/**
 * @var integer
 *
 * @ORM\Column(name="amount", type="integer")
 */
private $amount;
ChrisS
  • 736
  • 2
  • 8
  • 21

1 Answers1

0

I solved it with double embedded collections.

First i built three FormTypes:

With the First one i collect all the related ArticleOrderReferences: in my controller i call this: $form = $this->createForm(new AmountOrderType(), $order); and return it to my twig file.

$builder->add('purchaseOrders', 'collection', array('type' => new AmountOrderArticleType()));

With the second one i collect all related ArticleOrderSupplierReferences:

$builder->add('articleOrderReferences', 'collection', array('type' => new AmountOrderSubArticleType()));

And with the third one I set the amount:

$builder->add('amount');

The twig file:

{% for purchaseOrder in form.purchaseOrders %}
                        <span class="articleOrderReferences">
                            {% for articleOrderReference in purchaseOrder.articleOrderReferences %}
                                {{ form_errors(articleOrderReference.amount) }}
                                {{ form_widget(articleOrderReference.amount, { 'attr': {'class': 'input-mini'} }) }}
                            {% endfor %}<br>
                        </span>

{% endfor %}
ChrisS
  • 736
  • 2
  • 8
  • 21