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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line