我正在构建一个Symfony 2应用程序,因此使用ORM的学说。所以一切顺利,除了一个将永远为空的参考,我无法弄清楚为什么。所以这就是我所拥有的:
群组实体:
class Group {
...
/**
*
* @ORM\ManyToOne(targetEntity="Test")
* @ORM\JoinColumn(name="test", referencedColumnName="id")
*
*/
private $test;
/**
*
* @ORM\OneToMany(targetEntity="Participant", mappedBy="group")
*
*/
private $participants;
}
参与实体:
class Participant {
...
/**
*
* @ORM\ManyToOne(targetEntity="Group", inversedBy="participants")
* @ORM\JoinColumn(name="`group`", referencedColumnName="id", onDelete="CASCADE")
*
*/
private $group;
}
所以我有以下关系:
测试(1)< - > (N)组(1) - < - > (N)参与者
问题:
那么为什么1.不工作,2。即使两者都是同一种关系?
在调试时更加奇怪:
Debug::dump($participant);
导致:
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);
导致:
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)
所以突然间它就在那里。谢谢你的想法。希望你能帮帮我。
答案 0 :(得分:2)
使用反引号引用,如:
@ORM\JoinColumn(name="`group`", referencedColumnName="id", onDelete="CASCADE")
^ ^
不支持连接列。我假设你使用的DBMS有GROUP
作为保留字,这就是你使用反引号的原因。尝试按如下方式更改连接列:
@ORM\JoinColumn(name="group_id", referencedColumnName="id", onDelete="CASCADE")