My database structure:
Each magazine had own wardrobes, wardrobe own shelfs etc(one to many relations)
So i add one to many relation magazine,wardrobe,shelf to item, like this:
class Item
{
//...
/**
* @ORM\ManyToOne(targetEntity="Raltech\WarehouseBundle\Entity\Magazine", inversedBy="items")
* @ORM\JoinColumn(name="magazine_id", referencedColumnName="id",onDelete="SET NULL")
*/
private $magazine;
/**
* @ORM\ManyToOne(targetEntity="Raltech\WarehouseBundle\Entity\Wardrobe", inversedBy="items")
* @ORM\JoinColumn(name="wardrobe_id", referencedColumnName="id",onDelete="SET NULL")
*/
private $wardrobe;
/**
* @ORM\ManyToOne(targetEntity="Raltech\WarehouseBundle\Entity\Shelf", inversedBy="items")
* @ORM\JoinColumn(name="shelf_id", referencedColumnName="id",onDelete="SET NULL")
*/
private $shelf;
And i had form,
<?php
namespace Raltech\WarehouseBundle\Form\FormType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ItemForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'name',
'text',
array(
'label' => 'Nazwa'
)
)
->add(
'description',
'textarea',
array(
'label' => 'Opis',
'required' => false
)
)
->add(
'category',
'entity',
array(
'empty_value' => '',
'required' => false,
'class' => 'WarehouseBundle:Category',
'property' => 'name',
'label' => 'Kategoria',
)
)
->add(
'magazine',
'entity',
array(
'empty_value' => '',
'required' => false,
'class' => 'WarehouseBundle:Magazine',
'property' => 'name',
'label' => 'Magazyn',
)
)
->add(
'file',
'vich_file',
array(
'label' => 'Załącznik (maksymalnie 50 MB)',
'attr' => array(
'class' => 'filestyle',
'data-buttonName' => 'btn-primary',
'data-buttonText' => "Przeglądaj"
)
,
'required' => false,
'download_link' => false,
)
);
if ($builder->getData()->getId()) { // or !getId()
// $builder->add('delete', 'checkbox'); // or whatever
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array(
'data_class' => 'Raltech\WarehouseBundle\Entity\Item',
)
);
}
public function getName()
{
return 'ItemForm';
}
}
And I want to add related fields wardrobe and shelf. If i choose magazine i want to AJAX get related Wardrobes etc.
How to do this? How add fields in form builder? How AJAX modify select? (action in controller?)