I have two related tables: material and items_budget . The items_budget table has a form that has a field that lists the name of all the material in a group of checkbox
, and next to each checkbox
has two % fields, one for quantity and one for price . Below is the code for listing these Material
<strong>Materiais</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 %}
I also have a trigger that runs every time new data is persisted in the items_budget table. It subtracts the quantity from the currentQuantity of the material . It works perfectly, but now comes the problem: it just works for the first record of the material table , because for others the quantity mysteriously gets NULL after submitting the form
I have tried to add a couple of brackets at the end of each attribute input
, but by doing so, the form is simply reloaded and no data is persisted.
I really want to make this as clear as possible, so I put together an image of the steps I followed. From top left to bottom right: I first checked the option that matches the first record of the material table, I entered with a random value for quantity and so I submitted the form. According to the second image, everything worked normally. But now, the other two images demonstrate the problem. I checked another option and entered again with a quantity value, but this time the field becomes NULL, as can be seen in the last image.
Here, only the name
class and its ItemsBudgetController
method:
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()
));
}
And now, addAction
, where I set the ItemsBudgetType
field as an entity:
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',
),
));
}
This does not make sense. Why only the first record has its quantity field seen? If you need me to post more codes, I'll update the question.
Issue # 1
I'll show the relationship between tables. When checking how to make a field as a collection with an entity, I understood that it only works with manyToMany relationships.
First, material
:
CDG\PanelBundle\Entity\Material:
type: entity
table: material
repositoryClass: CDG\PanelBundle\Entity\MaterialRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 255
description:
type: string
length: 255
currentQuantity:
type: integer
column: current_quantity
price:
type: decimal
dateCreated:
type: datetime
oneToMany:
itemsBudget:
targetEntity: ItemsBudget
mappedBy: material
quantityMaterial:
targetEntity: QuantityMaterial
mappedBy: material
lifecycleCallbacks: { }
Then, Material
:
CDG\PanelBundle\Entity\ItemsBudget:
type: entity
table: items_budget
repositoryClass: CDG\PanelBundle\Entity\ItemsBudgetRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
quantity:
type: integer
nullable: true
price:
type: decimal
manyToOne:
budget:
targetEntity: Budget
inversedBy: itemsBudget
joinColumn:
name: budget
referencedColumnName: id
material:
targetEntity: Material
inversedBy: itemsBudget
joinColumn:
name: material
referencedColumnName: id
lifecycleCallbacks: { }