Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
queryhelper.php
1<?php
10
14
22{
34 public static function decompose(Query $query, $fairLimit = true, $separateRelations = true)
35 {
36 $entity = $query->getEntity();
37 $primaryNames = $entity->getPrimaryArray();
38 $originalSelect = $query->getSelect();
39
40 if ($fairLimit)
41 {
42 // select distinct primary
43 $query->setSelect($entity->getPrimaryArray());
44 $query->setDistinct();
45
46 $rows = $query->fetchAll();
47
48 // return empty result
49 if (empty($rows))
50 {
51 return $query->getEntity()->createCollection();
52 }
53
54 // reset query
55 $query = $entity->getDataClass()::query();
56 $query->setSelect($originalSelect);
57 $query->where(static::getPrimaryFilter($primaryNames, $rows));
58 }
59
60 // more than one OneToMany or ManyToMany
61 if ($separateRelations)
62 {
63 $commonSelect = [];
64 $dividedSelect = [];
65
66 foreach ($originalSelect as $selectItem)
67 {
68 // init query with select item
69 $selQuery = $entity->getDataClass()::query();
70 $selQuery->addSelect($selectItem);
71 $selQuery->getQuery(true);
72
73 // check for relations
74 foreach ($selQuery->getChains() as $chain)
75 {
76 if ($chain->hasBackReference())
77 {
78 $dividedSelect[] = $selectItem;
79 continue 2;
80 }
81 }
82
83 $commonSelect[] = $selectItem;
84 }
85
86 if (empty($commonSelect))
87 {
88 $commonSelect = $query->getEntity()->getPrimaryArray();
89 }
90
91 // common query
92 $query->setSelect($commonSelect);
93 }
94
96 $collection = $query->fetchCollection();
97
98 if (!empty($dividedSelect) && $collection->count())
99 {
100 // custom identity map & collect primaries
101 $im = new IdentityMap;
102 $primaryValues = [];
103
104 foreach ($collection as $object)
105 {
106 $im->put($object);
107
108 $primaryValues[] = $object->primary;
109 }
110
111 $primaryFilter = static::getPrimaryFilter($primaryNames, $primaryValues);
112
113 // select relations
114 foreach ($dividedSelect as $selectItem)
115 {
116 $result = $entity->getDataClass()::query()
117 ->addSelect($selectItem)
118 ->where($primaryFilter)
119 ->exec();
120
121 $result->setIdentityMap($im);
122 $result->fetchCollection();
123 }
124 }
125
126 return $collection;
127 }
128
135 public static function getPrimaryFilter($primaryNames, $primaryValues)
136 {
137 $commonSubFilter = new ConditionTree();
138
139 if (count($primaryNames) === 1)
140 {
141 $values = [];
142
143 foreach ($primaryValues as $row)
144 {
145 $values[] = $row[$primaryNames[0]];
146 }
147
148 $commonSubFilter->whereIn($primaryNames[0], $values);
149 }
150 else
151 {
152 $commonSubFilter->logic('or');
153
154 foreach ($primaryValues as $row)
155 {
156 $primarySubFilter = new ConditionTree();
157
158 foreach ($primaryNames as $primaryName)
159 {
160 $primarySubFilter->where($primaryName, $row[$primaryName]);
161 }
162 }
163 }
164
165 return $commonSubFilter;
166 }
167}
static getPrimaryFilter($primaryNames, $primaryValues)
setDistinct($distinct=true)
Definition query.php:3288
setSelect(array $select)
Definition query.php:338
fetchAll(\Bitrix\Main\Text\Converter $converter=null)
Definition query.php:995