John Davidson

php - Symfony 5 embed form not persist foreign key

0 comments
Message:


i start to study symfony and try to make a relation between two entities with an embed form, i can persist the news information inside my entity but the foreign key is not persisted.
i try to search everywhere before disturbing and i hope somebody can help me.
Thanks in advance.


WorkEntity.php


/**
* @ORM\Entity(repositoryClass=WorkEntityRepository::class)
*/
class WorkEntity
{
/** other attribut**/

/**
* @ORM\OneToMany(targetEntity=Address::class, mappedBy="workEntity",cascade={"persist"})
*/
private $address;

/**
* WorkEntity constructor.
*/
public function __construct()
{

$this->address = new ArrayCollection();
}
/**
* @return Collection|Address[]
*/
public function getAddress(): Collection
{
return $this->address;
}

/**
* @param Address $address
* @return $this
*/
public function addAddress(Address $address): self
{
if (!$this->address->contains($address)) {
$this->address[] = $address;
$address->setWorkEntity($this);
}

return $this;
}

/**
* @param Address $address
* @return $this
*/
public function removeAddress(Address $address): self
{
if ($this->address->removeElement($address)) {
// set the owning side to null (unless already changed)
if ($address->getWorkEntity() === $this) {
$address->setWorkEntity(null);
}
}

return $this;
}
}

WorkEntityController.php


/**
* @Route("/{id}/edit", name="work_entity_edit", methods={"GET","POST"})
* @param Request $request
* @param WorkEntity $workEntity
* @return Response
*/
public function edit(Request $request, WorkEntity $workEntity): Response
{
$form = $this->createForm(WorkEntityType::class, $workEntity);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', 'Modifié avec succès');

return $this->redirectToRoute('work_entity_index');
}

return $this->render('admin/work_entity/edit.html.twig', [
'work_entity' => $workEntity,
'form' => $form->createView(),
'current_menu' => 'work_entity',
'current_section' => 'administrative'
]);
}

WorkEntityType.php


class WorkEntityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
/**other type**/

->add('address', CollectionType::class, [
'entry_type' => AddressType::class,
'label' => 'Adresse de la société',
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
// 'by_reference' => false,

])
}

When i use by reference i got an issue like don't find the attributes for relying my address to workEntity.
Error with reference => (Could not determine access type for property "address" in class "App\Entity\WorkEntity".)


Without by_reference i can persist the entity inside address but not the foreign key.
if you know what i can did for add the fereign key inside my address, i thank you in advance.



My Answer:
"WorkEntity", cascade={"persist"})_ */_ private $address;_ public function __construct()_ {_ $this->address = new ArrayCollection();_ }_ // Getters and setters for address_}_Address.php_/**_ * @ORM\Entity(repositoryClass=AddressRepository::class)_ */_class Address_{_/** other attribut**/__ /**_ * @ORM\ManyToOne(targetEntity=WorkEntity::class, inversedBy="address")_ */_ private $workEntity;_ // Getters and setters for workEntity_}_WorkType.php_/**_ * @ORM\Entity(repositoryClass=WorkTypeRepository::class)_ */_class WorkType_{_/** other attribut**/__ /**_ * @ORM\OneToMany(targetEntity=WorkEntity::class, mappedBy="workType")_ */_ private $workEntity;_ public function __construct()_ {_ $this->workEntity = new ArrayCollection();_ }_ // Getters and setters for workEntity_}_WorkTypeFormType.php_/**_ * @ORM\Entity(repositoryClass=WorkTypeFormTypeRepository::class)_ */_class WorkTypeFormType extends AbstractType_{_ public function buildForm(FormBuilderInterface $builder, array $options)_ {_ $builder_ ->add('name')_ ->add('workEntity', CollectionType::class, array(_ 'entry_type' => WorkEntityType::class,_ 'allow_add' => true,_ 'by_reference' => false,_ ));_ }_}_WorkEntityType.php_/**_ * @ORM\Entity(repositoryClass=WorkEntityTypeRepository::class)_ */_class WorkEntityType extends AbstractType_{_ public function buildForm(FormBuilderInterface $builder, array $options)_ {_ $builder_ ->add('address', EntityType::class, array(_ 'class' => Address::class,_ 'choice_label' => 'name',_ ));_ }_}_Now, when you submit the form, the WorkEntity and Address entities should be persisted with the correct foreign key relationship. Make sure to check your database schema and relationships to ensure everything is set up correctly.

Rate this post

4 of 5 based on 2593 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us