samedi 19 septembre 2015

The field of an entity is NULL, except for the first register in the table

My system has two related tables called material and items_budget. The items_budget table has a form that lists the name of each material as a checkbox, followed by two more input, one for its quantity and the other one for its price. Here is the code:

<strong>Select the Budget</strong>
<div class="form-group">
    <select name="cdg_itemsbudget_type[budget]" class="form-control">
        {% for budget in budgets %}
            <option value="{{ budget.id }}">{{ budget.client }}</option>
        {% endfor %}
    </select>
</div>

<strong>Materials</strong>
{% for material in materials %}
    <div class="checkbox">
        <label>
            <input type="checkbox" class="itemsbudget_material" name="cdg_itemsbudget_type[material]" value="{{ material.id }}"> {{ material.name }} - 
        </label>
        <input type="hidden" class="itemsbudget_price_hidden" value="{{ material.price }}"/>
        <input type="text" class="itemsbudget_quantity" name="cdg_itemsbudget_type[quantity]" placeholder="Qtd" size="3"/>x - R$
        <input type="text" class="itemsbudget_price" name="cdg_itemsbudget_type[price]" value="0" size="3" readonly/>
    </div>
{% endfor %}

The ItemsBudgetType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('budget', 'entity', array(
                'class' => 'PanelBundle:Budget',
                'attr' => array(
                    'class' => 'form-control',
                ),
            ))
            ->add('material', 'entity', array(
                'class' => 'PanelBundle:Material',
                'attr' => array(
                    'required' => true,
                ),
            ))
            ->add('quantity', 'number', array(
                'attr' => array(
                    'class' => 'form-control',
                ),
            ))
            ->add('price', 'money', array(
                'attr' => array(
                    'class' => 'form-control',
                ),
            ));
}

The ItemsBudgetController:

public function addAction(Request $request)
{
    $form = $this->createForm(new ItemsBudgetType());
    $manager = $this->getDoctrine()->getManager();

    if ($request->getMethod() == 'POST') {
        $form->handleRequest($request);

        if ($form->isValid()) {
            $ItemsBudgetEntity = $form->getData();
            $manager->persist($ItemsBudgetEntity);
            $manager->flush();

            $BudgetEntity = $form->get('budget')->getData();
            $BudgetEntity->addItemsBudget($ItemsBudgetEntity);

            $manager->persist($BudgetEntity);
            $manager->flush();

            $this->addFlash('success', 'Materiais para o orçamento adicionados');

            return $this->redirect($this->generateUrl('panel_budgets'));
        }
    }

    return $this->render('PanelBundle:ItemsBudget:index.html.twig', array(
        'budgets' => $manager->getRepository('PanelBundle:Budget')->findAll(),
        'materials' => $manager->getRepository('PanelBundle:Material')->findAll()
    ));
}

A trigger is executed when new data is inserted into items_budget. This trigger subtracts the given quantity from the current quantity of the selected material in the form. The problem is that it only works properly with the FIRST register of the material table, with the others, the quantity value entered returns NULL after submitting the form. Does anyone know the reason it is happening?




Aucun commentaire:

Enregistrer un commentaire