1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
PropertyCollection.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\v2\Property;
4
5use Bitrix\Catalog\v2\BaseCollection;
6use Bitrix\Catalog\v2\BaseEntity;
7use Bitrix\Main\Result;
8
18{
20 protected $repository;
21
23 {
24 $this->repository = $repository;
25 }
26
27 public function findByCode(string $code): ?Property
28 {
30 foreach ($this->getIterator() as $item)
31 {
32 if ($item->getCode() === $code)
33 {
34 return $item;
35 }
36 }
37
38 return null;
39 }
40
47 public function findByCodeLazy(string $code): ?Property
48 {
50 foreach ($this->items as $item)
51 {
52 if ($item->getCode() === $code)
53 {
54 return $item;
55 }
56 }
57
58 $propertySettings = $this->repository->getPropertiesSettingsByFilter([
59 '=IBLOCK_ID' => $this->getParent()->getIblockId(),
60 '=CODE' => $code,
61 ]);
62 $items = $this->repository->getEntitiesBy(
63 [
64 'filter' => [
65 'IBLOCK_ID' => $this->getParent()->getIblockId(),
66 'ID' => $this->getParent()->getId(),
67 'PROPERTY_CODE' => $code,
68 ],
69 ],
70 $propertySettings,
71 );
72 if ($items)
73 {
74 $item = reset($items) ?: null;
75 $this->addInternal($item);
76
77 return $item;
78 }
79
80 return null;
81 }
82
89 public function findByIdLazy(int $id): ?Property
90 {
92 foreach ($this->items as $item)
93 {
94 if ($item->getId() === $id)
95 {
96 return $item;
97 }
98 }
99
100 $propertySettings = $this->repository->getPropertiesSettingsByFilter([
101 '=ID' => $id,
102 ]);
103 $items = $this->repository->getEntitiesBy(
104 [
105 'filter' => [
106 'IBLOCK_ID' => $this->getParent()->getIblockId(),
107 'ID' => $this->getParent()->getId(),
108 'PROPERTY_ID' => $id,
109 ],
110 ],
111 $propertySettings,
112 );
113 if ($items)
114 {
115 $item = reset($items) ?: null;
116 $this->addInternal($item);
117
118 return $item;
119 }
120
121 return null;
122 }
123
128 public function setValues(array $propertyValues): self
129 {
130 foreach ($propertyValues as $index => $values)
131 {
132 $property = null;
133
134 if (is_numeric($index))
135 {
136 $property = $this->findById((int)$index);
137 }
138
139 if (!$property)
140 {
141 $property = $this->findByCode($index);
142 }
143
144 if ($property)
145 {
146 $property->getPropertyValueCollection()->setValues($values);
147 }
148 }
149
150 return $this;
151 }
152
153 public function getValues(): array
154 {
155 $values = [];
156
158 foreach ($this->getIterator() as $property)
159 {
160 $values[$property->getId()] = $property->getPropertyValueCollection()->toArray();
161 }
162
163 return $values;
164 }
165
166 public function saveInternal(): Result
167 {
168 $result = new Result();
169
170 // ToDo make lazyLoad for getPropertyCollection() to not load collection everytime
171 if ($this->isChanged())
172 {
173 // ToDo re-initialize saved ids from database after file save - check in \CIBlockElement::SetPropertyValues
174 // property collection can't be saved one by one, all simultaneously
175 $res = $this->repository->save(...$this->getIterator());
176
177 if ($res->isSuccess())
178 {
179 $this->clearChanged();
180 }
181 else
182 {
183 $result->addErrors($res->getErrors());
184 }
185 }
186
187 return $result;
188 }
189
190 public function deleteInternal(): Result
191 {
192 // properties deletes with iblock element entity by CIBlockElement api
193 return new Result();
194 }
195
196 protected function getAlreadyLoadedFilter(): array
197 {
198 $filter = parent::getAlreadyLoadedFilter();
199
200 foreach ($this->items as $item)
201 {
202 if (!$item->isNew())
203 {
204 $filter['!PROPERTY_ID'][] = $item->getId();
205 }
206 }
207
208 return $filter;
209 }
210}
addInternal(BaseEntity $item)
Определения BaseCollection.php:60
__construct(PropertyRepositoryContract $repository)
Определения PropertyCollection.php:22
setValues(array $propertyValues)
Определения PropertyCollection.php:128
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$filter
Определения iblock_catalog_list.php:54
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195