1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
13
|
|
|
|
14
|
|
|
use Phalcon\Db\Column; |
15
|
|
|
use Phalcon\Di; |
16
|
|
|
use Phalcon\Mvc\Model\ManagerInterface; |
|
|
|
|
17
|
|
|
use Phalcon\Mvc\Model\Query\Builder; |
18
|
|
|
use Phalcon\Mvc\Model\QueryInterface; |
19
|
|
|
use Phalcon\Mvc\Model\ResultsetInterface; |
20
|
|
|
use Zemit\Mvc\Model; |
21
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractEventsManager; |
22
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractModelsManager; |
23
|
|
|
|
24
|
|
|
trait LifeCycle |
25
|
|
|
{ |
26
|
|
|
use AbstractModelsManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Return the query for data retention |
30
|
|
|
*/ |
31
|
|
|
public static function prepareLifeCycleQuery(Builder $builder = null, ?array $parameters = null): void |
32
|
|
|
{ |
33
|
|
|
// data life cycle policy must be defined |
34
|
|
|
if (empty($parameters)) { |
35
|
|
|
$builder->where('false'); |
|
|
|
|
36
|
|
|
$builder->setBindParams([]); |
37
|
|
|
$builder->setBindTypes([]); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function getLifeCyclePolicy(): array |
42
|
|
|
{ |
43
|
|
|
$config = Di::getDefault()->get('config'); |
44
|
|
|
$dataLifeCycleConfig = $config->pathToArray('dataLifeCycle'); |
45
|
|
|
$policyName = $dataLifeCycleConfig['models'][self::class] ?? null; |
46
|
|
|
return $dataLifeCycleConfig['models'][$policyName] ?? []; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function getLifeCyclePolicyQuery(): ?array |
50
|
|
|
{ |
51
|
|
|
return self::getLifeCyclePolicy()['query'] ?? null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Return the Query for data retention |
56
|
|
|
*/ |
57
|
|
|
public static function getLifeCycleQuery(?array $parameters = null, ?Builder $builder = null): QueryInterface |
58
|
|
|
{ |
59
|
|
|
$parameters ??= self::getLifeCyclePolicyQuery(); |
60
|
|
|
$builder ??= self::getBuilder($parameters); |
61
|
|
|
|
62
|
|
|
self::prepareLifeCycleQuery($builder, $parameters); |
63
|
|
|
|
64
|
|
|
return $builder->getQuery(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return a Query Builder based on parameters |
69
|
|
|
*/ |
70
|
|
|
public static function getBuilder(?array $parameters = null): Builder |
71
|
|
|
{ |
72
|
|
|
$di = Di::getDefault(); |
73
|
|
|
$modelsManager = $di->getShared('modelsManager'); |
74
|
|
|
assert($modelsManager instanceof ManagerInterface); |
75
|
|
|
|
76
|
|
|
$builder = $modelsManager->createBuilder($parameters); |
|
|
|
|
77
|
|
|
$builder->from(get_called_class()); |
78
|
|
|
|
79
|
|
|
if (isset($parameters['limit'])) { |
80
|
|
|
$builder->limit($parameters['limit']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $builder; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Find records to hard delete for data retention purpose |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public static function findLifeCycle(?array $parameters = null) |
91
|
|
|
{ |
92
|
|
|
$query = self::getLifeCycleQuery($parameters); |
93
|
|
|
$resultset = $query->execute(); |
94
|
|
|
|
95
|
|
|
if ($resultset instanceof ResultsetInterface) { |
96
|
|
|
if (isset($parameters['hydration'])) { |
97
|
|
|
$resultset->setHydrateMode($parameters['hydration']); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $resultset; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: