Doctrine 结果中的数组键重复
对于我正在构建的网站,我需要检查某个位置是否有合同,这些位置可以有直接与其链接的合同,或者较大组织的一部分也有与该组织链接的合同。
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在查询中重命名它。因此,在您的示例中,这只会将
sc_id
更改为类似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 likeSee http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en#select-queries:aggregate-values