ShoppingList包含许多水果和水果包含在许多具有额外字段数量的ShoppingList中。
我创建了一个名为ShoppingRow的多对多实体 所以ShoppingList有ManyToOne和ShoppingRow,而ShoppingRow有ManyToOne with Fruits
在ShoppingList的动作newAction中,我遇到了一个问题: 使用params [1,null,11]执行'INSERT INTO shopping_row(quantity,shopping_list_id,fruit_id)VALUES(?,?,?)'时发生异常:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'shopping_list_id' cannot be null
源代码:
/**
* ShoppingList
*
* @ORM\Table(name="shopping_list")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ShoppingListRepository")
*/
class ShoppingList
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ShoppingList
* @ORM\OneToMany(targetEntity="ShoppingRow", mappedBy="shoppingList", cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $shoppingRows;
class ShoppingListType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('shoppingRows', CollectionType::class, array(
'entry_type' => ShoppingRowType::class,
'allow_add' => true,
))
;
}/**
/**
* ShoppingRow
*
* @ORM\Table(name="shopping_row")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ShoppingRowRepository")
*/
class ShoppingRow
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ShoppingList
*
* @ORM\ManyToOne(targetEntity="ShoppingList", inversedBy="shoppingRows")
* @ORM\JoinColumn(name="shopping_list_id", referencedColumnName="id", nullable=FALSE)
*/
private $shoppingList;
/**
* @var Fruit
*
* @ORM\ManyToOne(targetEntity="Fruit", inversedBy="shoppingRows")
* @ORM\JoinColumn(name="fruit_id", referencedColumnName="id", nullable=FALSE)
*/
private $fruit;
/**
* @var int
*
* @ORM\Column(name="quantity", type="integer")
*/
private $quantity;
class ShoppingRowType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('quantity')
->add('fruit', EntityType::class, array(
'class' => 'AppBundle:Fruit',
'choice_label' => 'name',
));
;
}/**
/**
* Creates a new shopping list entity.
*
* @Route("/new", name="shopping_list_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$shoppingList = new ShoppingList();
$form = $this->createForm('AppBundle\Form\ShoppingListType', $shoppingList);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($shoppingList);
$em->flush();
return $this->redirectToRoute('shopping_list_show', array('id' => $shoppingList->getId()));
}
return $this->render('shopping_list/new.html.twig', array(
'shoppingList' => $shoppingList,
'form' => $form->createView(),
));
}
答案 0 :(得分:0)
您必须手动设置相关实体。
$shoppingRow->setShoppingList($shoppingList)
因此,例如,如果您要将$ shoppingRow添加到现有的$ shoppingList,例如,从表单中,您需要获取该列表并将shoppingList添加到购物行。有很多方法可以确定它是哪个列表,这是您的商业模式 - 您是否在列表中,并且您可以通过在URI中传递它的ID,或者通过param转换器访问它通过表单传递ChoiceType :: class,您可以在其中查询您的数据库中的现有列表。这是我们无法帮助您的地方,您需要自己解决这个问题。
现在,当你弄清楚如何进入你想要的列表时,你可以将你的代码放在一个控制器中,或者在处理你的ShoppingLists的某种服务中采用这种方法。