Symfony2 - ReferencedColumnName id为null [英] Symfony2 - ReferencedColumnName id is null

查看:155
本文介绍了Symfony2 - ReferencedColumnName id为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关于表单收藏的食谱文章当尝试将此持久化到数据库时,我得到一个约束违规错误,引用列名称为空。

I'm going off the cookbook article on form collections however when trying to persist this to the database, I am getting a constraint violation error with the referencedcolumn name id being null.

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'client_id' cannot be null

我相信实体已设置正确和相关的是否正确,有没有什么我需要添加到我的表单,我失踪了?

I believe the entities are setup correctly and related properly, is there something I need to add to my form that I'm missing?

客户

/**
 * @ORM\OneToMany(targetEntity="ClientPhone", mappedBy="clients", cascade={"persist"})
 */
protected $clientphones;

客户端电话

/**
 * @ORM\ManyToOne(targetEntity="Client", inversedBy="clientphones")
 * @ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=false)
 */
protected $clients;

ClientType表单

ClientType form

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('firstName', 'text', array(
            'label' => 'First Name'
    ))
        ->add('lastName', 'text', array(
            'label' => 'Last Name'
    ))
        ->add('email', 'text', array(
            'label' => 'E-mail Address'
    ))
        ->add('clientphones', 'collection', array(
            'type'         => new ClientPhoneType(),
            'allow_add'    => true,
            'allow_delete' => true,
            'by_reference' => false,
    ));
}

ClientPhoneType表单

ClientPhoneType form

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('home', 'text');
    $builder->add('office', 'text');
    $builder->add('mobile', 'text');
}

ClientController

ClientController

$client = new Client();

    $phone = new ClientPhone();
//        $phone->home   = '2134959249';
//        $phone->office = '2134959249';
//        $phone->mobile = '2134959249';
    $client->getClientPhones()->add($phone);

    $form = $this->createForm(new ClientType(), $client, array(
        'action' => $this->generateUrl('client'),
        'method' => 'POST',
    ));
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $em->persist($client);
        $em->flush();

        $session = $request->getSession();
        $session->getFlashBag()->add('message', 'Client successfully saved to database');

        return $this->redirect($this->generateUrl('client'));
    }


推荐答案

问题是客户端实体在 addClientphone 函数中。我必须更改预生成的代码:

Figured out the problem. The problem was with the Client entity in the addClientphone function. I had to change the pre-generated code:

$this->clientphones[] = $clientphones;

如下:

if (!$this->clientphones->contains($clientphone)) {
        $clientphone->setClients($this);
        $this->clientphones->add($clientphone);
    }

    return $this->clientphones;

这篇关于Symfony2 - ReferencedColumnName id为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆