我的数据库结构:
每个杂志都有自己的衣柜,衣柜自己的架子等(一对多的关系)
所以我添加一个到很多关系杂志,衣柜,货架到物品,像这样:
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;
我有表格,
<?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';
}
}
我想添加相关领域衣柜和货架。 如果我选择杂志我想要AJAX获得相关的衣柜等。
怎么做? 如何在表单构建器中添加字段? AJAX如何修改选择? (控制器中的动作?)