Doctrine 结果中的数组键重复

发布于 2024-09-13 10:50:54 字数 1187 浏览 4 评论 0原文

对于我正在构建的网站,我需要检查某个位置是否有合同,这些位置可以有直接与其链接的合同,或者较大组织的一部分也有与该组织链接的合同。

我正在尝试使用 DQL 查询首先检查是否有直接链接的合同,然后是否有通过组织与此查询链接的合同:

    $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();

它执行了我想要的操作,因为它返回一个包含我的所有数据的数组。 可以

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) { } 

问题

是重复的 ["Sc"] 键,因为在 php 中,当数组具有重复键时,最后一个键会覆盖第一个键(无论如何我读到的内容),这使得无法进行如下检查:

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

我 似乎不知道如何使学说以不同的方式命名其中一个键,甚至可能将两者合并。

我尝试使用 INDEXBY ,但这改变了 ['SC'] 数组内的键,而不是 ['SC'] 本身。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-09-20 10:50:54

您可以在查询中重命名它。因此,在您的示例中,这只会将 sc_id 更改为类似

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

See http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-学说查询语言/en#select-queries:聚合值

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文