Search code examples
phpformssymfonydoctrineformbuilder

how to submit form values in 2 different tables symfony


I want to submit a form and store the values in 2 different tables (that are in the same database).

This is the form:

<div class="control-group">
      <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>Naam</label>

      <div class="controls">
            {{ form_widget(form.product) }}
            {{ form_errors(form.product) }}
      </div>
</div>
<div class="control-group">
      <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>Aantal</label>

      <div class="controls">
           {{ form_widget(form.amount) }}
           {{ form_errors(form.amount) }}
      </div>
</div>

This is the form builder:

$builder->add("amount", "number", array("label" => "Aantal"));
$builder->add("product", "text", array("attr" => array("autocomplete" => "off", "data-provide" => "typeahead", "data-items" => "15", "data-source" => $dataSource), 'mapped' => false));
$builder->add("price", "number", array("label" => "Aangepaste prijs", 'mapped' => false));

this is a part of the entity:

/**
     * @var integer $id
     */
    private $id;

    /**
     * @var integer $amount
     */
    private $amount;

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

    /**
     * Set amount
     *
     * @param integer $amount
     * @return BookingEntry
     */
    public function setAmount($amount)
    {
        $this->amount = $amount;

        return $this;
    }

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

and here is the ORM:

type: entity
    table: null
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
        amount:
            type: float

Now do I wanna add a entry field by the form with the name 'serial_nr'. I want to store this value in a different table than where 'product' and 'amount' are stored in.

Can someone point me in the right direction?


Solution

  • You have on 'product' and 'price' options 'mapped' => false, so if form is defined on some entity it not save this values, or you do it by yourself. If I understand correctly, you have table (product) with columns:

    • product
    • price
    • amount

    and not you want add table (product_serial) related to it with columns:

    • product_id
    • serial_nr

    and add this one to many relation to your form? You can do it by define this relation in doctrine in table product_serial:

    manyToOne:
        product:
            targetEntity: Product
            inversedBy: serials
            joinColumn:
                name: product_id
                referencedColumnName: id
    

    and on table product:

    oneToMany:
        serials:
            targetEntity: ProductSerial
            mappedBy: product
    

    and add to form collection field 'serials'.

    http://symfony.com/doc/current/reference/forms/types/collection.html