0

I have a form for adding data into budget table. Its form has, in some part of it, a list of data from another table called material that simply lists the name of each material from the database. Beside of each item have two inputs field, one for quantity and one for price, that belong to budget table. Bellow is the code I wrote in my view:

{% for material in materials %}
    <div class="col-lg-2">
        {{ material.name }}
    </div>
    <div class="col-lg-10">
        <input type="hidden" class="material-price" value="{{ material.price }}"/>
        <input class="n-materials" type="number" size="15" name="cdg_budget_type[quantity]" value="0"/>
        - R$
        <input class="final-price" type="number" size="5" name="cdg_budget_type[price]" value="0" disabled/>
    </div>
{% endfor %}

The hidden field is just used to multiply the quantity for price via JavaScript and printing it in the price field the total. That was the solution I found.

The field materials from budget was created as array type, so I need to know how to save this into the table, for example like this:

"Material A" = [
    ["quantity" => 12],
    ["price" => 124]
],
...
1

2 Answers 2

2

As soon you need to store a whole array into a database field you can be sure that your database model is wrong. What you need is a new Entity and a new database table. How you want to call this table is your own choice but lets say that you will call it 'necessities' for example. Now your Budget table will have a OneToMany relation with your new table Necessities.

You can setup your Necessities table on two manners:

-id -material_id

In this situation your budget prices will change if you update your material prices.

OR

-id -quantity -price

In this situation your budget calculations will not change

Take a cup of coffee, a paper and a pencil and give yourself time to think about the datamodel.

Sign up to request clarification or add additional context in comments.

3 Comments

I guessed I'd have to do something like this, it'd be the best way.
Lets hope i pushed you in the right direction. Feel free to ask
Yes, I imagined I had to create a new table for this, the problem now is how to retrieve the ID of the selected material and save it into another table.
0

Let Doctrine help you. Add a One-to-Many to your entity. See the docs for all types of relationships. Dont forget to run the app/console doctrine:generate:entities command to auto generate the methods.

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

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

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

    /**
     * @ORM\OneToMany(targetEntity="Necessity", mappedBy="budget")
     **/
    private $necessities;

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

}

and the Necessity Entity:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

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

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

    /**
     * @var float
     *
     * @ORM\Column(name="price", type="float")
     */
    private $price;

    /**
     * @ManyToOne(targetEntity="Budget", inversedBy="necessities")
     * @JoinColumn(name="budget_id", referencedColumnName="id")
     **/
    private $budget;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.