Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
indexelement.php
1<?php
7use Bitrix\Main\Entity\DataManager;
8
9
26final class IndexElementTable extends DataManager
27{
29
35 public static function getTableName()
36 {
37 return 'b_sale_d_ix_element';
38 }
39
45 public static function getMap()
46 {
47 return array(
48 'ID' => array(
49 'data_type' => 'integer',
50 'primary' => true,
51 'autocomplete' => true,
52 ),
53 'DISCOUNT_ID' => array(
54 'data_type' => 'integer',
55 'required' => true,
56 ),
57 'DISCOUNT' => array(
58 'data_type' => '\Bitrix\Sale\Internals\DiscountTable',
59 'reference' => array(
60 '=this.DISCOUNT_ID' => 'ref.ID'
61 ),
62 'join_type' => 'INNER',
63 ),
64 'DISCOUNT_GROUP' => array(
65 'data_type' => '\Bitrix\Sale\Internals\DiscountGroupTable',
66 'reference' => array(
67 '=this.DISCOUNT_ID' => 'ref.DISCOUNT_ID'
68 ),
69 'join_type' => 'INNER',
70 ),
71 'ELEMENT_ID' => array(
72 'data_type' => 'integer',
73 'required' => true,
74 ),
75 );
76 }
77
84 public static function deleteByDiscount($discountId)
85 {
86 $discountId = (int)$discountId;
87 if($discountId <= 0)
88 {
89 return;
90 }
91
92 $connection = Application::getConnection();
93 $helper = $connection->getSqlHelper();
94 $connection->queryExecute(
95 'delete from ' . $helper->quote(self::getTableName()) .
96 ' where ' . $helper->quote('DISCOUNT_ID') . ' = ' . $discountId
97 );
98 }
99
100 public static function fillByDiscount($discountId, array $indexData)
101 {
102 $items = array();
103 foreach($indexData as $elementId)
104 {
105 $items[] = array(
106 'DISCOUNT_ID' => $discountId,
107 'ELEMENT_ID' => $elementId,
108 );
109 }
110
111 static::insertBatch($items);
112 }
113
119 private static function insertBatch(array $items)
120 {
121 $tableName = static::getTableName();
122 $connection = Application::getConnection();
123 $sqlHelper = $connection->getSqlHelper();
124
125 $query = $prefix = '';
126 if($connection instanceof MysqlCommonConnection)
127 {
128 foreach($items as $item)
129 {
130 list($prefix, $values) = $sqlHelper->prepareInsert($tableName, $item);
131
132 $query .= ($query ? ', ' : ' ') . '(' . $values . ')';
133 if(mb_strlen($query) > self::MAX_LENGTH_BATCH_MYSQL_QUERY)
134 {
135 $connection->queryExecute("INSERT INTO {$tableName} ({$prefix}) VALUES {$query}");
136 $query = '';
137 }
138 }
139 unset($item);
140
141 if($query && $prefix)
142 {
143 $connection->queryExecute("INSERT INTO {$tableName} ({$prefix}) VALUES {$query}");
144 }
145 }
146 elseif($connection instanceof MssqlConnection)
147 {
148 $valueData = array();
149 foreach($items as $item)
150 {
151 list($prefix, $values) = $sqlHelper->prepareInsert($tableName, $item);
152 $valueData[] = "SELECT {$values}";
153 }
154 unset($item);
155
156 $valuesSql = implode(' UNION ALL ', $valueData);
157 if($valuesSql && $prefix)
158 {
159 $connection->queryExecute("INSERT INTO {$tableName} ({$prefix}) $valuesSql");
160 }
161 }
162 elseif($connection instanceof OracleConnection)
163 {
164 $valueData = array();
165 foreach($items as $item)
166 {
167 list($prefix, $values) = $sqlHelper->prepareInsert($tableName, $item);
168 $valueData[] = "SELECT {$values} FROM dual";
169 }
170 unset($item);
171
172 $valuesSql = implode(' UNION ALL ', $valueData);
173 if($valuesSql && $prefix)
174 {
175 $connection->queryExecute("INSERT INTO {$tableName} ({$prefix}) $valuesSql");
176 }
177 }
178 }
179}
static getConnection($name="")
static fillByDiscount($discountId, array $indexData)