Answer the question
In order to leave comments, you need to log in
How to set the varchar type through migration to Bitrix?
I'm trying to create a migration to create an hl block, some fields must have a varchar(255) type. I can’t achieve it in any way, it turns out to specify only string in the migration, and the output is always text.
Is there any way to set the varchar(255) type via migration?
Here is a code example of how I create a table with two fields
<?php
namespace Sprint\Migration;
use Adv\AdvApplication\Migration\SprintMigrationBase;
use Bitrix\Main\Application;
use Bitrix\Main\Db\SqlQueryException;
use Throwable;
class AddHLBlockTableExample20210525162658 extends SprintMigrationBase
{
protected $description = 'Создаёт hl-блок "Пример"';
protected $moduleVersion = "3.15.1";
protected const HL_NAME = 'TableExample';
/**
* @return bool|void
*
* @throws Exceptions\HelperException
* @throws SqlQueryException
*/
public function up()
{
$helper = $this->getHelperManager();
$hlBlockId = $helper->Hlblock()->saveHlblock($this->getHlBlockData());
$this->addFields($hlBlockId, $helper);
}
/**
* @return bool|void
* @throws Exceptions\HelperException
*/
public function down()
{
$helper = $this->getHelperManager();
$helper->Hlblock()->deleteHlblockIfExists(self::HL_NAME);
}
/**
* @return array
*/
protected function getHlBlockData(): array
{
return [
'NAME' => self::HL_NAME,
'TABLE_NAME' => 'table_example',
'LANG' => [
'ru' => [
'NAME' => 'Таблица пример',
],
'en' => [
'NAME' => 'Table example',
]
]
];
}
/**
* @return array[]
*/
protected function getHlFieldsList(): array
{
return [
'UF_EXAMPLE1' => [
'FIELD_NAME' => 'UF_EXAMPLE1',
'USER_TYPE_ID' => 'string',
'XML_ID' => 'EXAMPLE1',
'SORT' => 100,
'MULTIPLE' => 'N',
'MANDATORY' => 'N',
'SHOW_FILTER' => 'I',
'SHOW_IN_LIST' => 'Y',
'EDIT_IN_LIST' => 'Y',
'IS_SEARCHABLE' => 'N',
'REQUIRED' => 'Y',
'SETTINGS' => [
'SIZE' => 255,
'DEFAULT_VALUE' => 0
],
'EDIT_FORM_LABEL' => [
'en' => 'Example1',
'ru' => 'Пример1'
],
'LIST_COLUMN_LABEL' => [
'en' => 'Example1',
'ru' => 'Пример1'
],
'LIST_FILTER_LABEL' => [
'en' => 'Example1',
'ru' => 'Пример1'
]
],
'UF_NAME' => [
'FIELD_NAME' => 'UF_NAME',
'USER_TYPE_ID' => 'string',
'XML_ID' => 'NAME',
'SORT' => 200,
'MULTIPLE' => 'N',
'MANDATORY' => 'N',
'SHOW_FILTER' => 'I',
'SHOW_IN_LIST' => 'Y',
'EDIT_IN_LIST' => 'Y',
'IS_SEARCHABLE' => 'N',
'REQUIRED' => 'Y',
'SETTINGS' => [
'SIZE' => 255,
'DEFAULT_VALUE' => ''
],
'EDIT_FORM_LABEL' => [
'en' => 'Name',
'ru' => 'Наименование'
],
'LIST_COLUMN_LABEL' => [
'en' => 'Name',
'ru' => 'Наименование'
],
'LIST_FILTER_LABEL' => [
'en' => 'Name',
'ru' => 'Наименование'
]
],
];
}
/**
* @param int $hlBlockId
* @param HelperManager $helper
*
* @throws Exceptions\HelperException
* @throws SqlQueryException
*/
protected function addFields(int $hlBlockId, HelperManager $helper): void
{
$connection = Application::getConnection();
try {
$connection->startTransaction();
foreach ($this->getHlFieldsList() as $fields) {
$helper->Hlblock()->saveField($hlBlockId, $fields);
}
$connection->commitTransaction();
} catch (Throwable $exception) {
$connection->rollbackTransaction();
throw $exception;
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question