Symfony2使用params [" test message",null]执行[...]时发生异常:

时间:2014-09-03 16:02:15

标签: php symfony

我有symfony2的问题:

 使用params [“Test message”,null]执行'INSERT INTO Municipality(name,region_id)VALUES(?,?)'时发生异常:

SQLSTATE [23000]:完整性约束违规:1048列'region_id'不能为空

这是我的控制者:

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(),
    );
}

制作工具的详情:

 /**
 * @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')
    ;
}

实体:

/**
 * @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给控制器:

Array
(
    [CMS_surveybundle_municipality] => Array
        (
            [name] => Test message
            [regionId] => 20
            [_token] => 8628f62dd8d398b85685def1d368334ay1d7a816
        )

)
1 

查看:

            <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/>

2 个答案:

答案 0 :(得分:1)

当您删除regionId时,您是否真的从表单和模板中清除它?考虑使用您的新regionId免费代码更新您的问题。

您还可以查看文档,了解Doctrine 2如何处理关系。想要实际使用id是一个常见的错误。 http://symfony.com/doc/current/book/doctrine.html

现在你没有任何区域与你的市政当局联系。在您的控制器中,您将需要:

$municipality = new Municipality(); // Used to be called entity.  Rename for clarity
$region = new Region();
$municipality->setRegion($region);

$form = $this->createCreateForm($municipality);

答案 1 :(得分:1)

正如另一条评论中所提到的,您的实体中的region和regionId属性都是错误的。它彻底打破了Doctrine2 ORM抽象,可能是你问题的根源。此外,您已创建一个映射到整数的实体表单字段。请参阅下面的建议更改。我已将您的表单的属性映射从regionId更改为region,并完全从regionId实体中删除了Municipality属性。如果您有迫切需要访问regionId,您可以使用$municipality->getRegion()->getId()

生成器:

 /**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('region', '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')
    ;
}

实体:

/**
 * @ORM\ManyToOne(targetEntity="Region", inversedBy="municipalities")
 * @ORM\JoinColumn(name="region_id", referencedColumnName="id")
 */

private $region;