1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
insertignore.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\ORM\Data\AddStrategy;
4
5use Bitrix\Main\NotSupportedException;
6use Bitrix\Main\ORM\Data\AddStrategy\Contract\AddStrategy;
7use Bitrix\Main\ORM\Data\AddStrategy\Internal\Helper;
8use Bitrix\Main\ORM\Entity;
9
10final class InsertIgnore implements AddStrategy
11{
12 private readonly array $uniqueFieldNames;
13
14 public function __construct(
15 private readonly Entity $entity,
16 ?array $uniqueFieldNames = null,
17 )
18 {
19 $this->uniqueFieldNames = $uniqueFieldNames ?? $this->entity->getPrimaryArray();
20
21 if (!Helper::isEntitySupportedByUniqueValidatingStrategies($this->entity, $this->uniqueFieldNames))
22 {
23 throw new NotSupportedException('Entity is not supported by the ' . self::class);
24 }
25 }
26
27 public function add(array $dbFields): AddedData
28 {
29 $insertedId = $this->insertIgnore($dbFields);
30
31 $isDBChanged = $this->isDBChanged();
32
33 if ($insertedId !== 0)
34 {
35 return new AddedData($insertedId, $isDBChanged);
36 }
37 else
38 {
39 return new AddedData(
40 Helper::guessInsertedId($this->entity, $dbFields, $this->uniqueFieldNames),
41 $isDBChanged,
42 );
43 }
44 }
45
46 public function addMulti(array $multiDbFields): AddedMultiData
47 {
48 $this->insertIgnoreMulti($multiDbFields);
49
50 return new AddedMultiData($this->isDBChanged());
51 }
52
53 private function insertIgnore(array $insertFields): int
54 {
55 $tableName = $this->entity->getDBTableName();
56 $sqlHelper = $this->entity->getConnection()->getSqlHelper();
57
58 [$sqlFieldNames, $sqlValues] = $sqlHelper->prepareInsert(
59 $tableName,
60 $insertFields,
61 );
62
63 $sql = $sqlHelper->getInsertIgnore($tableName, " ({$sqlFieldNames})", " VALUES ({$sqlValues})");
64
65 return Helper::executeAndGetInsertedId($this->entity, $sql);
66 }
67
68 private function insertIgnoreMulti(array $multiInsertFields): void
69 {
70 $connection = $this->entity->getConnection();
71 $tableName = $this->entity->getDBTableName();
72 $sqlHelper = $connection->getSqlHelper();
73
74 $uniqueColumns = [];
75 $inserts = [];
76
77 // prepare data
78 foreach ($multiInsertFields as $insertFields)
79 {
80 $insert = $sqlHelper->prepareInsert($tableName, $insertFields, true);
81 $inserts[] = $insert;
82
83 // and get unique column names
84 foreach ($insert[0] as $column)
85 {
86 $uniqueColumns[$column] = true;
87 }
88 }
89
90 // prepare sql
91 $sqlValues = [];
92
93 foreach ($inserts as $insert)
94 {
95 $columns = array_flip($insert[0]);
96 $values = $insert[1];
97
98 $finalValues = [];
99
100 foreach (array_keys($uniqueColumns) as $column)
101 {
102 if (array_key_exists($column, $columns))
103 {
104 // set real value
105 $finalValues[] = $values[$columns[$column]];
106 }
107 else
108 {
109 // set default
110 $finalValues[] = 'DEFAULT';
111 }
112 }
113
114 $sqlValues[] = '(' . join(', ', $finalValues) . ')';
115 }
116
117 $sql = $sqlHelper->getInsertIgnore(
118 $tableName,
119 '(' . join(', ', array_keys($uniqueColumns)) . ')',
120 'VALUES ' . join(', ', $sqlValues),
121 );
122
123 $connection->queryExecute($sql);
124 }
125
126 private function isDBChanged(): bool
127 {
128 // on mysql count will be 0 if no changes, 1 if inserted
129 // pgsql will always return 1 on insert-ignore
130 return $this->entity->getConnection()->getAffectedRowsCount() > 0;
131 }
132}
$connection
Определения actionsdefinitions.php:38
__construct(private readonly Entity $entity, ?array $uniqueFieldNames=null,)
Определения insertignore.php:14
addMulti(array $multiDbFields)
Определения insertignore.php:46
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$entity
Определения ufield.php:9