Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
route.php
1<?php
2
4
8use Bitrix\Main\Entity\AddResult;
9use Bitrix\Main\Entity\ScalarField;
10
27class RouteTable extends Entity\DataManager
28{
29
35 public static function getTableName()
36 {
37 return 'b_urlpreview_route';
38 }
39
45 public static function getMap()
46 {
47 return array(
48 'ID' => new Entity\IntegerField('ID', array(
49 'primary' => true,
50 'autocomplete' => true,
51 )),
52 'ROUTE' => new Entity\StringField('ROUTE', array(
53 'required' => true,
54 'unique' => true
55 )),
56 'MODULE' => new Entity\StringField('MODULE', array(
57 'required' => true,
58 )),
59 'CLASS' => new Entity\StringField('CLASS', array(
60 'required' => true,
61 )),
62 'PARAMETERS' => new Entity\TextField('PARAMETERS', array(
63 'serialized' => true,
64 )),
65 );
66 }
67
75 public static function getByRoute($route)
76 {
77 $parameters = array(
78 'select' => array('*'),
79 'filter' => array(
80 '=ROUTE' => $route,
81 )
82 );
83
84 return static::getList($parameters)->fetch();
85 }
86
93 public static function merge(array $data)
94 {
95 $result = new AddResult();
96
97 try
98 {
99 // set fields with default values
100 foreach (static::getEntity()->getFields() as $field)
101 {
102 if ($field instanceof ScalarField && !array_key_exists($field->getName(), $data))
103 {
104 $defaultValue = $field->getDefaultValue();
105
106 if ($defaultValue !== null)
107 {
108 $data[$field->getName()] = $field->getDefaultValue();
109 }
110 }
111 }
112
113 static::checkFields($result, null, $data);
114
115 // use save modifiers
116 foreach ($data as $fieldName => $value)
117 {
118 $field = static::getEntity()->getField($fieldName);
119 $data[$fieldName] = $field->modifyValueBeforeSave($value, $data);
120 }
121
122 $helper = Application::getConnection()->getSqlHelper();
123 $insertData = $data;
124 $updateData = $data;
125 unset($updateData['ROUTE']);
126 $merge = $helper->prepareMerge(
127 static::getTableName(),
128 array("ROUTE"),
129 $insertData,
130 $updateData
131 );
132
133 if ($merge[0] != "")
134 {
135 Application::getConnection()->query($merge[0]);
136 $id = Application::getConnection()->getInsertedId();
137 if($id == 0)
138 {
139 $updatedRecord = static::getByRoute($data['ROUTE']);
140 $id = $updatedRecord['ID'];
141 }
142 $result->setId($id);
143 $result->setData($data);
144 }
145 else
146 {
147 $result->addError(new Main\Error('Error constructing query'));
148 }
149 }
150 catch (\Exception $e)
151 {
152 // check result to avoid warning
153 $result->isSuccess();
154
155 throw $e;
156 }
157
158 return $result;
159 }
160}
static getConnection($name="")
static merge(array $data)
Definition route.php:93