Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
elementv2.php
1<?php
9namespace Bitrix\Iblock\ORM;
10
11
18
25abstract class ElementV2 extends CommonElement
26{
27 public function sysSaveRelations(Result $result)
28 {
29 parent::sysSaveRelations($result);
30
31 // by default property references are not handled
32 // we can collect them here and compose into one sql query
33 // and mark them as ACTUAL after query
34
36 $valueObjects = [];
37
38 // compose data for update query
39 $valuesToDb = [];
40
41 foreach ($this->entity->getFields() as $field)
42 {
43 if ($field instanceof PropertyReference)
44 {
45 // save single value references
46 if ($this->has($field->getName()))
47 {
48 $valueObject = $this->get($field->getName());
49
50 if ($valueObject->state == State::RAW || $valueObject->state == State::CHANGED)
51 {
52 $valueObjects[$field->getName()] = $valueObject;
53 }
54 }
55 }
56 elseif ($field instanceof PropertyOneToMany && $this->state === State::CHANGED)
57 {
58 // clear serialized multiple value cache
59 $columnName = 'PROPERTY_'.$field->getIblockElementProperty()->getId();
60 $valuesToDb[$columnName] = null;
61 }
62 }
63
64 foreach ($valueObjects as $valueObject)
65 {
66 // collect changed values and descriptions from all the objects
67 if ($valueObject->isChanged('VALUE'))
68 {
69 $columnName = $valueObject->entity->getField('VALUE')->getColumnName();
70 $valuesToDb[$columnName] = $valueObject->get('VALUE');
71 }
72
73 if ($valueObject->entity->hasField('DESCRIPTION') && $valueObject->isChanged('DESCRIPTION'))
74 {
75 $columnName = $valueObject->entity->getField('DESCRIPTION')->getColumnName();
76 $valuesToDb[$columnName] = $valueObject->get('DESCRIPTION');
77 }
78 }
79
80 if (!empty($valuesToDb))
81 {
82 // execute update
83 $connection = $this->entity->getConnection();
84 $helper = $connection->getSqlHelper();
85
86 $tableName = $this->entity->getSingleValueTableName();
87 $update = $helper->prepareUpdate($tableName, $valuesToDb);
88 $where = $helper->prepareAssignment($tableName, 'IBLOCK_ELEMENT_ID', $this->getId());
89
90 $sql = "UPDATE ".$helper->quote($tableName)." SET ".$update[0]." WHERE ".$where;
91 $connection->queryExecute($sql, $update[1]);
92 }
93
94 // get current values and set them as actual
95 foreach ($valueObjects as $fieldName => $valueObject)
96 {
97 $currentValues = $valueObject->collectValues(Values::CURRENT);
98
99 foreach ($currentValues as $propFieldName => $propFieldValue)
100 {
101 $valueObject->sysSetActual($propFieldName, $propFieldValue);
102 }
103
104 $valueObject->sysPostSave();
105 }
106 }
107}