我想使用以下mysql查询:select * from x join y on x.y_id = y.id where x.a= 0
我尝试使用以下教义语法将它放在我的Symfony项目中。
$repositoryX->createQueryBuilder('x')
->add('select', 'x')
->add('from', 'AppBundle:X x')
->leftJoin('AppBundle:Y', 'y', 'WITH', 'x.y_id = y.id')
->where('x.a = :a')
->setParameter('a', 0)
->getQuery()
->getResult();
我的问题是我只看到表x的结果,但我也想要表y的结果(因为连接)。当我进行额外选择时,我得到Neither the property "y_id" nor one of the methods "y_id()", "gety_id()"/"isy_id()" or "__call()" exist and have public access in class
' *'在学说中不起作用。
答案 0 :(得分:1)
您只选择x
更改:
->add('select', 'x')
到
->add('select', 'x,y')
//The second param might accept and array i dont remember off hand
->add('select', ['x','y'])
或写得更好:
$repository->createQueryBuilder()
->select(['x','y'])
->from('AppBundle:X','x')
->join('x.y','y') // This assumes you have the mapping defined for this property
->where('x.a = :a')
->setParameter('a', 0)
->getQuery()
->getResult();
如果使用注释映射,它应该看起来像
//X entity
//...
class X {
//...
/**
* @ORM\OneToOne(targetEntity="Y")
* @ORM\JoinColumn(name="y_id", referencedColumnName="id")
*/
protected $y;
}