1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
helper.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\ORM\Data\AddStrategy\Internal;
4
5use Bitrix\Main\ORM\Entity;
6use Bitrix\Main\ORM\Query\Query;
7
11final class Helper
12{
13 private function __construct()
14 {
15 }
16
17 public static function isEntitySupportedByUniqueValidatingStrategies(Entity $entity, array $uniqueFieldNames): bool
18 {
19 return (
20 self::isUniqueIdenticalToPrimary($entity, $uniqueFieldNames)
21 || (
22 self::isEntityHasAutoIncrementID($entity) && !self::isUniqueContainsPrimary($entity, $uniqueFieldNames)
23 )
24 );
25 }
26
27 private static function isUniqueIdenticalToPrimary(Entity $entity, array $uniqueFieldNames): bool
28 {
29 $primary = $entity->getPrimaryArray();
30 sort($primary, SORT_STRING);
31 sort($uniqueFieldNames, SORT_STRING);
32
33 return $primary === $uniqueFieldNames;
34 }
35
36 private static function isEntityHasAutoIncrementID(Entity $entity): bool
37 {
38 if (empty($entity->getAutoIncrement()))
39 {
40 return false;
41 }
42
43 return $entity->getPrimaryArray() === [$entity->getAutoIncrement()];
44 }
45
46 private static function isUniqueContainsPrimary(Entity $entity, array $uniqueFieldNames): bool
47 {
48 $intersect = array_intersect($uniqueFieldNames, $entity->getPrimaryArray());
49
50 return !empty($intersect);
51 }
52
53 public static function executeAndGetInsertedId(Entity $entity, string $sql): int
54 {
55 $connection = $entity->getConnection();
56
57 if ($connection->getType() === 'pgsql' && !empty($entity->getAutoIncrement()))
58 {
59 $autoIncrementColumn = $entity->getField($entity->getAutoIncrement())->getColumnName();
60
61 $sql .= " RETURNING {$autoIncrementColumn}";
62
63 $row = $connection->query($sql)->fetch();
64 if (!is_array($row))
65 {
66 return 0;
67 }
68
69 return (int)reset($row);
70 }
71
72 $connection->queryExecute($sql);
73
74 if ($connection->getType() === 'pgsql')
75 {
76 // If table hasn't autoincrement key, PostgreSQL returns wrong last inserted id - from previous insert query in a table with autoincrement
77 return 0;
78 }
79
80 return $connection->getInsertedId();
81 }
82
83 public static function guessInsertedId(
85 array $dbFields,
86 array $uniqueFieldNames,
87 ): int
88 {
89 // yes, there is a case of $isGuessedPrimary in \Bitrix\Main\ORM\Data\DataManager::sysAddInternal.
90 // but we would rather not make a useless db query than guess in this case.
91 // if you use a unique-validating strategy, have an autoincrement column, and didn't specify it,
92 // you are doing something really wrong.
93 if (empty($entity->getAutoIncrement()))
94 {
95 return 0;
96 }
97
98 // here we have data after \Bitrix\Main\ORM\Fields\Field::modifyValueBeforeSave,
99 // but with field names instead of columns
100 $modifiedData = self::replaceColumnNamesWithFieldNames($entity, $dbFields);
101
102 $row = self::fetchProbablyOriginalRow($entity, $modifiedData, $uniqueFieldNames);
103 if (!$row)
104 {
105 return 0;
106 }
107
108 return self::extractInsertedId($row, $entity->getAutoIncrement());
109 }
110
111 private static function replaceColumnNamesWithFieldNames(Entity $entity, array $dbFields): array
112 {
113 $columnToFieldMap = [];
114 foreach ($entity->getScalarFields() as $field)
115 {
116 $columnToFieldMap[$field->getColumnName()] = $field->getName();
117 }
118
119 $result = [];
120 foreach ($dbFields as $column => $value)
121 {
122 $fieldName = $columnToFieldMap[$column] ?? $column;
123
124 $result[$fieldName] = $value;
125 }
126
127 return $result;
128 }
129
130 private static function fetchProbablyOriginalRow(
132 array $modifiedData,
133 array $uniqueFieldNames,
134 ): array|false
135 {
136 $query = (new Query($entity))
137 ->setSelect([$entity->getAutoIncrement()])
138 ->setLimit(1)
139 ;
140
141 foreach ($modifiedData as $field => $value)
142 {
143 if (in_array($field, $uniqueFieldNames, true))
144 {
145 $query->where($field, $value);
146 }
147 }
148
149 return $query->fetch();
150 }
151
152 private static function extractInsertedId(array $row, string $primaryFieldName): int
153 {
154 $value = $row[$primaryFieldName] ?? null;
155 if (is_numeric($value))
156 {
157 return (int)$value;
158 }
159
160 return 0;
161 }
162}
$connection
Определения actionsdefinitions.php:38
static guessInsertedId(Entity $entity, array $dbFields, array $uniqueFieldNames,)
Определения helper.php:83
static isEntitySupportedByUniqueValidatingStrategies(Entity $entity, array $uniqueFieldNames)
Определения helper.php:17
static executeAndGetInsertedId(Entity $entity, string $sql)
Определения helper.php:53
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
$entity
Определения ufield.php:9
Определения chain.php:3
return false
Определения prolog_main_admin.php:185