0

I am trying to display a "CategorieActivite" in an activite.html twig page.

I have 2 entities. The first one is Activite. The second one is CategorieActivite.

I have put annotations in the Activite. I have prepared my query with join in ActiviteRepository. It's ok in my database (foreign key).

BUT i get this error : Method "CategorieActivite" for object "AssoFranceRussie\MainBundle\Entity\Activite" does not exist in (in my twig page)

Do I have to do something else to get some entity data from another entity? Thanks EB

The code :

class Activite
{

    // $categorieActiviteId lié à l'entité CategorieActivite
    // ManyToOne
    /**
     * @ORM\ManyToOne(targetEntity="AssoFranceRussie\MainBundle\Entity\CategorieActivite")
     * @ORM\JoinColumn(name="categorie_activite_id", referencedColumnName="id")
     */
    private $categorieActiviteId;   

...
}

ActiviteRepository :

public function getAllActivites()
    {
    $query = $this->getEntityManager()->createQuery(
      'SELECT a,c,n 
        FROM AssoFranceRussieMainBundle:Activite a 
        JOIN a.categorieActiviteId c 
        JOIN a.niveauActiviteId n 
        ORDER BY a.nom ASC '
        );   


    return $query->getResult();

    }

And in the twig html:

<p><strong>{{activite.CategorieActivite.libelle}}</strong></p>

2 Answers 2

1

You should create getter for $categorieActiviteId property. So in Activite class you should have

public function getCategorieActivite() {
    return $this->categorieActiviteId;
}

and in twig you should have:

<p><strong>{{activite.getCategorieActivite.libelle}}</strong></p>

Dont forget libelle have to be public method or property

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you for your help. It works now. Actually i had already the getter with annotation in the class Activite. So, I should have write in twig {{activite.categorieActiviteId.libelle}} instead of {{activite.categorieActivite.libelle}}. The 2 ways to access the data work.

the getter in Activite class:

/**
 * Get categorieActiviteId
 *
 * @return \AssoFranceRussie\MainBundle\Entity\CategorieActivite 
 */
public function getCategorieActiviteId()
{
    return $this->categorieActiviteId;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.