prisma-client-php 0.0.24 → 0.0.26

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.
@@ -230,6 +230,11 @@ final class PPHPUtility
230
230
  continue;
231
231
  }
232
232
 
233
+ if ($key === '_count' && is_array($value) && array_key_exists('select', $value)) {
234
+ $includes[$key] = $value;
235
+ continue;
236
+ }
237
+
233
238
  self::processIncludeValue($key, $value, $relatedEntityFields, $fields, $modelName, $key);
234
239
 
235
240
  if (is_numeric($key) && is_string($value)) {
@@ -718,14 +723,18 @@ final class PPHPUtility
718
723
  }
719
724
 
720
725
  public static function processRelation(
721
- string $fieldName,
726
+ array $relatedField,
722
727
  array $fieldData,
723
728
  array $relationFromFields,
724
729
  array $relationToFields,
725
730
  string $relatedClassName,
731
+ string $model,
726
732
  PDO $pdo,
733
+ string $dbType,
734
+ string $lastInsertId = '',
727
735
  bool $requestOption = true,
728
736
  ): array {
737
+ $fieldName = $relatedField['name'];
729
738
  if (count($relationFromFields) !== count($relationToFields)) {
730
739
  throw new Exception("Mismatch between 'relationFromFields' and 'relationToFields' for relation '$fieldName'.");
731
740
  }
@@ -738,8 +747,38 @@ final class PPHPUtility
738
747
  $relatedData = ['data' => $fieldData['create']];
739
748
  $relatedResult = $relatedClass->create($relatedData);
740
749
  } elseif (isset($fieldData['connect'])) {
741
- $relatedData = ['where' => $fieldData['connect']];
742
- $relatedResult = $relatedClass->findUnique($relatedData);
750
+ if (empty($relationToFields) && empty($relationFromFields) && !empty($lastInsertId)) {
751
+ $relatedDataArray = $fieldData['connect'];
752
+ $relatedId = is_array($relatedDataArray) ? reset($relatedDataArray) : $relatedDataArray;
753
+ $implicitModelInfo = PPHPUtility::compareStringsAlphabetically($relatedField['type'], $model);
754
+ $searchColumn = ($relatedField['type'] === $implicitModelInfo['A']) ? 'B' : 'A';
755
+ $returnColumn = ($searchColumn === 'A') ? 'B' : 'A';
756
+
757
+ $searchColumnValue = '';
758
+ $returnColumnValue = '';
759
+ if ($implicitModelInfo['A'] === $model) {
760
+ $searchColumnValue = $lastInsertId;
761
+ $returnColumnValue = $relatedId;
762
+ } else {
763
+ $searchColumnValue = $relatedId;
764
+ $returnColumnValue = $lastInsertId;
765
+ }
766
+
767
+ $tableName = self::quoteColumnName($dbType, $implicitModelInfo['Name']);
768
+ $searchColumn = self::quoteColumnName($dbType, $searchColumn);
769
+ $returnColumn = self::quoteColumnName($dbType, $returnColumn);
770
+ $sql = "INSERT IGNORE INTO $tableName ($searchColumn, $returnColumn) VALUES (?,?)";
771
+ $stmt = $pdo->prepare($sql);
772
+ $stmt->execute([$searchColumnValue, $returnColumnValue]);
773
+
774
+ $sqlSelect = "SELECT * FROM $tableName WHERE $searchColumn = ? AND $returnColumn = ?";
775
+ $stmtSelect = $pdo->prepare($sqlSelect);
776
+ $stmtSelect->execute([$searchColumnValue, $returnColumnValue]);
777
+ $relatedResult = $stmtSelect->fetch();
778
+ } else {
779
+ $relatedData = ['where' => $fieldData['connect']];
780
+ $relatedResult = $relatedClass->findUnique($relatedData);
781
+ }
743
782
  } elseif (isset($fieldData['connectOrCreate'])) {
744
783
  $relatedData = ['where' => $fieldData['connectOrCreate']['where']];
745
784
  $relatedResult = $relatedClass->findUnique($relatedData);
@@ -844,7 +883,8 @@ final class PPHPUtility
844
883
  array $includes,
845
884
  array $fields,
846
885
  array $fieldsRelatedWithKeys,
847
- PDO $pdo
886
+ PDO $pdo,
887
+ string $dbType,
848
888
  ): array {
849
889
  $isSingle = !isset($records[0]) || !is_array($records[0]);
850
890
  if ($isSingle) {
@@ -1007,7 +1047,10 @@ final class PPHPUtility
1007
1047
 
1008
1048
  $idValue = $singleRecord[$idField];
1009
1049
 
1010
- $sql = "SELECT " . $returnColumn . " FROM " . $implicitModelInfo['Name'] . " WHERE " . $searchColumn . " = :id";
1050
+ $tableName = PPHPUtility::quoteColumnName($dbType, $implicitModelInfo['Name']);
1051
+ $searchColumn = PPHPUtility::quoteColumnName($dbType, $searchColumn);
1052
+ $returnColumn = PPHPUtility::quoteColumnName($dbType, $returnColumn);
1053
+ $sql = "SELECT " . $returnColumn . " FROM " . $tableName . " WHERE " . $searchColumn . " = :id";
1011
1054
  $stmt = $pdo->prepare($sql);
1012
1055
  $stmt->execute(['id' => $idValue]);
1013
1056
  $implicitRecords = $stmt->fetchAll(PDO::FETCH_COLUMN);
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": "0.0.24",
4
+ "version": "0.0.26",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {