1
|
|
|
<?php |
2
|
|
|
namespace Nkey\Caribu\Orm; |
3
|
|
|
|
4
|
|
|
use Nkey\Caribu\Model\AbstractModel; |
5
|
|
|
|
6
|
|
|
trait OrmEntityAnalyzer |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Include mapping related functionality |
10
|
|
|
*/ |
11
|
|
|
use OrmMapping; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Get the name of table |
15
|
|
|
* |
16
|
|
|
* @param string $class |
17
|
|
|
* The name of class |
18
|
|
|
* |
19
|
|
|
* @return string The name of table |
20
|
|
|
* |
21
|
|
|
* @throws OrmException |
22
|
|
|
*/ |
23
|
32 |
|
private static function getTableName(string $class): string |
24
|
|
|
{ |
25
|
32 |
|
$parts = explode('\\', $class); |
26
|
32 |
|
$simpleClassName = end($parts); |
27
|
32 |
|
$tableName = strtolower($simpleClassName); |
28
|
32 |
|
$tableName = preg_replace('#model$#', '', $tableName); |
29
|
32 |
|
return self::getAnnotatedTableName($class, $tableName); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Retrieve the primary key value |
34
|
|
|
* |
35
|
|
|
* @param string $class |
36
|
|
|
* The name of class where to retrieve the primary key value |
37
|
|
|
* |
38
|
|
|
* @param AbstractModel $object |
39
|
|
|
* The object instance |
40
|
|
|
* |
41
|
|
|
* @param bool $onlyValue |
42
|
|
|
* Whether to retrieve only primary key value or both, value and column name |
43
|
|
|
* |
44
|
|
|
* @return array Pair of column name and value of primary key or value only |
45
|
|
|
* |
46
|
|
|
* @throws OrmException |
47
|
|
|
*/ |
48
|
16 |
|
private static function getPrimaryKey(string $class, AbstractModel $object, bool $onlyValue = false) |
49
|
|
|
{ |
50
|
16 |
|
$primaryKey = self::getAnnotatedPrimaryKey($class, $object, $onlyValue); |
51
|
|
|
|
52
|
16 |
|
if (null !== $primaryKey) { |
53
|
12 |
|
return $primaryKey; |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
$pkCol = self::getPrimaryKeyCol($class); |
57
|
4 |
|
$method = sprintf("get%s", ucfirst($pkCol)); |
58
|
|
|
|
59
|
|
|
try { |
60
|
4 |
|
$rfMethod = new \ReflectionMethod($class, $method); |
61
|
3 |
|
$primaryKey = $rfMethod->invoke($object); |
62
|
3 |
|
if (! $onlyValue) { |
63
|
|
|
$primaryKey = array( |
64
|
3 |
|
$pkCol => $primaryKey |
65
|
|
|
); |
66
|
|
|
} |
67
|
1 |
|
} catch (\ReflectionException $exception) { |
68
|
1 |
|
throw OrmException::fromPrevious($exception); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
return $primaryKey; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Retrieve the name of column which represents the primary key |
76
|
|
|
* |
77
|
|
|
* @param string $class |
78
|
|
|
* The name of class |
79
|
|
|
* |
80
|
|
|
* @return string The name of primary key column |
81
|
|
|
* |
82
|
|
|
* @throws OrmException |
83
|
|
|
*/ |
84
|
17 |
|
private static function getPrimaryKeyCol(string $class): string |
85
|
|
|
{ |
86
|
17 |
|
$instance = self::getInstance(); |
87
|
|
|
|
88
|
17 |
|
$pkColumn = self::getAnnotatedPrimaryKeyColumn($class); |
89
|
17 |
|
if ("" === $pkColumn) { |
90
|
5 |
|
$pkColumn = $instance->getDbType()->getPrimaryKeyColumn(self::getTableName($class), $instance); |
91
|
|
|
} |
92
|
17 |
|
if ("" === $pkColumn) { |
93
|
1 |
|
$pkColumn = 'id'; |
94
|
|
|
} |
95
|
|
|
|
96
|
17 |
|
return $pkColumn; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|