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\Cache\Adapter\Apcu; |
15
|
|
|
use Phalcon\Mvc\Model\MetaData; |
16
|
|
|
use Zemit\Mvc\Model; |
17
|
|
|
use Zemit\Support\Utils; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @todo, fix phalcon models meta data instead of just resetting it |
21
|
|
|
*/ |
22
|
|
|
class Dynamic extends Model |
23
|
|
|
{ |
24
|
|
|
protected $_metaData = []; |
25
|
|
|
protected $_columnMap = []; |
26
|
|
|
|
27
|
|
|
public function initialize(): void |
28
|
|
|
{ |
29
|
|
|
$this->setConnectionService('dbd'); |
30
|
|
|
$this->setReadConnectionService('dbd'); |
31
|
|
|
$this->setWriteConnectionService('dbd'); |
32
|
|
|
|
33
|
|
|
// force delete cache for dynamic models |
34
|
|
|
// @todo find a better way to handle dynamic models using |
35
|
|
|
// meta data strategy or by overriding models meta data caching adapter etc. |
36
|
|
|
// ->reset didn't work for unknown reason |
37
|
|
|
$modelsMetaData = $this->getModelsMetaData(); |
38
|
|
|
assert($modelsMetaData instanceof MetaData); |
39
|
|
|
$adapter = $modelsMetaData->getAdapter(); |
|
|
|
|
40
|
|
|
if ($adapter instanceof Apcu) { |
41
|
|
|
$lowerClassName = strtolower(Utils::getName($this)); |
42
|
|
|
$adapter->delete('meta-' . $lowerClassName); |
43
|
|
|
$adapter->delete('map-' . $lowerClassName); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
parent::initialize(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set the source table dynamically. |
51
|
|
|
*/ |
52
|
|
|
public function setDynamicSource(string $table): void |
53
|
|
|
{ |
54
|
|
|
$this->setSource($table); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sets dynamic metadata for the object. |
59
|
|
|
*/ |
60
|
|
|
public function setDynamicMetaData(array $metaData): void |
61
|
|
|
{ |
62
|
|
|
$this->_metaData = $metaData; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the column mapping dynamically. |
67
|
|
|
*/ |
68
|
|
|
public function setDynamicColumnMap(array $map): void |
69
|
|
|
{ |
70
|
|
|
$this->_columnMap = $map; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Retrieves the metadata associated with the object. |
75
|
|
|
* |
76
|
|
|
* @return array The metadata as an associative array. Returns an empty array if no metadata is set. |
77
|
|
|
*/ |
78
|
|
|
// public function metaData(): array |
79
|
|
|
// { |
80
|
|
|
// return $this->_metaData ??= $this->getModelsMetaData()->readMetaData($this); |
81
|
|
|
// } |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Dynamically set the column mapping. |
85
|
|
|
* Phalcon uses this to map database columns to model attributes. |
86
|
|
|
*/ |
87
|
|
|
public function columnMap(): array |
88
|
|
|
{ |
89
|
|
|
if (empty($this->_columnMap)) { |
90
|
|
|
// Dynamically load column names from the database (or handle them dynamically) |
91
|
|
|
$metadata = $this->getModelsMetaData(); |
92
|
|
|
$dataTypes = $metadata->getDataTypes($this); |
93
|
|
|
$columnMap = array_keys($dataTypes); |
94
|
|
|
|
95
|
|
|
// Store and return the dynamically generated column map |
96
|
|
|
$this->_columnMap = array_combine($columnMap, $columnMap); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->_columnMap; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Create a dynamic instance with a specific source and column map. |
104
|
|
|
*/ |
105
|
|
|
public static function createInstance(string $source, array $columnMap = []): Dynamic |
106
|
|
|
{ |
107
|
|
|
$instance = new self(); |
108
|
|
|
$instance->setDynamicSource($source); |
109
|
|
|
if (!empty($columnMap)) { |
110
|
|
|
$instance->setDynamicColumnMap($columnMap); |
111
|
|
|
} |
112
|
|
|
return $instance; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.