i have problem with symfony2:
An exception occurred while executing 'INSERT INTO Municipality (name, region_id) VALUES (?, ?)' with params ["Test message", null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'region_id' cannot be null
This is my controller:
public function createAction(Request $request)
{
$entity = new Municipality();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
$params = $this->getRequest()->request->all();
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
//$entity->setRegionId(21);
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('municipality_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
Bulder:
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('regionId', 'entity', array('class' => 'CMSSurveyBundle:Region',
'query_builder' => function(EntityRepository $er) { return $er->createQueryBuilder('a')->orderBy('a.name', 'ASC'); },
'attr' => array('control-class' => 'col-lg-6', 'class' => 'form-control'))
)
->add('name')
;
}
Entity:
/**
* @var integer
*
* @ORM\Column(name="region_id", type="integer")
*/
private $regionId;
/**
* @ORM\ManyToOne(targetEntity="Region", inversedBy="municipalities")
* @ORM\JoinColumn(name="region_id", referencedColumnName="id")
*/
private $region;
$_POST give to controller:
Array
(
[CMS_surveybundle_municipality] => Array
(
[name] => Test message
[regionId] => 20
[_token] => 8628f62dd8d398b85685def1d368334ay1d7a816
)
)
1
Views:
<hr/>
<table>
<tr>
<td class="col-lg-4">
<?php echo $view['form']->label($form['name']); ?>
</td>
<td class="col-lg-6">
<?php echo $view['form']->errors($form['name']); ?>
<?php echo $view['form']->widget($form['name']); ?>
</td>
</tr>
<tr>
<td class="col-lg-4">
<?php echo $view['form']->label($form['regionId']); ?>
</td>
<td class="col-lg-6">
<?php echo $view['form']->errors($form['regionId']); ?>
<?php echo $view['form']->widget($form['regionId']); ?>
</td>
</tr>
</table>
<hr/>
regionandregion_id? That just doesn't make sense. I suggest removingregionIdand relying on objects instead of identifiers... - Jovan Perovic