-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathBaseModelCriteria.php
More file actions
395 lines (348 loc) · 10.8 KB
/
BaseModelCriteria.php
File metadata and controls
395 lines (348 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/**
* MIT License. This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Propel\Runtime\ActiveQuery;
use ArrayIterator;
use IteratorAggregate;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\InvalidArgumentException;
use Propel\Runtime\Exception\LogicException;
use Propel\Runtime\Formatter\AbstractFormatter;
use Propel\Runtime\Map\TableMap;
use Propel\Runtime\Propel;
use Traversable;
/**
* @phpstan-template T of \Propel\Runtime\ActiveRecord\ActiveRecordInterface
* @phpstan-template TColl of \Propel\Runtime\Collection\Collection
* @phpstan-template TReturn
*/
abstract class BaseModelCriteria extends Criteria implements IteratorAggregate
{
/**
* @phpstan-var class-string<T>|null
*
* @var string|null
*/
protected $modelName;
/**
* @phpstan-var class-string<\Propel\Runtime\Map\TableMap>|null
*
* @var string|null
*/
protected $modelTableMapName;
/**
* @var bool
*/
protected $useAliasInSQL = false;
/**
* @var string|null
*/
protected $modelAlias;
/**
* @phpstan-var \Propel\Runtime\Map\TableMap<T>|null
*
* @var \Propel\Runtime\Map\TableMap|null
*/
protected $tableMap;
/**
* @var \Propel\Runtime\Formatter\AbstractFormatter|null
*/
protected $formatter;
/**
* @var array
*/
protected $with = [];
/**
* @phpstan-var class-string<\Propel\Runtime\Formatter\AbstractFormatter>
*
* @var string
*/
protected $defaultFormatterClass = ModelCriteria::FORMAT_OBJECT;
/**
* Creates a new instance with the default capacity which corresponds to
* the specified database.
*
* @phpstan-param class-string<T> $modelName
*
* @param string|null $dbName The database name
* @param string|null $modelName The phpName of a model, e.g. 'Book'
* @param string|null $modelAlias The alias for the model in this query, e.g. 'b'
*/
public function __construct(?string $dbName = null, ?string $modelName = null, ?string $modelAlias = null)
{
parent::__construct($dbName);
$this->setModelName($modelName);
$this->modelAlias = $modelAlias;
}
/**
* Gets the array of ModelWith specifying which objects must be hydrated
* together with the main object.
*
* @see with()
*
* @return array<\Propel\Runtime\ActiveQuery\ModelWith>
*/
public function getWith(): array
{
return $this->with;
}
/**
* Sets the array of ModelWith specifying which objects must be hydrated
* together with the main object.
*
* @param array $with
*
* @return $this The current object, for fluid interface
*/
public function setWith(array $with)
{
$this->with = $with;
return $this;
}
/**
* Sets the formatter to use for the find() output
* Formatters must extend AbstractFormatter
* Use the ModelCriteria constants for class names:
* <code>
* $c->setFormatter(ModelCriteria::FORMAT_ARRAY);
* </code>
*
* @phpstan-template AColl of \Propel\Runtime\Collection\Collection
* @phpstan-template AReturn
*
* @phpstan-param \Propel\Runtime\Formatter\AbstractFormatter<T, AColl, AReturn>|class-string<\Propel\Runtime\Formatter\AbstractFormatter> $formatter
*
* @phpstan-return $this<T, AColl, AReturn>
*
* @param \Propel\Runtime\Formatter\AbstractFormatter|string $formatter a formatter class name, or a formatter instance
*
* @throws \Propel\Runtime\Exception\InvalidArgumentException
*
* @return $this The current object, for fluid interface
*/
public function setFormatter($formatter)
{
if (is_string($formatter)) {
$formatter = new $formatter($this);
}
if (!$formatter instanceof AbstractFormatter) {
throw new InvalidArgumentException('setFormatter() only accepts classes extending AbstractFormatter');
}
$this->formatter = $formatter;
return $this;
}
/**
* Gets the formatter to use for the find() output
* Defaults to an instance of ModelCriteria::$defaultFormatterClass, i.e. PropelObjectsFormatter
*
* @return \Propel\Runtime\Formatter\AbstractFormatter
*/
public function getFormatter(): AbstractFormatter
{
if ($this->formatter === null) {
$formatterClass = $this->defaultFormatterClass;
$this->formatter = new $formatterClass();
}
return $this->formatter;
}
/**
* Returns the name of the class for this model criteria
*
* @phpstan-return class-string<T>
*
* @return string|null
*/
public function getModelName(): ?string
{
return $this->modelName;
}
/**
* Returns the name of the class for this model criteria
*
* @throws \Propel\Runtime\Exception\LogicException
*
* @return string
*/
public function getModelNameOrFail(): string
{
$modelName = $this->getModelName();
if ($modelName === null) {
throw new LogicException('Model name is not defined.');
}
return $modelName;
}
/**
* Sets the model name.
* This also sets `this->modelTableMapName` and `this->tableMap`.
*
* @phpstan-param class-string<T>|null $modelName
*
* @param string|null $modelName
*
* @return $this The current object, for fluid interface
*/
public function setModelName(?string $modelName)
{
if (!$modelName) {
$this->modelName = null;
return $this;
}
if (strpos($modelName, '\\') === 0) {
/** @phpstan-var class-string<T> $modelName */
$modelName = substr($modelName, 1);
}
$this->modelName = $modelName;
if (!$this->modelTableMapName) {
$this->modelTableMapName = $modelName::TABLE_MAP;
}
$dbName = $this->getDbName();
$this->tableMap = Propel::getServiceContainer()->getDatabaseMap($dbName)->getTableByPhpName($modelName);
$this->setPrimaryTableName($this->modelTableMapName::TABLE_NAME);
return $this;
}
/**
* @phpstan-return class-string<T>
*
* @return string
*/
public function getFullyQualifiedModelName(): string
{
return '\\' . $this->getModelName();
}
/**
* Sets the alias for the model in this query
*
* @param string $modelAlias The model alias
* @param bool $useAliasInSQL Whether to use the alias in the SQL code (false by default)
*
* @return $this The current object, for fluid interface
*/
public function setModelAlias(string $modelAlias, bool $useAliasInSQL = false)
{
if ($useAliasInSQL) {
$this->addAlias($modelAlias, $this->tableMap->getName());
$this->useAliasInSQL = true;
}
$this->modelAlias = $modelAlias;
return $this;
}
/**
* Returns the alias of the main class for this model criteria
*
* @return string|null The model alias
*/
public function getModelAlias(): ?string
{
return $this->modelAlias;
}
/**
* Return the string to use in a clause as a model prefix for the main model
*
* @return string|null The model alias if it exists, the model name if not
*/
public function getModelAliasOrName(): ?string
{
return $this->modelAlias ?: $this->modelName;
}
/**
* Return The short model name (the short ClassName for class with namespace)
*
* @return string The short model name
*/
public function getModelShortName(): string
{
return static::getShortName($this->modelName ?: '');
}
/**
* Return the short ClassName for class with namespace
*
* @param string $fullyQualifiedClassName The fully qualified class name
*
* @return string The short class name
*/
public static function getShortName(string $fullyQualifiedClassName): string
{
$namespaceParts = explode('\\', $fullyQualifiedClassName);
return array_pop($namespaceParts);
}
/**
* Returns the TableMap object for this Criteria
*
* @phpstan-return \Propel\Runtime\Map\TableMap<T>|null
*
* @return \Propel\Runtime\Map\TableMap|null
*/
public function getTableMap(): ?TableMap
{
return $this->tableMap;
}
/**
* Returns the TableMap object for this Criteria
*
* @throws \Propel\Runtime\Exception\LogicException
*
* @return \Propel\Runtime\Map\TableMap
*/
public function getTableMapOrFail(): TableMap
{
$tableMap = $this->getTableMap();
if ($tableMap === null) {
throw new LogicException('Table map is not defined.');
}
return $tableMap;
}
/**
* Returns the name of the table as used in the query.
*
* Either the SQL name or an alias.
*
* @return string|null
*/
public function getTableNameInQuery(): ?string
{
if ($this->useAliasInSQL && $this->modelAlias) {
return $this->modelAlias;
}
return $this->getTableMap()->getName();
}
/**
* Execute the query with a find(), and return a Traversable object.
*
* The return value depends on the query formatter. By default, this returns an ArrayIterator
* constructed on a Propel\Runtime\Collection\PropelCollection.
* Compulsory for implementation of \IteratorAggregate.
*
* @throws \Propel\Runtime\Exception\LogicException
*
* @return \Traversable<T>
*/
public function getIterator(): Traversable
{
$res = $this->find(null); // use the default connection
if ($res instanceof IteratorAggregate) {
return $res->getIterator();
}
if ($res instanceof Traversable) {
return $res;
}
if (is_array($res)) {
return new ArrayIterator($res);
}
throw new LogicException('The current formatter doesn\'t return an iterable result');
}
/**
* Issue a SELECT query based on the current ModelCriteria
* and format the list of results with the current formatter
* By default, returns an array of model objects
*
* @phpstan-return T|mixed
*
* @param \Propel\Runtime\Connection\ConnectionInterface|null $con an optional connection object
*
* @return \Propel\Runtime\Collection\ObjectCollection|\Propel\Runtime\ActiveRecord\ActiveRecordInterface[]|mixed the list of results, formatted by the current formatter
*/
abstract public function find(?ConnectionInterface $con = null);
}