Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
addressrepository.php
1<?php
2
4
9
15{
17 protected $tableClass = Model\AddressTable::class;
19 protected $fieldTableClass = Model\AddressFieldTable::class;
21 protected $linkTableClass = Model\AddressLinkTable::class;
22
32 public function findById(int $id)
33 {
34 $res = $this->tableClass::getList([
35 'select' => ['*', 'FIELDS', 'LOCATION', 'LOCATION.NAME', 'LINKS'],
36 'filter' => ['=ID' => $id]
37 ])->fetchObject();
38
39 if(!$res)
40 {
41 $result = null;
42 }
43 else
44 {
45 $result = Entity\Address\Converter\OrmConverter::convertFromOrm($res);
46 }
47
48 return $result;
49 }
50
59 public function findByLinkedEntity(string $entityId, string $entityType): Entity\Address\AddressCollection
60 {
61 $res = $this->tableClass::getList([
62 'select' => ['*', 'FIELDS', 'LOCATION', 'LOCATION.NAME', 'LOCATION.FIELDS', 'LINKS'],
63 'filter' => [
64 '=LINKS.ENTITY_ID' => $entityId,
65 '=LINKS.ENTITY_TYPE' => $entityType
66 ]
67 ])->fetchCollection();
68
69 return Entity\Address\Converter\OrmConverter::convertCollectionFromOrm($res);
70 }
71
76 public function save(Entity\Address $address)
77 {
78 $result = new Result();
79
80 if($location = $address->getLocation())
81 {
82 // Even if location has id > 0. It could contain unsaved name on other language.
83 $res = $location->save();
84
85 if(!$res->isSuccess())
86 {
87 $result->addErrors($res->getErrors());
88 }
89 }
90
91 $fields = Entity\Address\Converter\DbFieldConverter::convertToDbField($address);
92
93 if((int)$fields['ID'] > 0)
94 {
95 $result = $this->tableClass::update($fields['ID'], $fields);
96 }
97 else
98 {
99 $result = $this->tableClass::add($fields);
100
101 if($result->isSuccess())
102 {
103 $address->setId($result->getId());
104 }
105 }
106
107 if($address->getId() <= 0 || !$result->isSuccess())
108 {
109 return $result;
110 }
111
112 $res = $this->saveFieldCollection($address);
113
114 if(!$res->isSuccess())
115 {
116 $result->addErrors($res->getErrors());
117 }
118
119 $res = $this->saveLinkCollection($address);
120
121 if(!$res->isSuccess())
122 {
123 $result->addErrors($res->getErrors());
124 }
125
126 return $result;
127 }
128
134 protected function saveFieldCollection(Entity\Address $address): \Bitrix\Main\ORM\Data\Result
135 {
136 if($address->getId() <= 0)
137 {
138 throw new ArgumentNullException('Address Id');
139 }
140
141 $fieldsCollection = Entity\Address\Converter\OrmConverter::convertFieldsToOrm($address);
142
143 $this->fieldTableClass::deleteByAddressId($address->getId());
144 return $fieldsCollection->save();
145 }
146
153 protected function saveLinkCollection(Entity\Address $address): \Bitrix\Main\ORM\Data\Result
154 {
155 if($address->getId() <= 0)
156 {
157 throw new ArgumentNullException('Address Id');
158 }
159
160 $this->linkTableClass::deleteByAddressId($address->getId());
161 $collectionLinks = Entity\Address\Converter\OrmConverter::convertLinksToOrm($address);
162 return $collectionLinks->save();
163 }
164
165 public function delete(int $addressId)
166 {
167 if($addressId <= 0)
168 {
169 throw new ArgumentNullException('Address Id');
170 }
171
172 $result = $this->tableClass::delete($addressId);
173 $this->fieldTableClass::deleteByAddressId($addressId);
174 $this->linkTableClass::deleteByAddressId($addressId);
175 return $result;
176 }
177}
findByLinkedEntity(string $entityId, string $entityType)