1 | <?php |
||
11 | trait OrmPersister |
||
12 | { |
||
13 | /** |
||
14 | * Include statement related functionality |
||
15 | */ |
||
16 | use OrmStatement; |
||
17 | |||
18 | /** |
||
19 | * Set the primary key value after persist |
||
20 | * |
||
21 | * @param string $class |
||
22 | * The name of class of entity |
||
23 | * @param \Nkey\Caribu\Model\AbstractModel $object |
||
24 | * The object where the primary key should be set |
||
25 | * @param mixed $primaryKey |
||
26 | * The primary key value |
||
27 | * @throws OrmException |
||
28 | */ |
||
29 | 10 | private static function setPrimaryKey($class, $object, $primaryKey) |
|
44 | |||
45 | /** |
||
46 | * Persist the mapped-by entities |
||
47 | * |
||
48 | * @param string $class |
||
49 | * The name of class of which the data has to be persisted |
||
50 | * @param \Nkey\Caribu\Model\AbstractModel $object |
||
51 | * The entity which contain mapped-by entries to persist |
||
52 | * |
||
53 | * @throws OrmException |
||
54 | */ |
||
55 | 11 | private static function persistMappedBy(string $class, \Nkey\Caribu\Model\AbstractModel $object) |
|
102 | |||
103 | /** |
||
104 | * Persist inner entity |
||
105 | * |
||
106 | * @param \ReflectionProperty $property |
||
107 | * The property which represents the inner entity |
||
108 | * @param string $class |
||
109 | * The result class name |
||
110 | * @param \Nkey\Caribu\Model\AbstractModel $object |
||
111 | * The object which holds the entity |
||
112 | * @param string $namespace |
||
113 | * The result class namespace |
||
114 | * @param bool $persist |
||
115 | * Whether to persist |
||
116 | * |
||
117 | * @throws OrmException |
||
118 | */ |
||
119 | 13 | private static function persistProperty(\ReflectionProperty $property, string $class, \Nkey\Caribu\Model\AbstractModel $object, string $namespace, bool $persist) |
|
120 | { |
||
121 | try { |
||
122 | 13 | if ("" !== ($type = self::getAnnotatedType($property->getDocComment(), $namespace)) && ! self::isPrimitive($type)) { |
|
123 | 6 | if (! $persist && self::isCascadeAnnotated($property->getDocComment())) { |
|
124 | $persist = true; |
||
125 | } |
||
126 | |||
127 | 6 | $rfMethod = new \ReflectionMethod($class, sprintf("get%s", ucfirst($property->getName()))); |
|
128 | 6 | $entity = $rfMethod->invoke($object); |
|
129 | 6 | if ($entity instanceof \Nkey\Caribu\Model\AbstractModel) { |
|
130 | 6 | if (! $persist && count($pk = self::getAnnotatedPrimaryKey($type, $entity, false))) { |
|
131 | list ($pkCol) = $pk; |
||
132 | if (!isset($pk[$pkCol]) || empty($pk[$pkCol])) { |
||
133 | $persist = true; |
||
134 | } |
||
135 | } |
||
136 | 6 | if ($persist) { |
|
137 | 13 | $entity->persist(); |
|
138 | } |
||
139 | } |
||
140 | } |
||
141 | } catch (\ReflectionException $exception) { |
||
142 | throw OrmException::fromPrevious($exception); |
||
143 | } |
||
144 | 13 | } |
|
145 | |||
146 | /** |
||
147 | * Persist the entity and all sub entities if necessary |
||
148 | * |
||
149 | * @param string $class |
||
150 | * The name of class of which the data has to be persisted |
||
151 | * @param \Nkey\Caribu\Model\AbstractModel $object |
||
152 | * The entity to persist |
||
153 | * |
||
154 | * @throws OrmException |
||
155 | */ |
||
156 | 13 | private static function persistAnnotated(string $class, \Nkey\Caribu\Model\AbstractModel $object) |
|
168 | } |
||
169 |