【问题标题】:Doctrine - Is it possible to associate a column result with an other column result instead of her column nameDoctrine - 是否可以将列结果与其他列结果而不是她的列名相关联
【发布时间】:2021-12-18 07:10:33
【问题描述】:

抱歉这个不清楚的问题。
如果没有示例代码,我不知道如何用文字解释我想要什么,所以我第一次在网上找不到解决方案,我就在这里。

我正在使用 Doctrine 进行 Symfony 3.4 项目。
我使用 Doctrine 查询构建器进行以下查询:

$this->em->createQueryBuilder()
    ->select('p.email', 'pa.value','a.key')
    ->from('myEntity', 'p')
    ->join('myOtherEntity', 'pa', Join::WITH, 'pa.myEntity = p.id', )
    ->join('anOtherOneEntity', 'a', Join::WITH, 'a.id = pa.anOtherOneEntity')

它给了我以下结果:

Array
(
    [email] => test@test.com
    [value] => 758433
    [key] => postalcode
)
Array
(
    [email] => test@test.com
    [value] => John
    [key] => firstname
)
Array
(
    [email] => test@test.com
    [value] => Doe
    [key] => lastname
)

我想->groupBy('email') 得到这个结果:

Array
(
    [email] => test@test.com
    [postalcode] => 758433
    [firstname] => John
    [lastname] => Doe
)

我已尝试更新 myEntity 以获取关联值:

private $myAttribute

public function getMyAttribute()

还有我的查询

$this->em->createQueryBuilder()
     ->select('p.email', 'p.myAttribute')
     ->from('myEntity', 'p')

但我收到以下错误:

Error: Class AppBundle\Entity\myEntity has no field or association named myAttrib
  ute

我认为解决方案是更改我的第一个查询,但我需要一些帮助。

感谢您的宝贵时间。

【问题讨论】:

    标签: php sql symfony doctrine symfony-3.4


    【解决方案1】:

    这在 PHP 中很简单,只需从结果中构建所需的数组。

    $result = [
        'email' => 'test@test.com',
        'value' => 758433,
        'key' => 'postalcode',
    ];
    $return = [
        'email' => $result['email'],
        $result['key'] => $result['value'],
    ];
    var_export($return);
    

    上面会输出以下内容:

    array (
      'email' => 'test@test.com',
      'postalcode' => 758433,
    )
    

    【讨论】:

    • 感谢您的回答让我明白我忘记了一个细节,所以我编辑了我的问题。我有多个结果要通过电子邮件分组。
    • 而且我认为可以直接从查询中得到我期望的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2012-08-31
    • 2016-03-10
    • 1970-01-01
    相关资源
    最近更新 更多