A. Grima
A. Grima

Reputation: 57

Getter on ManyToMany returns empty array

I have a symfony2/Doctrine project with two Entities :

Project and User

Projects can have severals owners, and Users can be owners of severals Projects.

So I am trying to setup a manyToMany association between those entities.

Here we go :

Project :

    /**
     * Projet
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="Acme\Bundle\ProjetBundle\Entity\ProjetRepository")
     */
    class Projet
    {
        //some properties 
        /**
        * @ORM\ManyToMany(targetEntity="Acme\Bundle\UserBundle\Entity\Utilisateurs"), cascade={"all"}, fetch="EAGER")
        * @ORM\JoinTable(name="Beneficiaire", joinColumns={@ORM\JoinColumn(name="projet_id", referencedColumnName="id")},
         *      inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
         *      )
        **/
        private $beneficiaires;



        public function __construct(){
            $beneficiaires =  new ArrayCollection();
        }
        public function addBeneficiaire(Utilisateurs $u){
            $this->beneficiaires[] = $u;

             return $this;
        }

        public function removeBeneficiaire(Utilisateurs $u){
            $this->beneficiaires->removeElement($u);
        }

        public function getBeneficiaires(){
           return $this->beneficiaires;
        }

Users :

/**
 * Utilisateurs
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\Bundle\UserBundle\Entity\UtilisateursRepository")
 */
class Utilisateurs extends BaseUser
{

    //no property about Projet
}

When I'm adding a user to a project :

$projet->addBeneficiaire($this->get('security.token_storage')->getToken()->getUser());

It fills the right table :

 select * from Beneficiaire;
+-----------+---------+
| projet_id | user_id |
+-----------+---------+
|         8 |       3 |
+-----------+---------+
1 row in set (0.00 sec)

But when i'm trying to access it :

/**
     * @Route("/projet/{idprojet}")
     * @Template()
     */
    public function projetAction($idprojet)
    {
        $projet = $this->getDoctrine()->getRepository('Acme:Projet')->findOneById($idprojet);
        if (!$projet) {

            return new Response("Projet Introuvable !");
        }
        //Triggers the request
        $projet->getBeneficiaires();

        return $this->render('Acme:Default:index.html.twig', array("projet" => $projet));
    }

The array "beneficiaires" in project is empty :

Projet {#515 ▼
  -id: 8
  -nom: "blou2"
  -description: "blou"
  -beneficiaires: PersistentCollection {#516 ▼
    -snapshot: []
    -owner: Projet {#515}
    -association: array:19 [ …19]
    -em: EntityManager {#120 …10}
    -backRefFieldName: null
    -typeClass: ClassMetadata {#86 …}
    -isDirty: false
    -initialized: false
    -coll: ArrayCollection {#517 ▼
      -_elements: []
    }
  }
}

Any Idea what's wrong in here?

Thanks a lot !

EDIT

count($projet->getBeneficiaires());

returns 1

Upvotes: 0

Views: 958

Answers (1)

Alexandru Olaru
Alexandru Olaru

Reputation: 7102

I saw a error in your code:

    public function __construct(){
        $beneficiaires = new ArrayCollection();
    }

you should use:

    public function __construct(){
        $this->beneficiaires = new ArrayCollection();
    }

But a better way to do stuff is just to create: the attributes, and use the command : doctrine:generate:entity and it will automatically add all setters and getters.

Upvotes: 1

Related Questions