Reputation: 2679
I have an entity Travel has an attribute country and I have an entity City related to Travel. I'd like that when I choose a country showing all cities related. In fact I don't know Ajax and I need a help
class TravelType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('country', 'country', array(
'required' => true,
'label' => 'Country',
))
->add('destination', 'entity',array(
'required' => true,
'class' => 'ProjectAdminBundle:City',
'property' => 'name',
'multiple' => false,
'expanded' => false,
'empty_value' => 'Choose a city',
'label' => 'Destination',
))
//.....
}
}
and this is entity Travel:
class Travel
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="country", type="string", length=255, nullable=false)
*
* @Assert\Country
*/
protected $country;
/**
* @ORM\ManyToOne(targetEntity="Project\AdminBundle\Entity\City", inversedBy="travels")
* @ORM\JoinColumn(nullable=false)
*/
protected $destination;
//........
}
Each city has a country code, for exemple:
London -> UK
Paris -> FR .....
Upvotes: 1
Views: 54
Reputation: 1373
A similar case like yours is discussed in details in the Symphony Cookbook:
Upvotes: 1