Last active
March 17, 2017 10:59
-
-
Save insideone/49dfa891dcbab729da9124b92a2e284a to your computer and use it in GitHub Desktop.
Bitrix: Быстрое обновление множества элементов инфоблока
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Обновим сортировку у элементов с ID 55 и 56 | |
IBlockElement::MultiUpdate(array( | |
55 => array( | |
'SORT' => 66, | |
), | |
56 => array( | |
'SORT' => 888, | |
), | |
)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class IBlock | |
{ | |
static function GetSQLTypeByPropType($propType) | |
{ | |
static $sqlTypeByPropType = array( | |
'G' => 'INT', | |
'S' => 'CHAR(255)', | |
'N' => 'INT', | |
); | |
return isset($sqlTypeByPropType[$propType]) | |
? $sqlTypeByPropType[$propType] | |
: $sqlTypeByPropType['S']; | |
} | |
static function GetFieldsDefaults() | |
{ | |
$arDefFields = array( | |
'IBLOCK_SECTION' => array( | |
'NAME' => 'Привязка к разделам', | |
'MULTIPLE' => 'Y', | |
'PROPERTY_TYPE' => 'G', | |
), | |
'ACTIVE' => array( | |
'NAME' => 'Активность', | |
'MULTIPLE' => 'N', | |
'PROPERTY_TYPE' => 'S', | |
), | |
'SORT' => array( | |
'NAME' => 'Сортировка', | |
'MULTIPLE' => 'N', | |
'PROPERTY_TYPE' => 'N', | |
), | |
'NAME' => array( | |
'NAME' => 'Название', | |
'MULTIPLE' => 'N', | |
'PROPERTY_TYPE' => 'S', | |
), | |
'PREVIEW_PICTURE' => array( | |
'NAME' => 'Картинка для анонса', | |
'MULTIPLE' => 'N', | |
'PROPERTY_TYPE' => 'N', | |
), | |
'PREVIEW_TEXT_TYPE' => array( | |
'NAME' => 'Тип описания для анонса', | |
'MULTIPLE' => 'N', | |
'PROPERTY_TYPE' => 'S', | |
), | |
'PREVIEW_TEXT' => array( | |
'NAME' => 'Описание для анонса', | |
'MULTIPLE' => 'N', | |
'PROPERTY_TYPE' => 'S', | |
), | |
'DETAIL_PICTURE' => array( | |
'NAME' => 'Детальная картинка', | |
'MULTIPLE' => 'N', | |
'PROPERTY_TYPE' => 'N', | |
), | |
'DETAIL_TEXT_TYPE' => array( | |
'NAME' => 'Тип детального описания', | |
'MULTIPLE' => 'N', | |
'PROPERTY_TYPE' => 'S', | |
), | |
'DETAIL_TEXT' => array( | |
'NAME' => 'Детальное описание', | |
'MULTIPLE' => 'N' , | |
'PROPERTY_TYPE' => 'S', | |
), | |
'XML_ID' => array( | |
'NAME' => 'Внешний код', | |
'MULTIPLE' => 'N', | |
'PROPERTY_TYPE' => 'N', | |
), | |
'CODE' => array( | |
'NAME' => 'Символьный код', | |
'MULTIPLE' => 'N', | |
'PROPERTY_TYPE' => 'S', | |
), | |
'TAGS' => array( | |
'NAME' => 'Теги', | |
'MULTIPLE' => 'Y', | |
'PROPERTY_TYPE' => 'S', | |
) | |
); | |
return $arDefFields; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class IBlockElement | |
{ | |
static function MultiUpdate($arUpdate) | |
{ | |
if ( ! is_array($arUpdate) ) | |
return false; | |
$arKeys = array_keys(reset($arUpdate)); | |
if ( count($arKeys) === 0 ) | |
return false; | |
$arDefaultFields = IBlock::GetFieldsDefaults(); | |
// Объявление временной таблицы | |
$arTempTableDesc = array('ID' => 'ID INT'); | |
// Список полей апдейта | |
$arSetList = array(); | |
// Тип MySQL по ключу | |
$arTypeByKey = array( | |
'ID' => 'N' | |
); | |
foreach($arKeys as $key) | |
{ | |
if ( ! isset($arDefaultFields[$key]) ) | |
continue; | |
$propType = $arDefaultFields[$key]['PROPERTY_TYPE']; | |
$sqlType = IBlock::GetSQLTypeByPropType($propType); | |
$arTypeByKey[$key] = $propType; | |
$arTempTableDesc[$key] = "{$key} {$sqlType}"; | |
$arSetList[] = "ibe.{$key} = temp.{$key}"; | |
} | |
$arCorrectKeys = array_keys($arTempTableDesc); | |
// Значения временной таблицы | |
$arTempValues = array(); | |
foreach($arUpdate as $ELEMENT_ID => $arValues) | |
{ | |
$arLineValues = array(); | |
$arValues['ID'] = $ELEMENT_ID; | |
foreach($arCorrectKeys as $key) | |
{ | |
$type = $arTypeByKey[$key]; | |
$value = $arValues[$key]; | |
$arLineValues[] = $type === 'S' | |
? '"'.$value.'"' | |
: $value; | |
} | |
$arTempValues[$ELEMENT_ID] = '('.implode(',', $arLineValues).')'; | |
} | |
$strTempValues = implode(', ', $arTempValues); | |
$strTempTableDesc = implode(', ', $arTempTableDesc); | |
$strSetList = implode(', ', $arSetList); | |
$arQuery = array( | |
"CREATE TEMPORARY TABLE temp($strTempTableDesc)", | |
"INSERT INTO temp VALUES $strTempValues", | |
"UPDATE b_iblock_element as ibe, temp SET {$strSetList} WHERE temp.ID = ibe.ID", | |
); | |
array_map(array($GLOBALS['DB'], 'Query'), $arQuery); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment