ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ActiveRecordList.php
Go to the documentation of this file.
1<?php
2
31{
32 protected \arWhereCollection $arWhereCollection;
33 protected \arJoinCollection $arJoinCollection;
34 protected \arOrderCollection $arOrderCollection;
35 protected \arLimitCollection $arLimitCollection;
36 protected \arConcatCollection $arConcatCollection;
37 protected \arSelectCollection $arSelectCollection;
38 protected \arHavingCollection $arHavingCollection;
39 protected bool $loaded = false;
40 protected string $class = '';
44 protected array $result = [];
45 protected array $result_array = [];
46 protected bool $debug = false;
47 protected ?string $date_format = null;
48 protected array $addidtional_parameters = [];
49 protected static ?string $last_query = null;
50 protected ?\arConnector $connector = null;
51 protected ?\ActiveRecord $ar = null;
52 protected bool $raw = false;
53
57 public function __construct(ActiveRecord $activeRecord)
58 {
59 $this->class = $activeRecord::class;
60 $this->setAR($activeRecord);
61 $this->arWhereCollection = arWhereCollection::getInstance($this->getAR());
62 $this->arJoinCollection = arJoinCollection::getInstance($this->getAR());
63 $this->arLimitCollection = arLimitCollection::getInstance($this->getAR());
64 $this->arOrderCollection = arOrderCollection::getInstance($this->getAR());
65 $this->arConcatCollection = arConcatCollection::getInstance($this->getAR());
66 $this->arSelectCollection = arSelectCollection::getInstance($this->getAR());
67 $this->arHavingCollection = arHavingCollection::getInstance($this->getAR());
68
69 $arSelect = new arSelect();
70 $arSelect->setTableName($activeRecord->getConnectorContainerName());
71 $arSelect->setFieldName('*');
72 $this->getArSelectCollection()->add($arSelect);
73 }
74
75 protected function getArConnector(): \arConnector
76 {
77 return arConnectorMap::get($this->getAR());
78 }
79
80 public function additionalParams(array $additional_params): self
81 {
82 $this->setAddidtionalParameters($additional_params);
83
84 return $this;
85 }
86
87
88 //
89 // Statements
90 //
97 public function where($where, $operator = null): self
98 {
99 $this->loaded = false;
100 if (is_string($where)) {
101 $arWhere = new arWhere();
102 $arWhere->setType(arWhere::TYPE_STRING);
103 $arWhere->setStatement($where);
104 $this->getArWhereCollection()->add($arWhere);
105
106 return $this;
107 }
108
109 if (is_array($where)) {
110 foreach ($where as $field_name => $value) {
111 $arWhere = new arWhere();
112 $arWhere->setFieldname($field_name);
113 $arWhere->setValue($value);
114 if ($operator) {
115 if (is_array($operator)) {
116 $arWhere->setOperator($operator[$field_name]);
117 } else {
118 $arWhere->setOperator($operator);
119 }
120 }
121 $this->getArWhereCollection()->add($arWhere);
122 }
123
124 return $this;
125 }
126
127 throw new Exception('Wrong where Statement, use strings or arrays');
128 }
129
134 public function orderBy(string $order_by, string $order_direction = 'ASC'): self
135 {
136 $arOrder = new arOrder();
137 $arOrder->setFieldname($order_by);
138 $arOrder->setDirection($order_direction);
139 $this->getArOrderCollection()->add($arOrder);
140
141 return $this;
142 }
143
149 public function limit(int $start, int $end): self
150 {
151 $arLimit = new arLimit();
152 $arLimit->setStart($start);
153 $arLimit->setEnd($end);
154
155 $this->getArLimitCollection()->add($arLimit);
156
157 return $this;
158 }
159
164 public function innerjoinAR(
165 ActiveRecord $activeRecord,
166 $on_this,
167 string $on_external,
168 array $fields = ['*'],
169 string $operator = '=',
170 bool $both_external = false
171 ): self {
172 return $this->innerjoin(
173 $activeRecord->getConnectorContainerName(),
174 $on_this,
175 $on_external,
176 $fields,
177 $operator,
178 $both_external
179 );
180 }
181
188 protected function join(
189 string $type,
190 string $tablename,
191 $on_this,
192 string $on_external,
193 array $fields = ['*'],
194 string $operator = '=',
195 bool $both_external = false
196 ): self {
197 if (!$both_external && !$this->getAR()->getArFieldList()->isField($on_this)) {
199 }
200 $full_names = false;
201 foreach ($fields as $field_name) {
202 if ($this->getAR()->getArFieldList()->isField($field_name)) {
203 $full_names = true;
204 break;
205 }
206 }
207
208 $arJoin = new arJoin();
209 $arJoin->setType($type);
210 $arJoin->setFullNames($full_names);
211 $arJoin->setTableName($tablename);
212 $arJoin->setOnFirstField($on_this);
213 $arJoin->setOnSecondField($on_external);
214 $arJoin->setOperator($operator);
215 $arJoin->setFields($fields);
216 $arJoin->setBothExternal($both_external);
217 $this->getArJoinCollection()->add($arJoin);
218
219 foreach ($fields as $field) {
220 $arSelect = new arSelect();
221 $arSelect->setTableName($arJoin->getTableNameAs());
222 $arSelect->setFieldName($field);
223 $arSelect->setAs($arJoin->getTableNameAs() . '_' . $field);
224 $this->getArSelectCollection()->add($arSelect);
225 }
226
227 return $this;
228 }
229
235 public function leftjoin(
236 string $tablename,
237 $on_this,
238 string $on_external,
239 array $fields = ['*'],
240 string $operator = '=',
241 bool $both_external = false
242 ): self {
243 return $this->join(arJoin::TYPE_LEFT, $tablename, $on_this, $on_external, $fields, $operator, $both_external);
244 }
245
251 public function innerjoin(
252 string $tablename,
253 $on_this,
254 string $on_external,
255 array $fields = ['*'],
256 string $operator = '=',
257 bool $both_external = false
258 ): self {
259 return $this->join(arJoin::TYPE_INNER, $tablename, $on_this, $on_external, $fields, $operator, $both_external);
260 }
261
265 public function concat(array $fields, string $as): self
266 {
267 $arConcat = new arConcat();
268 $arConcat->setAs($as);
269 $arConcat->setFields($fields);
270 $this->getArConcatCollection()->add($arConcat);
271
272 return $this;
273 }
274
276 {
277 return $this->arWhereCollection;
278 }
279
281 {
282 return $this->arJoinCollection;
283 }
284
286 {
287 return $this->arOrderCollection;
288 }
289
291 {
292 return $this->arLimitCollection;
293 }
294
296 {
297 return $this->arConcatCollection;
298 }
299
301 {
302 return $this->arSelectCollection;
303 }
304
306 {
307 return $this->arHavingCollection;
308 }
309
310 public function setArHavingCollection(\arHavingCollection $arHavingCollection): void
311 {
312 $this->arHavingCollection = $arHavingCollection;
313 }
314
315 public function dateFormat(string $date_format = 'd.m.Y - H:i:s'): self
316 {
317 $this->loaded = false;
318 $this->setDateFormat($date_format);
319
320 return $this;
321 }
322
323 public function debug(): self
324 {
325 $this->loaded = false;
326 $this->debug = true;
327
328 return $this;
329 }
330
331 public function connector(arConnector $arConnector): self
332 {
333 $this->connector = $arConnector;
334
335 return $this;
336 }
337
338 public function raw(bool $set_raw = true): self
339 {
340 $this->setRaw($set_raw);
341
342 return $this;
343 }
344
345 public function hasSets(): bool
346 {
347 return $this->affectedRows() > 0;
348 }
349
350 public function affectedRows(): int
351 {
352 return $this->getArConnector()->affectedRows($this);
353 }
354
355 public function count(): int
356 {
357 return $this->affectedRows();
358 }
359
360 public function getCollection(): self
361 {
362 return $this;
363 }
364
365 public function setClass(string $class): void
366 {
367 $this->class = $class;
368 }
369
370 public function getClass(): string
371 {
372 return $this->class;
373 }
374
378 public function get(): array
379 {
380 $this->load();
381
382 return $this->result;
383 }
384
389 {
390 $this->loadLastQuery();
391
392 $result = array_values($this->result);
393
394 return array_shift($result);
395 }
396
397 public function first(): ?\ActiveRecord
398 {
399 $this->load();
400
401 $result = array_values($this->result);
402
403 return array_shift($result);
404 }
405
406 public function last(): ?\ActiveRecord
407 {
408 $this->load();
409
410 $result = array_values($this->result);
411
412 return array_pop($result);
413 }
414
420 public function getArray(?string $key = null, string|array|null $values = null): array
421 {
422 $this->load();
423
424 return $this->buildArray($key, $values);
425 }
426
432 protected function buildArray(?string $key, $values): array
433 {
434 if ($key === null && $values === null) {
435 return $this->result_array;
436 }
437 $array = [];
438 foreach ($this->result_array as $row) {
439 if ($key) {
440 if (!array_key_exists($key, $row)) {
441 throw new Exception("The attribute $key does not exist on this model.");
442 }
443 $array[$row[$key]] = $this->buildRow($row, $values);
444 } else {
445 $array[] = $this->buildRow($row, $values);
446 }
447 }
448
449 return $array;
450 }
451
456 protected function buildRow(?array $row, $values)
457 {
458 if ($values === null) {
459 return $row;
460 }
461
462 if (!is_array($values)) {
463 return $row[$values];
464 }
465
466 $array = [];
467 foreach ($row as $key => $value) {
468 if (in_array($key, $values)) {
469 $array[$key] = $value;
470 }
471 }
472
473 return $array;
474 }
475
476 protected function load(): void
477 {
478 if ($this->loaded) {
479 return;
480 }
481
482 $records = $this->getArConnector()->readSet($this);
486 $primaryFieldName = $this->getAR()->getArFieldList()->getPrimaryFieldName();
488 $class_name = $this->getAR()::class;
489 foreach ($records as $record) {
490 $primary_field_value = $record[$primaryFieldName];
491 if (!$this->getRaw()) {
492 $obj = new $class_name(0, $this->getArConnector(), $this->getAddidtionalParameters());
493 $this->result[$primary_field_value] = $obj->buildFromArray($record);
494 }
495 $res_awake = [];
496 if (!$this->getRaw()) {
497 foreach ($record as $key => $value) {
498 $arField = $obj->getArFieldList()->getFieldByName($key);
499 if ($arField !== null && ($arField->isDateField() && $this->getDateFormat())) {
500 $res_awake[$key . '_unformatted'] = $value;
501 $res_awake[$key . '_unix'] = strtotime((string) $value);
502 $value = date($this->getDateFormat(), strtotime((string) $value));
503 }
504 $waked = $this->getAR()->wakeUp($key, $value);
505 $res_awake[$key] = $waked ?? $value;
506 }
507 $this->result_array[$res_awake[$primaryFieldName]] = $res_awake;
508 } else {
509 $this->result_array[$primary_field_value] = $record;
510 }
511 }
512 $this->loaded = true;
513 }
514
518 protected function loadLastQuery(): void
519 {
520 // $this->readFromDb(self::$last_query);
521 }
522
523 public function setAR(\ActiveRecord $activeRecord): void
524 {
525 $this->ar = $activeRecord;
526 }
527
528 public function getAR(): ?\ActiveRecord
529 {
530 return $this->ar;
531 }
532
533 public function getDebug(): bool
534 {
535 return $this->debug;
536 }
537
538 public function setDateFormat(string $date_format): void
539 {
540 $this->date_format = $date_format;
541 }
542
543 public function getDateFormat(): string
544 {
545 return $this->date_format ?? '';
546 }
547
548 public static function setLastQuery(string $last_query): void
549 {
550 self::$last_query = $last_query;
551 }
552
553 public static function getLastQuery(): ?string
554 {
555 return self::$last_query;
556 }
557
561 public function setAddidtionalParameters(array $addidtional_parameters): void
562 {
563 $this->addidtional_parameters = $addidtional_parameters;
564 }
565
569 public function getAddidtionalParameters(): array
570 {
571 return $this->addidtional_parameters;
572 }
573
574 public function setRaw(bool $raw): void
575 {
576 $this->raw = $raw;
577 }
578
579 public function getRaw(): bool
580 {
581 return $this->raw;
582 }
583}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
raw(bool $set_raw=true)
arLimitCollection $arLimitCollection
connector(arConnector $arConnector)
setAR(\ActiveRecord $activeRecord)
innerjoin(string $tablename, $on_this, string $on_external, array $fields=[' *'], string $operator='=', bool $both_external=false)
buildArray(?string $key, $values)
limit(int $start, int $end)
arWhereCollection $arWhereCollection
innerjoinAR(ActiveRecord $activeRecord, $on_this, string $on_external, array $fields=[' *'], string $operator='=', bool $both_external=false)
where($where, $operator=null)
setDateFormat(string $date_format)
leftjoin(string $tablename, $on_this, string $on_external, array $fields=[' *'], string $operator='=', bool $both_external=false)
setArHavingCollection(\arHavingCollection $arHavingCollection)
arHavingCollection $arHavingCollection
getArray(?string $key=null, string|array|null $values=null)
__construct(ActiveRecord $activeRecord)
@noinspection PhpFieldAssignmentTypeMismatchInspection
additionalParams(array $additional_params)
setAddidtionalParameters(array $addidtional_parameters)
arSelectCollection $arSelectCollection
dateFormat(string $date_format='d.m.Y - H:i:s')
arOrderCollection $arOrderCollection
static setLastQuery(string $last_query)
join(string $type, string $tablename, $on_this, string $on_external, array $fields=[' *'], string $operator='=', bool $both_external=false)
concat(array $fields, string $as)
orderBy(string $order_by, string $order_direction='ASC')
arConcatCollection $arConcatCollection
arJoinCollection $arJoinCollection
buildRow(?array $row, $values)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getConnectorContainerName()
@description Return the Name of your Connector Table
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static get(ActiveRecord $activeRecord)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const LIST_JOIN_ON_WRONG_FIELD
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const TYPE_STRING
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists('../ilias.ini.php'))