Symfony Getter on Class on Relation get unrelated entity

22 views Asked by At

So in Symfony 6, i'm looking for a way of creating a getter in Entity that has a ManyToOne relation, basically this relation has a field that may contain a entity id or not (can be null), if this field is null, I want the getter to append the nulled values into the getter (if this is easier), or create a new getter that contains this entities with the relation as null values.

I'm not sure how to do this as it doesn't seem right to call entity manager inside a entity.

This is some sort of the code I want, but actually using some good practice:

    /**
     * @return Collection<int, DocumentCategory>
     */
    public function getPublicDocumentCategories(): Collection
    {
        $publicDocumentService = $this->getRepository(DocumentCategory::class)->find(["country" => null]);

        return $publicDocumentService;
    }

    /**
     * @return Collection<int, DocumentCategory>
     */
    public function getDocumentCategories(): Collection
    {
        return $this->documentCategories;
    }

I've looked already and it's bad practice this, but yeah that's the result I need in order to not repeat code (which also seems a bad practice) and return in all the controllers at twig render this entities.

0

There are 0 answers