|
18 | 18 | use DBD\Common\Debug; |
19 | 19 | use DBD\Common\Options; |
20 | 20 | use DBD\Common\Query; |
| 21 | +use DBD\Entity\Column; |
21 | 22 | use DBD\Entity\Constraint; |
22 | 23 | use DBD\Entity\Entity; |
23 | 24 | use DBD\Entity\Primitives\StringPrimitives; |
@@ -750,6 +751,8 @@ protected function createInsertRecord(Entity $entity): array |
750 | 751 |
|
751 | 752 | $originName = $column->name; |
752 | 753 |
|
| 754 | + $this->convertToJsonValue($entity, $propertyName, $column); |
| 755 | + |
753 | 756 | if (!$column->nullable) { |
754 | 757 | // Mostly we always define properties for any columns |
755 | 758 | if (property_exists($entity, $propertyName)) { |
@@ -856,12 +859,7 @@ public function entityUpdate(Entity &$entity): Entity |
856 | 859 |
|
857 | 860 | foreach ($columns as $propertyName => $column) { |
858 | 861 |
|
859 | | - // If column has json type and current value is not string, then convert it to the json string |
860 | | - if (property_exists($entity, $propertyName) and isset($entity->$propertyName)) { |
861 | | - if ($column->type == StringPrimitives::String and stripos($column->originType, 'json') !== false and !is_string($entity->$propertyName)) { |
862 | | - $entity->$propertyName = json_encode($entity->$propertyName, JSON_UNESCAPED_UNICODE); |
863 | | - } |
864 | | - } |
| 862 | + $this->convertToJsonValue($entity, $propertyName, $column); |
865 | 863 |
|
866 | 864 | if ($column->nullable === false) { |
867 | 865 | if (property_exists($entity, $propertyName)) { |
@@ -1233,4 +1231,20 @@ public function commit(): bool |
1233 | 1231 | * @see DBD::commit() |
1234 | 1232 | */ |
1235 | 1233 | abstract protected function _commit(): bool; |
| 1234 | + |
| 1235 | + /** |
| 1236 | + * @param Entity $entity |
| 1237 | + * @param int|string $propertyName |
| 1238 | + * @param Column $column |
| 1239 | + * @return void |
| 1240 | + */ |
| 1241 | + private function convertToJsonValue(Entity $entity, int|string $propertyName, Column $column): void |
| 1242 | + { |
| 1243 | + // If column has json type and current value is not string, then convert it to the json string |
| 1244 | + if (property_exists($entity, $propertyName) and isset($entity->$propertyName)) { |
| 1245 | + if ($column->type == StringPrimitives::String and stripos($column->originType, 'json') !== false and !is_string($entity->$propertyName)) { |
| 1246 | + $entity->$propertyName = json_encode($entity->$propertyName, JSON_UNESCAPED_UNICODE); |
| 1247 | + } |
| 1248 | + } |
| 1249 | + } |
1236 | 1250 | } |
0 commit comments