dongxuan1660 2010-08-11 10:19 采纳率: 100%
浏览 16
已采纳

Doctrine结果中的重复数组键

For a website i'm building I need to check wether a location has contracts, these locations can have contracts linked directly to them or when there part of a larger organisation also have contracts that are linked to the organisation.

I am trying to use a DQL query to first check if there are contracts linked directly and then if there are any linked via an organistion with this query:

    $q = self::createQuery("l")
        ->select('sl.sc_id, sl.type, so.sc_id, so.type, l.naam, l.loc_id, l.straat, l.telefoon, l.plaats, l.postcode, l.huisnummer, l.huisnummer_achtervoegsel, o.naam')
        ->from('Locatie l, l.Organisatie o, l.Sc sl, o.Sc so')
        ->orderBy('o.naam, l.naam')
        ->execute();

It does what I want in so much as that it returns an array with all the data I need:

array(13) { 
["loc_id"]=> string(2) "93" ["org_id"]=> string(1) "9" ["naam"]=> string(12) "test" 
["Organisatie"]=> array(4) { ["org_id"]=> string(1) "9" ["naam"]=> string(3) "test"
["Sc"]=> array(1) { [0]=> array(6) { ["sc_id"]=> string(1) "1" ["sc_nummer"]=> NULL ["type"]=> string(6) "All-in" } } } 
["Sc"]=> array(0) { } 

}

The problem is the duplicate ["Sc"] key, because in php when an array has duplicate keys, that last key overwrites the first (what i read anyway), this makes it impossible to make a check like:

if(!empty($SC) or !empty($SC2) {}

I can't seem to figure out how to make doctrine name one of the keys differently or maybe even merge the two.

I tried using INDEXBY but that changed the key inside the ['SC'] array and not ['SC'] itself.

Any ideas?

  • 写回答

1条回答 默认 最新

  • douyihuaimao733955 2010-08-13 10:46
    关注

    You can rename it within the query. So in your example, that would only change on of the sc_id to something like

    [...] sl.sc_id AS your_new_key [...]
    

    See http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en#select-queries:aggregate-values

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?