В ShoppingList содержится много фруктов, а во многих списках ShoppingList содержится больше фруктов.
Я создал объект «Многие ко многим» под названием ShoppingRow
Таким образом, в ShoppingList есть ManyToOne с ShoppingRow, а в ShoppingRow есть ManyToOne с фруктами
В действии newAction of ShoppingList я получаю проблему:
Возникла исключительная ситуация при выполнении 'INSERT INTO shopping_row (amount, shopping_list_id, fruit_id) VALUES (?,?,?)' С параметрами [1, ноль, 11]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'shopping_list_id' cannot be null
исходный код:
ShoppingList Entity:
/**
* 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;
ShoppingListType:
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 Entity:
/**
* 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;
ShoppingRowType:
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',
));
;
}/**
ShoppingListController:
/**
* 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(),
));
}