Я работаю над ассоциацией OneToMany в моей базе данных. Эта связь отлично работает, когда я пытаюсь добавить данные из приборов и когда я пытаюсь вернуть данные из базы данных.
Проблема с моим FormType CommandType, который не работает. Symfony и Doctrine возвращают это сообщение об ошибке:
Возникла исключительная ситуация при выполнении 'INSERT INTO command_product (amount, command_id, product_id) VALUES (?,?,?)' С параметрами [3, null, 1]: \ n \ nSQLSTATE [23000]: нарушение ограничения целостности: 1048 Столбец 'command_id' не может быть нулевым
Код CommandType:
class CommandType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('establishment', EntityType::class, array(
'class' => Company::class,
'required' => true
))
->add('dateCreation', DateTimeType::class, array(
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'required' => true
))
->add('contains', CollectionType::class, array(
'entry_type' => CommandProductType::class,
'required' => true,
'allow_add' => true
))
->add('state',TextType::class, array(
'required' => true
))
->add('totalAmount', MoneyType::class, array(
'required' => true
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Command::class
));
}
public function getBlockPrefix()
{
return 'appbundle_command';
}
}
Код CommandProductType:
class CommandProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('quantity', NumberType::class, array(
'required' => true
))
->add('product', EntityType::class, array(
'class' => Product::class,
'required' => true
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => CommandProduct::class
));
}
public function getBlockPrefix()
{
return 'appbundle_commandproduct';
}
}
Класс кода команды:
class Command
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Company $establishment
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Company")
* @ORM\JoinColumn(name="establishment_id", referencedColumnName="id", nullable=false)
*/
private $establishment;
/**
* @var DateTime $dateCreation
*
* @ORM\Column(name="dateCreation", type="datetime", nullable=false)
* @Assert\Type("datetime")
*/
private $dateCreation;
/**
* @var string $state
*
* @ORM\Column(name="state", type="string", length=255, nullable=false)
* @Assert\Type("string")
*/
private $state;
/**
* @var float $totalAmount
*
* @ORM\Column(name="totalAmount", type="float", precision=10, scale=2, nullable=false)
* @Assert\NotBlank()
* @Assert\Type(type="float")
*/
private $totalAmount;
/**
* @var mixed $contains
*
* @ORM\OneToMany(targetEntity="AppBundle\Entity\CommandProduct", mappedBy="contain", cascade={"persist", "remove"})
*/
private $contains;
public function __construct()
{
$this->contains = new ArrayCollection();
}
/**
* @var CommandProduct $commandProduct
*/
public function addContain(CommandProduct $commandProduct = null)
{
$commandProduct->setContain($this);
$this->contains->add($commandProduct);
}
/**
* @param CommandProduct $commandProduct
*/
public function removeContain(CommandProduct $commandProduct)
{
if ($this->contains->contains($commandProduct)) {
$this->contains->removeElement($commandProduct);
}
}
}
Класс кода CommandOrder:
class CommandProduct
{
/**
* @var int $id
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var Command $contain
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Command", inversedBy="contains")
* @ORM\JoinColumn(name="command_id", referencedColumnName="id", nullable=false)
*/
private $contain;
/**
* @var int $quantity
*
* @ORM\Column(name="quantity", type="integer", nullable=true, options={"default": 1})
* @Assert\NotBlank()
* @Assert\Type(type="int")
*/
private $quantity;
/**
* @var Product $product
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Product")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
}