Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
dictionary.php
1<?php
8
10{
11 protected $iblockId = 0;
12 protected $cache = array();
13 protected static $exists = array();
14
18 public function __construct($iblockId)
19 {
20 $this->iblockId = intval($iblockId);
21 }
22
28 public function getIblockId()
29 {
30 return $this->iblockId;
31 }
32
38 public function getTableName()
39 {
40 return "b_iblock_".$this->iblockId."_index_val";
41 }
42
49 public function isExists()
50 {
51 if (!isset(self::$exists[$this->iblockId]))
52 {
53 $connection = \Bitrix\Main\Application::getConnection();
54 self::$exists[$this->iblockId] = $connection->isTableExists($this->getTableName());
55 }
56
57 return self::$exists[$this->iblockId];
58 }
59
66 public static function validateValue()
67 {
68 return array(
69 new \Bitrix\Main\Entity\Validator\Length(null, 2000),
70 );
71 }
72
79 public function create()
80 {
81 $connection = \Bitrix\Main\Application::getConnection();
82
83 $connection->createTable($this->getTableName(), array(
84 "ID" => new \Bitrix\Main\Entity\IntegerField("ID", array(
85 'primary' => true,
86 'unique' => true,
87 'required' => true,
88 )),
89 "VALUE" => new \Bitrix\Main\Entity\StringField("VALUE", array(
90 'required' => true,
91 'validation' => array(__CLASS__, 'validateValue'),
92 )),
93 ), array("ID"), array("ID"));
94
95 $connection->createIndex($this->getTableName(), 'IX_'.$this->getTableName().'_0', array("VALUE"), array("VALUE" => 200));
96
97 $this->cache = array();
98 self::$exists[$this->iblockId] = true;
99 }
100
107 public function drop()
108 {
109 $connection = \Bitrix\Main\Application::getConnection();
110
111 $connection->dropTable($this->getTableName());
112
113 $this->cache = array();
114 self::$exists[$this->iblockId] = false;
115 }
116
125 public function getStringId($value, $addWhenNotFound = true)
126 {
127 if (!isset($this->cache[$value]))
128 {
129 $connection = \Bitrix\Main\Application::getConnection();
130
131 $sqlHelper = $connection->getSqlHelper();
132 $valueId = $connection->queryScalar("SELECT ID FROM ".$this->getTableName()." WHERE VALUE = '".$sqlHelper->forSql($value)."'");
133 if ($valueId === null)
134 {
135 if ($addWhenNotFound)
136 {
137 $valueId = $connection->add($this->getTableName(), array(
138 "VALUE" => $value,
139 ));
140 }
141 else
142 {
143 $valueId = 0;
144 }
145 }
146
147 $this->cache[$value] = intval($valueId);
148 }
149
150 return $this->cache[$value];
151 }
152
160 public function getStringById($valueId)
161 {
162 $valueId = (int)$valueId;
163 if ($valueId <= 0)
164 return "";
165
166 $connection = \Bitrix\Main\Application::getConnection();
167 $stringValue = $connection->queryScalar("SELECT VALUE FROM ".$this->getTableName()." WHERE ID = ".$valueId);
168 return ($stringValue === null ? "" : $stringValue);
169 }
170
178 public function getStringByIds($valueIDs)
179 {
180 $result = [];
181 if (empty($valueIDs) || !is_array($valueIDs))
182 return $result;
183 \Bitrix\Main\Type\Collection::normalizeArrayValuesByInt($valueIDs, true);
184 if (empty($valueIDs))
185 return $result;
186
187 $connection = \Bitrix\Main\Application::getConnection();
188
189 $result = array_fill_keys($valueIDs, '');
190
191 $rs = $connection->query("SELECT ID, VALUE FROM ".$this->getTableName()." WHERE ID IN(".implode(',',$valueIDs).")");
192 while ($row = $rs->fetch())
193 {
194 $result[$row['ID']] = $row['VALUE'];
195 }
196
197 return $result;
198 }
199}
getStringId($value, $addWhenNotFound=true)