[英]Symfony 2 & doctrine2 - how to use column name as array key when using join in doctrine query builder?
I am performing a join using doctrine2
query builder and symfony3
. 我正在使用doctrine2
查询构建器和symfony3
执行symfony3
。
My controller contains following code: 我的控制器包含以下代码:
$rp_konta = $this->getDoctrine()->getRepository('AppBundle:konto');
$query = $rp_konta->createQueryBuilder('p')
->select('p')
->addSelect('SUM(g.kwota)')
->leftJoin('p.ksiegowaniaWinien', 'g')
->getQuery();
which results in the SQL query: 这将导致SQL查询:
SELECT k0_.id AS id_0, k0_.kod_konta AS kod_konta_1, k0_.nazwa_konta AS nazwa_konta_2, k0_.typ_konta AS typ_konta_3, k0_.aktywne AS aktywne_4, SUM(d1_.kwota) AS sclr_5 FROM konto k0_ LEFT JOIN dziennik d1_ ON k0_.id = d1_.konto_winien_id AND (d1_.usuniety IS NULL) WHERE k0_.id IN ('1', '2', '3');
Please note that each column name is followed by a suffix id _0 , kod_konta _1 and so on. 请注意,每个列名后均带有后缀id _0 ,kod_konta _1等。
As my twig template looks like the following: 由于我的树枝模板如下所示:
{% for konto in konta_pagination %}
<tr>
<td>{{ konto.id }}</td>
{% endfor %}
and the id column returned as a result of SQL query is marked by doctrine as id_0
and not as id
I get the following error: 并且由于SQL查询而返回的id列被该学说标记为id_0
而不是id
我得到以下错误:
Key "id" for array with keys "0, 1" does not exist in AppBundle::/raport/saldaKont.html.twig at line 18 第18行的AppBundle :: / raport / saldaKont.html.twig中不存在带有键“ 0、1”的数组的键“ id”
How should I modify the query so that it returns the array with keys consistent without any suffix? 我应该如何修改查询,以便它返回键一致且没有任何后缀的数组?
Based on @Tiriel request please find below also my entities code. 根据@Tiriel的要求,请在下面也找到我的实体代码。
Dziennik entity : Dziennik实体 :
class dziennik
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/.../
/**
* @ORM\ManyToOne(targetEntity="konto", inversedBy="ksiegowaniaWinien")
* @Gedmo\Versioned
*/
protected $kontoWinien;
/**
* @ORM\ManyToOne(targetEntity="konto", inversedBy="ksiegowaniaMa")
* @Gedmo\Versioned
*/
protected $kontoMa;
}
Konto entity : Konto实体 :
class konto
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/.../
/**
* @ORM\OneToMany(targetEntity="dziennik", mappedBy="kontoWinien")
*/
protected $ksiegowaniaWinien;
/**
* @ORM\OneToMany(targetEntity="dziennik", mappedBy="kontoMa")
*/
protected $ksiegowaniaMa;
}
Try putting your addSelect('SUM(g.kwota)')
AFTER your leftJoin('p.ksiegowaniaWinien', 'g')
(at least that's the way I learned, mybe it'll fix your problem). 尝试在您的leftJoin('p.ksiegowaniaWinien', 'g')
addSelect('SUM(g.kwota)')
之后放置addSelect('SUM(g.kwota)')
(至少这是我学到的方法,除非它可以解决您的问题)。
Otherwise, we need to see your entities. 否则,我们需要查看您的实体。 To me, you must have a column name id in each table, and so Doctrine has to suffix each one in it's query. 对我来说,每个表中都必须有一个列名ID,因此Doctrine必须在查询中为每个表后缀。 It has never made any problem in my code, but maybe here... 它从未在我的代码中造成任何问题,但也许在这里...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.