Hi, I have a trouble with a Collection Type in a one-to-many association, I have 2 entities, Sale, that includes mobile devices to put on sale, one have a one-to-many relationship with the IMEI device. When I try to insert a IMEI get next error:
An exception occurred while executing 'INSERT INTO Imei (code, sale_id) VALUES (?, ?)' with params ["4524324234", null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'sale_id' cannot be null
I had followed the post #851 to get a Collection Type with the IMEI association, these are my files:
Sale.php:
/**
* One Product has Many Features.
* @ORM\OneToMany(targetEntity="Imei", mappedBy="sale", cascade={"persist","remove"}, orphanRemoval=true)
*/
private $imeis;
Imei.php
/**
* Many Features have One Product.
* @ORM\ManyToOne(targetEntity="Sale", inversedBy="imeis")
* @ORM\JoinColumn(name="sale_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
*/
private $sale;
ImeyType.php:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Formulario para los correos de contacto.
*
* @author Enrique Jos茅 Esteban Plaza <[email protected]>
*/
class ImeiType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('code', TextType::class);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Imei',
));
}
/**
* @return string
*/
public function getName()
{
return 'appbundle_imei';
}
}
Sale.yml:
- { property: 'imeis', type: 'collection', type_options: { entry_type: 'AppBundle\Form\ImeiType', by_reference: false, required: true } }
I'm not sure what could be happening here. Could you please try to use the default settings for this property and see what happens? Just use this config:
- { property: 'imeis' }
Hi Javier, ty for answering me.
If I use this config, all works fine, but I can not enter a new imei in the form of devices (I finally separated the devices to an own entity), and I have to create them more later, in addition I get a list of imeis that are already in use, to choose one, which is neither useful nor desirable.
Hi enrique,
dealing with same problem here. After hours of research I have found this ticket:
https://github.com/javiereguiluz/EasyAdminBundle/issues/1278#issuecomment-242334673
The problem is the bidirectionnal setup of your relationship. You need to adapt a setter to the correct entity. I'm quoting from the link:
/**
* Add evtdate
*
* @param \AppBundle\Entity\Evtdateate $evtdate
*
* @return Evenement
*/
public function addEvtdate(\AppBundle\Entity\Evtdate $evtdate)
{
$this->evtdates[] = $evtdate;
$evtdate->setEvenement($this); // Add this line
return $this;
}
This resolved the exact same issue for me. Hope I could help.
I'm closing this as fixed by @crowdbytes's answer ... but if this problem persists, please reopen. Thanks!
Tyvm @javiereguiluz and @crowdbytes