gpt4 book ai didi

forms - Symfony2 OneToMany——从嵌入式表单持久化数据

转载 作者:行者123 更新时间:2023-12-02 00:05:29 24 4
gpt4 key购买 nike

当我想将数据从表单保存到数据库时遇到问题

我有两个简单的实体用户和电话

一个用户可以有多个电话一部电话只能有一个用户

数据库表是:

user [ id, name, email]

telephone [id, user_id, phone_number]

我的表单也有以下字段

  • 一个名字
  • 一封电子邮件
  • 和电话号码

当我填写并提交表单时,出现以下异常

An exception occurred while executing 'INSERT INTO telephone (phone_number, user_id) VALUES (?, ?)' with params [123456, null]:

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

从调试器栏我看到执行了 4 个查询

  1. 开始交易
  2. 插入用户(姓名、电子邮件)值(value)观(?,?)参数:{ 1: ‘姓名’, 2: email@yahoogmail.com }
  3. 插入电话(phone_number, user_id)值(value)观(?,?)参数:{ 1: 123456, 2: null }
  4. 回滚

我明白为什么会出现这个异常,但我不明白为什么user_id会得到NULL值。

这是我的代码(我省略了一些属性的 getter 、 setter 和注解)

用户实体

class User
{

private $name;

private $email;

/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var \Acme\FormBundle\Entity\Telephone
*
* @ORM\OneToMany(targetEntity="Acme\FormBundle\Entity\Telephone", mappedBy="user", cascade={"ALL"})
*/
private $telephones;

/**
* Constructor
*/
public function __construct()
{
$this->telephones = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
* Add telephones
*
* @param \Acme\FormBundle\Entity\Telephone $telephones
* @return User
*/
public function addTelephone(\Acme\FormBundle\Entity\Telephone $telephones)
{
$this->telephones[] = $telephones;
$telephones->setUser($this);

return $this;
}

/**
* Remove telephones
*
* @param \Acme\FormBundle\Entity\Telephone $telephones
*/
public function removeTelephone(\Acme\FormBundle\Entity\Telephone $telephones)
{
$this->telephones->removeElement($telephones);
}

/**
* Get telephones
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTelephones()
{
return $this->telephones;
}
}

电话实体

class Telephone
{
private $phoneNumber;

/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var \Acme\FormBundle\Entity\User
*
* @ORM\ManyToOne(targetEntity="Acme\FormBundle\Entity\User", inversedBy="telephones")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $user;

/**
* Set user
*
* @param \Acme\FormBundle\Entity\User $user
* @return Telephone
*/
public function setUser(\Acme\FormBundle\Entity\User $user = null)
{
$this->user = $user;

return $this;
}

/**
* Get user
*
* @return \Acme\FormBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}

用户类型

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name')
->add('email')
->add('telephones', 'collection', array('type' => new TelephoneType()))
->add('save it', 'submit');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\FormBundle\Entity\User',
));
}

public function getName()
{
return 'user';
}
}

电话类型

class TelephoneType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('phoneNumber');

}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\FormBundle\Entity\Telephone',
));
}

public function getName()
{
return 'telephone';
}
}

和我的 Controller

class DefaultController extends Controller
{
public function indexAction(Request $request)
{
$user = new User();
$Telephone1 = new Telephone();

$user->getTelephones()->add($Telephone1);

$form = $this->createForm(new UserType(), $user);

$form->handleRequest($request);

if ($form->isValid()) {

$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();

}

return $this->render('AcmeFormBundle:Default:index.html.twig', array(
'form' => $form->createView(),
));
}
}

对不起,这篇文章很长

谁能帮帮我?

非常感谢!

最佳答案

我换了

$user->getTelephones()->add($Telephone1);

$user->addTelephone($Telephone1);

在我的 Controller 中

关于forms - Symfony2 OneToMany——从嵌入式表单持久化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18685960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com