Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

OneToMany relationship issues

I have two entities, PartenairePermission and StructurePermission, I’m trying to get properties from the other entity in a One To Many relationship.

Which was working great with the one to one relationship but I modified it for a One To Many relationship and now I can’t access the properties anymore

Attempted to call an undefined method named "setIsMembersRead" of class "Doctrine\ORM\PersistentCollection".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The idea of the script below is when the property is modified in PartenairePermission, it modified the property in StructurePermission too.

Any idea on how to solve this issue?

PartenaireController: [EDITED]

 #[Route('/{id}/activate-permission', name: 'app_partenaire_activate-permission', methods: ['GET', 'POST'])]
public function activatePermission(EntityManagerInterface $entityManager, Request $request, PartenaireRepository $partenaireRepository, MailerInterface $mailer): Response
{

    $partenairePermission = $entityManager->getRepository(PartenairePermission::class)->findOneBy([ // get the id of the partenaire
        'id' => $request->get('id'),
    ]);

    $partenairePermission->setIsMembersRead(!$partenairePermission->isIsMembersRead()); // set the value of the permission to the opposite of what it is ( for toggle switch )
    $structurePermission = $partenairePermission->getPermissionStructure();

    foreach ($structurePermission as $structurePermission) {
        $structurePermission->setIsMembersRead($partenairePermission->isIsMembersRead());
    }


    $entityManager->persist($partenairePermission);
    $entityManager->flush();

PartenairePermission.php :

    #[ORM\OneToMany(mappedBy: 'permission_partenaire', targetEntity: StructurePermission::class, orphanRemoval: true)]
private Collection $permission_structure;

public function __construct()
{
    $this->permission_structure = new ArrayCollection();
} /**
 * @return Collection<int, StructurePermission>
 */
public function getPermissionStructure(): Collection
{
    return $this->permission_structure;
}

public function addPermissionStructure(StructurePermission $permissionStructure): self
{
    if (!$this->permission_structure->contains($permissionStructure)) {
        $this->permission_structure->add($permissionStructure);
        $permissionStructure->setPermissionPartenaire($this);
    }

    return $this;
}

public function removePermissionStructure(StructurePermission $permissionStructure): self
{
    if ($this->permission_structure->removeElement($permissionStructure)) {
        // set the owning side to null (unless already changed)
        if ($permissionStructure->getPermissionPartenaire() === $this) {
            $permissionStructure->setPermissionPartenaire(null);
        }
    }

    return $this;
}

StructurePermission.php :

    #[ORM\ManyToOne(fetch: "EAGER", inversedBy: 'permission_structure')]
    #[ORM\JoinColumn(nullable: false)]
    private ?PartenairePermission $permission_partenaire = null;
public function getPermissionPartenaire(): ?PartenairePermission
    {
        return $this->permission_partenaire;
    }

    public function setPermissionPartenaire(?PartenairePermission $permission_partenaire): self
    {
        $this->permission_partenaire = $permission_partenaire;

        return $this;
    }

>Solution :

Now you have to work different since you changed the association type:

 $structurePermission = $partenairePermission->getPermissionStructure();

this will return a Collection (instead of a single Object as with your former One-to-One relationship).

and then something like:

foreach($structurePermission as $permission) {
   // here you call your set/get/is for an Object within the Collection
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading