Friday, 15 April 2011

php - Getter on ManyToMany returns empty array -


I have a symfony2 / doctrine project with two entities:

Project and User

Projects can be different bosses, and users can own different projects.

That is why I am trying to establish a lot of restraint among those institutions.

We go here:

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 = "beneficial" columns = {@ ORM \ JoinColumn (name = "Projet_id" refers Colmnam = "id")} * Inwarjhonj column = {@ ORM \ JoinColumn (name = "user_id", referenced column name = "id")}} *) / personal $ beneficial; Public Function __ Composition () {$ Beneficial = New ArrayCollection (); } Public Function addBeneficiaire (Utilisateurs $ u) {$ this- & gt; Beneficial [] = $ u; $ This return; } Public function removal execution (utilities $ U) {$ this- & gt; Beneficial- & gt; Delete element ($ u); } Public event getBeneficiaires () {Return $ this-> Beneficial; }  

User:

  / ** * Utilisateurs * * @ORM \ Table () * @ORM \ unit (repositoryClass = "Acme \ bundle \ Userbundle \ Entity \ UtilisateursRepository ") * / class Utilisateurs BaseUser {// about Projet} does not offer any property  

when I add users to a project:

  $ projet-> AddBeneficiaire ($ this-> Mill ('security.token_storage') - & gt; getToken () - & gt; getUser ());  

This fills the right table:

  Choose from * beneficial *; + ----------- + --------- + | Projet_id | User_id | + ----------- + --------- + | 8 | 3 | + ----------- + --------- + 1 line set (0.00 seconds)  

But when I'm trying to access do it:

  / ** * @ path ( "/ projet / {idprojet}") * @ template () * / public function project action ($ idprojet) {$ projet = $ this -> gt; GetDoctrine () - & gt; GetRepository ('Acme: Projet') - & gt; FindOneById ($ idprojet); If (! $ Projet) {New Response Back ("Projet Intawvable!"); } // requests that $ projet- & gt; GetBeneficiaires (); Return $ $- & gt; Render ('acme default: index.html.twig', array ("projet" => $ projet)); }  

The array "Profit Proceeds" is empty in the project:

  projected {# 515 ▼ -id: 8 -nom: "blou2" - Description: " blocks "-binefiaers: Persistent Colekshn {# 516 ▼ -sapapः [-updec]: Projet {# 515} -sabeshn: array: 1 9 [1 ... 9] M: Antaitimanger {# 120 ... 10} - Bakriffildnam faucet-type class: Clasmatetata {# 86 ...} -sinshi: false-start: wrong -col: Arreclekshn {# 517 ▼ -_elements: []}}}  

Any ideas which are wrong here?

Thanks a lot!

Edit

  Count ($ projet-> getBeneficiaires ());   

1

Look at the error in your code: Public Function __Composition () {$ Beneficial = New Array Compilation (); }

You should use:

  public function __construct () {$ this- & gt; Beneficiaires = new ArrayCollection (); }  

But a better way to make stuff is simply: Use properties, and commands: Principle: generated: unit and it automatically Sets and gesters will add


No comments:

Post a Comment