I have profiles and studies. One person can complete a lot of research. The form is displayed correctly. There is a “Add New Study” button, and with jQuery I am adding another subform based on the data prototype, and it works well. When I submit this form with new subforms, I get a database error
Integrity constraint violation: 1048 Column 'profile_id' cannot be null
I understand this error, but I do not know how to get rid of it. I know that I can update the research collection after binding to the controller, but I hope there is a way to configure it correctly in the annotations. When I only update objects, everything works fine.
The code:
class Profile {
private $profileId;
...
private $study;
public function __construct()
{
$this->study = new \Doctrine\Common\Collections\ArrayCollection();
}
...
}
and
class Study {
private $studyId;
...
private $profile;
...
}
with s (g) etters. Database Structure Substrate
CREATE TABLE IF NOT EXISTS `profile` (
`profile_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`profile_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `study` (
`study_id` int(11) NOT NULL AUTO_INCREMENT,
`profile_id` int(11) NOT NULL,
PRIMARY KEY (`study_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `study`
ADD CONSTRAINT `study_fk2` FOREIGN KEY (`profile_id`) REFERENCES `profile` (`profile_id`);
Forming buidlers:
class ProfileType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('study', 'collection', array(
'type' => new StudyType(),
'allow_add' => true,
'allow_delete' => true
)
)
}
...
}
and
class StudyType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('city')
}
...
}
Javascript Part
function profileNewStudy() {
var nr = $('[class^=profile_study_][class*=type2]').last().parent();
nr = nr.attr('id').split('_');
nr = nr.pop()*1 + 1;
var proto = $('#profile_study').data('prototype');
proto = proto.replace(/\$\$name\$\$/g, nr);
$('#profile_study').append(proto).find('.profile_study_' + nr + ' input').val('qpa' + nr);
}
and twig template
<form action="#" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" value="Zapisz"/>
</form>
NOT NULL study.profile_id, , study.profile_id = null.
@Gremo
. , :(
class Profile
/**
* @var Study
* @ORM\OneToMany(targetEntity="Study", mappedBy="profile")
*/
private $study;
class Study
/**
* @var Profile
* @ORM\ManyToOne(targetEntity="Profile", inversedBy="study")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE", referencedColumnName="profile_id")
*/
private $profile;
, : "Alden\xxxBundle\Entity\Profile # study", entity: Alden\xxxBundle\Entity\Study @0000000073eb1a8b00000000198469ff. persist . , , "Alden\xxxxBundle\Entity\Study #__ toString()", .
, :
/**
* @var Study
* @ORM\OneToMany(targetEntity="Study", mappedBy="profile", cascade={"persist"})
*/
private $study;
, : SQLSTATE [23000]: : 1048 Column 'profile_id' .
:
$request = $this->getRequest();
$r = $this->getProfileRepository();
$profile = $id ? $r->find($id) : new \Alden\BonBundle\Entity\Profile();
$form = $this->createForm(new ProfileType(), $profile);
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
{
$vacation = $profile->getVacation();
foreach($vacation as $v) {
$r=5;
}
$em = $this->getEntityManager();
$em->persist($profile);
$em->flush();
}
}
return array(
'profile' => $profile,
'form' => $form->createView()
);
Profile , orphanRemoval foreach loop setStudies() ( @Parhs)
private $studies;
public function __construct()
{
$this->studies = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getStudies()
{
return $this->studies;
}
public function setStudies($studies)
{
foreach ($studies as $v)
{
if (is_null($v->getProfile()))
{
$v->setProfile($this);
}
}
$this->studies = $studies;
}
/**
* @var Profile
* @ORM\ManyToOne(targetEntity="Profile", inversedBy="studies")
* @ORM\JoinColumn(name="profile_id", nullable=false, onDelete="CASCADE", referencedColumnName="profile_id")
*/
private $profile;
.