1
I have two related tables: material and items_budget. The table items_budget has a form that has a field that lists the name of all material in a group of checkbox
, and next to each checkbox
has two fields input
, one for Quantity and another to price. Below is the code for the listing of 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 Rigger that runs every time new data is persisted in the table items_budget. It subtracts the Quantity inserted from currentQuantity of 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 becomes NULL after submitting the form
I tried to add at the end of each attribute name
a pair of square brackets, but in 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 a picture of the steps that I followed. From top left to bottom right: first I checked the option that corresponds to the first table record material, entered a random value for Quantity and then submit the form. According to the second image, everything worked normally. But now, the other two images demonstrate the problem. I checked any other option and entered again with a value for the Quantity, but this time the field is NULL, as can be seen in the last image.
Here, only the class ItemsBudgetController
and its method addAction
:
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, the ItemsBudgetType
, where I set the field material
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 makes no sense. Why only the first record has its field Quantity "seen"? If you need me to post more codes, I will update the question.
Issue #1
I’ll show you the relationship between the tables. When checking how to make a field like Collection with an entity, I understood that it only works with manyToMany relations.
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: { }
So, ItemsBudget
:
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: { }
I don’t quite understand your question, maybe you should clean up and be clearer. But still, it sounds to me like you need to use Collection Field Type: http://symfony.com/doc/current/reference/forms/types/collection.html
– felipsmartins
@felipsmartins Hi! That first code shown is what creates the
checkbox
, according to the picture. The situation is that theinput
to enter a quantity, when submitting the form, they are NULL, except for the first record of the table material, only this problem, understands?– fermoga
@felipsmartins I have a table items_budget specific to save the budget and material idenficator, and if there is more than one, iterate on that result set and persist in the table. Would this collection really be ideal?
– fermoga
I’m not sure if Collection Type Field is the option, I’m just wary of my general understanding of what you addressed (because only the first field is recorded as well) and also of what I’ve been through before. There was a system where there were entities OCCURRENCES and ATTACHMENTS. AN OCCURRENCE may have several ATTACHMENTS so I needed to use Collection Type Field in my OCCURRENCE form. I don’t have time to evaluate the case better, but if you want to add me in Skype, my ID is: fmartinsdev
– felipsmartins
@felipsmartins I get it. Actually, seeing the image, all the MATERIAL I select will be recorded, but only the Quantity is NULL. Note that not even the price stays, because it appears with "0". There is some problem only with that field.
– fermoga
@felipsmartins I was seeing creation of Collections with entities, and it seems that to work the tables must have a manyToMany relation, correct? Is the relationship with the tables mentioned in the question wrong? I will update with this information
– fermoga