简体   繁体   中英

Doctrine OneToMany / ManyToOne referenced object always null

I am building a Symfony 2 application and therefore using doctrine for ORM. So fare everything went well except one reference which will allways be null and I can't figure out why. So here is what I have:

Group Entity:

class Group {

    ...

    /**
    * 
    * @ORM\ManyToOne(targetEntity="Test")
    * @ORM\JoinColumn(name="test", referencedColumnName="id")
    *
    */

    private $test;

    /**
    * 
    * @ORM\OneToMany(targetEntity="Participant", mappedBy="group")
    * 
    */

    private $participants;

}

Participant Entity:

class Participant {

    ...

    /**
     * 
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="participants")
     * @ORM\JoinColumn(name="`group`", referencedColumnName="id", onDelete="CASCADE")
     * 
    */

    private $group;
}

So I have the following relationships:

Test (1) <-> (N) Group (1) <-> (N) Participant

Problem:

  1. When calling getGroup() on an object of the participant entity I get as return null.
  2. When calling getTest() on an object of the group entity I get the proper test object.
  3. When calling getParticipants on the group object I get the collection which all the participants.

So why is 1. not working and 2. is even though both are the same kind of relationships?

Even more strange is this while debuging:

Debug::dump($participant); 

Leads to:

object(stdClass)[406] public '__CLASS__' => string 'GroupBundle\Entity\Participant' (length=52) 
public 'id' => int 1 
public 'firstname' => string 'xxx' (length=4) 
public 'lastname' => string 'xxx' (length=7) 
public 'email' => string 'xxx' (length=19) 
public 'auth_token' => string 'xxx' (length=43) 
public 'group' => null

BUT

Debug::dump($group);
Debug::dump($participant);

Leads to:

object(stdClass)[406] public '__CLASS__' => string 'GroupBundle\Entity\Participant' (length=52) 
public 'id' => int 1 
public 'firstname' => string 'xxx' (length=4) 
public 'lastname' => string 'xxx' (length=7) 
public 'email' => string 'xxx' (length=19) 
public 'auth_token' => string 'xxx' (length=43) 
public 'group' => object(stdClass)[412] public '__CLASS__' => string 'GroupBundle\Entity\Group' (length=46) 
public 'id' => int 1 
public 'name' => string 'xxx' (length=4) 
public 'description' => string 'xxx' (length=4) 
public 'status' => int 0 
public 'test' => string 'Proxies\__CG__\TestBundle\Entity\Test' (length=59) 
public 'participants' => string 'Array(5)' (length=8)

So all of a sudden it is there. Thanks for your thoughts. Hope you can help me.

Using backtick quoting as in:

@ORM\JoinColumn(name="`group`", referencedColumnName="id", onDelete="CASCADE")
                      ^     ^

Is not supported for join columns. I assume the DBMS you are using has GROUP as a reserved word, which is why you used backticks. Try changing the join column as follows:

@ORM\JoinColumn(name="group_id", referencedColumnName="id", onDelete="CASCADE")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM