我的基本模型中有这个结构:
class Value extends ...
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
...
}
此模型无法从我的网站扩展,但我需要添加一个自引用的ManyToMany关系来读取相关数据。
所以我创造了这个新模型:
class ValueGrid extends ...
{
/**
* @var integer $id
*
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer $parentValueId
*
* @ORM\Column(name="parentValueID", type="integer", nullable=false)
*/
private $parentValueId;
/**
* @var integer $childValueId
*
* @ORM\Column(name="childValueID", type="integer", nullable=false)
*/
private $childValueId;
...
}
当我从" Value"中读取数据时,我这样做:
$builder = $this->getEntityManager()->createQueryBuilder();
$builder->select(array('value'))
->from('Value', 'value');
// Next i need to simulate the relation
$builder->addSelect(['valuegrid']);
$builder->leftJoin('ValueGrid', 'valuegrid', 'WITH', 'value.id = valuegrid.parentValueId');
它给出了这个输出:
{"id":1,"value":"a"},
null,
{"id":2,"value":"b"},
null,
{"id":3,"value":"c"},
{"id":1,"parentValueId":1,"childValueId":2},
{"id":4,"value":"d"},
null,
{"id":5,"value":"e"},
null
但它应该是这种格式:
{"id":1,"value":"a", "valuegrid":null},
{"id":2,"value":"b", "valuegrid":null},
{"id":3,"value":"c", "valuegrid":{"id":1,"parentValueId":1,"childValueId":2}},
{"id":4,"value":"d", "valuegrid":null},
{"id":5,"value":"e", "valuegrid":null},
有人有什么建议吗?
我真的不知道为什么我的做法不起作用..
Thansk和Greetings!