如何在2个不同的表symfony中提交表单值

时间:2022-02-04 15:36:24

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:

这是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.

现在我想通过名称为'serial_nr'的表单添加一个输入字段。我想将此值存储在与“product”和“amount”存储在的表不同的表中。

Can someone point me in the right direction?

有人能指出我正确的方向吗?

1 个解决方案

#1


1  

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'选项'mapped'=> false,所以如果在某个实体上定义了form,它就不会保存这个值,或者你自己做。如果我理解正确,你有表(产品)列:

  • product
  • price
  • amount

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

而不是你想要与列相关的表格(product_serial):

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

并将此一个添加到您的表单的许多关系?您可以通过在表product_serial中的doctrine中定义此关系来实现:

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

并添加到表单集合字段'serials'。

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

#1


1  

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'选项'mapped'=> false,所以如果在某个实体上定义了form,它就不会保存这个值,或者你自己做。如果我理解正确,你有表(产品)列:

  • product
  • price
  • amount

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

而不是你想要与列相关的表格(product_serial):

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

并将此一个添加到您的表单的许多关系?您可以通过在表product_serial中的doctrine中定义此关系来实现:

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

并添加到表单集合字段'serials'。

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