prisma-client-php 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -834,36 +834,62 @@ final class PPHPUtility
834
834
  public static function processRelation(
835
835
  string $modelName,
836
836
  string $relatedFieldName,
837
- array $fieldData,
838
- PDO $pdo,
837
+ array $fieldData,
838
+ PDO $pdo,
839
839
  string $dbType,
840
- bool $requestOption = true,
840
+ bool $requestOption = true,
841
841
  ): array {
842
+ $modelClassName = "Lib\\Prisma\\Classes\\{$modelName}";
843
+ $modelClass = (new ReflectionClass($modelClassName))->newInstance($pdo);
842
844
 
843
- $modelClassName = "Lib\\Prisma\\Classes\\" . $modelName;
844
- $modelReflection = new ReflectionClass($modelClassName);
845
- $modelClass = $modelReflection->newInstance($pdo);
846
845
  $modelFieldsRelatedWithKeys = $modelClass->_fieldsRelatedWithKeys[$relatedFieldName];
847
- $modelRelatedFromFields = $modelFieldsRelatedWithKeys['relationFromFields'];
848
- $modelRelatedToFields = $modelFieldsRelatedWithKeys['relationToFields'];
849
- $modelRelatedField = $modelClass->_fields[$relatedFieldName];
846
+ $modelRelatedFromFields = $modelFieldsRelatedWithKeys['relationFromFields'];
847
+ $modelRelatedToFields = $modelFieldsRelatedWithKeys['relationToFields'];
848
+
849
+ $modelRelatedField = $modelClass->_fields[$relatedFieldName];
850
850
  $modelRelatedFieldIsList = $modelRelatedField['isList'] ?? false;
851
- $modelRelatedFieldType = $modelRelatedField['type'];
852
- $relatedClassName = "Lib\\Prisma\\Classes\\" . $modelRelatedFieldType;
853
- $relatedReflection = new ReflectionClass($relatedClassName);
854
- $relatedClass = $relatedReflection->newInstance($pdo);
851
+ $modelRelatedFieldType = $modelRelatedField['type'];
852
+
853
+ $relatedClassName = "Lib\\Prisma\\Classes\\{$modelRelatedFieldType}";
854
+ $relatedClass = (new ReflectionClass($relatedClassName))->newInstance($pdo);
855
+
856
+ $inverseInfo = null;
857
+ $childFkFields = [];
858
+ foreach ($relatedClass->_fieldsRelatedWithKeys as $childField => $info) {
859
+ $infoType = $info['type'] ?? ($relatedClass->_fields[$childField]['type'] ?? null);
860
+ if ($infoType === $modelName && !empty($info['relationFromFields'])) {
861
+ $inverseInfo = $info;
862
+ $childFkFields = $info['relationFromFields'];
863
+ break;
864
+ }
865
+ }
866
+ $isExplicitOneToMany = $modelRelatedFieldIsList && $inverseInfo !== null;
855
867
 
856
868
  $relatedResult = null;
869
+
857
870
  foreach ($fieldData as $action => $actionData) {
858
871
  $operations = isset($actionData[0]) ? $actionData : [$actionData];
859
872
 
860
873
  foreach ($operations as $op) {
861
874
  switch ($action) {
862
875
  case 'connect':
863
- if (empty($modelRelatedFromFields) && empty($modelRelatedToFields)) {
876
+ if ($isExplicitOneToMany) {
877
+ $parentId = $op[$childFkFields[0]]
878
+ ?? throw new Exception("Missing parent id while connecting '{$relatedFieldName}'.");
879
+
880
+ $where = array_diff_key($op, array_flip($childFkFields));
881
+ if (!$where) {
882
+ throw new Exception("A unique selector (e.g. 'code') is required inside 'connect' for '{$relatedFieldName}'.");
883
+ }
884
+ $fkUpdate = array_fill_keys($childFkFields, $parentId);
885
+ $relatedResult = $relatedClass->update([
886
+ 'where' => $where,
887
+ 'data' => $fkUpdate,
888
+ ]);
889
+ } elseif (empty($modelRelatedFromFields) && empty($modelRelatedToFields)) {
864
890
  $relatedFieldData = $op[$modelRelatedFieldType];
865
- $modelFieldData = $op[$modelName];
866
- $relatedResult = self::handleImplicitRelationInsert(
891
+ $modelFieldData = $op[$modelName];
892
+ $relatedResult = self::handleImplicitRelationInsert(
867
893
  $modelName,
868
894
  $modelRelatedFieldType,
869
895
  $dbType,
@@ -873,9 +899,8 @@ final class PPHPUtility
873
899
  );
874
900
  } else {
875
901
  if (!$modelRelatedFieldIsList && count($operations) > 1) {
876
- throw new Exception("Cannot connect multiple records for a non-list relation '$relatedFieldName'.");
902
+ throw new Exception("Cannot connect multiple records for a non-list relation '{$relatedFieldName}'.");
877
903
  }
878
-
879
904
  $relatedResult = $relatedClass->findUnique(['where' => $op]);
880
905
  }
881
906
  break;
@@ -940,23 +965,52 @@ final class PPHPUtility
940
965
  $relatedResult = $relatedClass->delete(['where' => $whereCondition]);
941
966
  break;
942
967
  case 'disconnect':
943
- if (empty($modelRelatedFromFields) && empty($modelRelatedToFields)) {
944
- $relatedFieldData = $op[$modelRelatedFieldType];
945
- $modelFieldData = $op[$modelName];
968
+ if ($isExplicitOneToMany) {
969
+ foreach ($operations as $opDisc) {
970
+ $where = array_diff_key($opDisc, array_flip($childFkFields));
971
+ $relatedClass->update([
972
+ 'where' => $where,
973
+ 'data' => array_fill_keys($childFkFields, null),
974
+ ]);
975
+ }
976
+ $relatedResult = true;
977
+ } elseif (empty($modelRelatedFromFields) && empty($modelRelatedToFields)) {
978
+ $rData = $op[$modelRelatedFieldType];
979
+ $mData = $op[$modelName];
946
980
  $relatedResult = self::handleImplicitRelationDelete(
947
981
  $modelName,
948
982
  $modelRelatedFieldType,
949
983
  $dbType,
950
984
  $pdo,
951
- $modelFieldData[$modelClass->_primaryKey],
952
- $relatedFieldData[$relatedClass->_primaryKey]
985
+ $mData[$modelClass->_primaryKey],
986
+ $rData[$relatedClass->_primaryKey]
953
987
  );
954
988
  } else {
955
989
  $relatedResult = $relatedClass->delete(['where' => $op]);
956
990
  }
957
991
  break;
958
992
  case 'set':
959
- if (empty($modelRelatedFromFields) && empty($modelRelatedToFields)) {
993
+ if ($isExplicitOneToMany) {
994
+ $parentId = $operations[0][$childFkFields[0]]
995
+ ?? throw new Exception("Missing parent id in 'set' for '{$relatedFieldName}'.");
996
+
997
+ $relatedClass->updateMany([
998
+ 'where' => [$childFkFields[0] => $parentId],
999
+ 'data' => array_fill_keys($childFkFields, null),
1000
+ ]);
1001
+
1002
+ $newIds = [];
1003
+ foreach ($operations as $opSet) {
1004
+ $where = array_diff_key($opSet, array_flip($childFkFields));
1005
+ $fkUpdate = array_fill_keys($childFkFields, $parentId);
1006
+ $updateRes = $relatedClass->update(['where' => $where, 'data' => $fkUpdate]);
1007
+ $newIds[] = $updateRes->{$relatedClass->_primaryKey};
1008
+ }
1009
+
1010
+ $relatedResult = $relatedClass->findMany([
1011
+ 'where' => [$relatedClass->_primaryKey => ['in' => $newIds]],
1012
+ ]);
1013
+ } elseif (empty($modelRelatedFromFields) && empty($modelRelatedToFields)) {
960
1014
  $newRelatedIds = [];
961
1015
  $primaryId = null;
962
1016
  foreach ($operations as $opSet) {
@@ -1018,26 +1072,23 @@ final class PPHPUtility
1018
1072
  }
1019
1073
  }
1020
1074
 
1021
- $relatedResult = (array) $relatedResult;
1075
+ $relatedResult = (array)$relatedResult;
1022
1076
 
1023
1077
  if (!$requestOption) {
1024
1078
  return $relatedResult;
1025
1079
  }
1026
-
1027
1080
  if (!$relatedResult) {
1028
- throw new Exception("Failed to process related record for '$relatedFieldName'.");
1081
+ throw new Exception("Failed to process related record for '{$relatedFieldName}'.");
1029
1082
  }
1030
1083
 
1031
1084
  $bindings = [];
1032
- foreach ($modelRelatedFromFields as $index => $fromField) {
1033
- $toField = $modelRelatedToFields[$index];
1085
+ foreach ($modelRelatedFromFields as $i => $fromField) {
1086
+ $toField = $modelRelatedToFields[$i];
1034
1087
  if (!isset($relatedResult[$toField])) {
1035
- throw new Exception("The field '$toField' is missing in the related data for '$relatedFieldName'.");
1088
+ throw new Exception("The field '{$toField}' is missing in the related data for '{$relatedFieldName}'.");
1036
1089
  }
1037
-
1038
1090
  $bindings[$fromField] = $relatedResult[$toField];
1039
1091
  }
1040
-
1041
1092
  return $bindings;
1042
1093
  }
1043
1094
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "prisma-client-php",
3
3
  "description": "Prisma Client PHP is an auto-generated query builder that enables type-safe database access in PHP.",
4
- "version": "2.0.0",
4
+ "version": "2.0.1",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {