1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
addressrepository.php
См. документацию.
1<?php
2
4
10
16{
18 protected $tableClass = Model\AddressTable::class;
20 protected $fieldTableClass = Model\AddressFieldTable::class;
22 protected $linkTableClass = Model\AddressLinkTable::class;
23
33 public function findById(int $id)
34 {
35 $res = $this->tableClass::getList([
36 'select' => ['*', 'FIELDS', 'LOCATION', 'LOCATION.NAME', 'LINKS'],
37 'filter' => ['=ID' => $id],
38 ])->fetchObject();
39
40 if(!$res)
41 {
42 $result = null;
43 }
44 else
45 {
46 $result = Entity\Address\Converter\OrmConverter::convertFromOrm($res);
47 }
48
49 return $result;
50 }
51
60 public function findByLinkedEntity(string $entityId, string $entityType): Entity\Address\AddressCollection
61 {
62 $res = $this->tableClass::getList([
63 'select' => ['*', 'FIELDS', 'LOCATION', 'LOCATION.NAME', 'LOCATION.FIELDS', 'LINKS'],
64 'filter' => [
65 '=LINKS.ENTITY_ID' => $entityId,
66 '=LINKS.ENTITY_TYPE' => $entityType,
67 ],
68 ])->fetchCollection();
69
70 return Entity\Address\Converter\OrmConverter::convertCollectionFromOrm($res);
71 }
72
77 public function save(Entity\Address $address)
78 {
79 $result = new Result();
80
81 if($location = $address->getLocation())
82 {
83 // Even if location has id > 0. It could contain unsaved name on other language.
84 $res = $location->save();
85
86 if(!$res->isSuccess())
87 {
88 $result->addErrors($res->getErrors());
89 }
90 }
91
93
94 if((int)$fields['ID'] > 0)
95 {
96 $result = $this->tableClass::update($fields['ID'], $fields);
97 }
98 else
99 {
100 $result = $this->tableClass::add($fields);
101
102 if($result->isSuccess())
103 {
104 $address->setId($result->getId());
105 }
106 }
107
108 if ($address->getId() <= 0 || !$result->isSuccess())
109 {
110 return $result;
111 }
112
113 $this->saveFieldCollection($address);
114 $this->saveLinkCollection($address);
115
116 return $result;
117 }
118
119 protected function saveFieldCollection(Entity\Address $address): void
120 {
121 if ($address->getId() <= 0)
122 {
123 throw new ArgumentNullException('Address Id');
124 }
125
126 $connection = Application::getConnection();
127
128 $this->fieldTableClass::deleteByAddressId($address->getId());
129
130 $fields = [];
132 foreach ($address->getFieldCollection() as $field)
133 {
134 $value = $field->getValue();
135
136 $fields[] = [
137 'TYPE' => $field->getType(),
138 'VALUE' => $value,
139 'ADDRESS_ID' => $address->getId(),
140 'VALUE_NORMALIZED' => Entity\Address\Normalizer\Builder::build($address->getLanguageId())
141 ->normalize($value)
142 ,
143 ];
144 }
145
146 $sqls = $connection->getSqlHelper()->prepareMergeMultiple(
147 $this->fieldTableClass::getTableName(),
148 [
149 'ADDRESS_ID',
150 'TYPE',
151 ],
152 $fields
153 );
154 foreach ($sqls as $sql)
155 {
156 $connection->query($sql);
157 }
158 }
159
160 protected function saveLinkCollection(Entity\Address $address)
161 {
162 if ($address->getId() <= 0)
163 {
164 throw new ArgumentNullException('Address Id');
165 }
166
167 $connection = Application::getConnection();
168
169 $this->linkTableClass::deleteByAddressId($address->getId());
170
171 $links = [];
173 foreach ($address->getLinks() as $link)
174 {
175 $links[] = [
176 'ADDRESS_ID' => $address->getId(),
177 'ENTITY_ID' => $link->getAddressLinkEntityId(),
178 'ENTITY_TYPE' => $link->getAddressLinkEntityType(),
179 ];
180 }
181
182 $sqls = $connection->getSqlHelper()->prepareMergeMultiple(
183 $this->linkTableClass::getTableName(),
184 [
185 'ADDRESS_ID',
186 'ENTITY_ID',
187 'ENTITY_TYPE',
188 ],
189 $links
190 );
191 foreach ($sqls as $sql)
192 {
193 $connection->query($sql);
194 }
195 }
196
197 public function delete(int $addressId)
198 {
199 if($addressId <= 0)
200 {
201 throw new ArgumentNullException('Address Id');
202 }
203
204 $result = $this->tableClass::delete($addressId);
205 $this->fieldTableClass::deleteByAddressId($addressId);
206 $this->linkTableClass::deleteByAddressId($addressId);
207 return $result;
208 }
209}
$connection
Определения actionsdefinitions.php:38
static convertToDbField(Entity\Address $address)
Определения dbfieldconverter.php:20
static build(string $lang)
Определения builder.php:18
findByLinkedEntity(string $entityId, string $entityType)
Определения addressrepository.php:60
save(Entity\Address $address)
Определения addressrepository.php:77
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
Определения ufield.php:9
$entityId
Определения payment.php:4
$location
Определения options.php:2729
$fields
Определения yandex_run.php:501