Answer the question
In order to leave comments, you need to log in
How to write data to the database when installing a module?
Hello.
People, I can’t figure out how to write data to the database when installing the module.
Here is the code in install.php
function InstallDB(){
global $DB, $APPLICATION;
if (file_exists($f = dirname(__FILE__).'/db/mysql/install.sql'))
{
foreach($DB->ParseSQLBatch(file_get_contents($f)) as $sql)
$DB->Query($sql);
RegisterModuleDependences('main', 'OnPageStart', $this->MODULE_ID, 'DemoData', 'AddDemoDataOptions');
}
if ($this->errors !== false)
{
$APPLICATION->ThrowException(implode("<br>", $this->errors));
return false;
}
return true;
}
use Bitrix\Main\Loader;
Loader::registerAutoLoadClasses("partner.demosite", array(
'DemoData' => 'lib/DemoData.php',
));
Answer the question
In order to leave comments, you need to log in
Okay, let's go.
1) How did such a hell of a design come about? It is not readable, and it is not needed (see below)
2) The dirname(__FILE__).'/db/mysql/install.sql') file is always present in the module directory.
In my practice, I have not yet met helluva-smart-guys who would climb into someone else's module and delete files, so if it is in the module, then it is always there.
Would you like to check further? Check only if it is not found - at least display an error, otherwise now everything is idle - no file -> no event -> it's not clear why it doesn't work
3) Do you want to execute a batch? Well, do it like Bitrix. Look at least at the module of the trade catalog:
if(!$DB->Query("SELECT 'x' FROM b_catalog_group", true))
$errors = $DB->RunSQLBatch($_SERVER['DOCUMENT_ROOT']."/bitrix/modules/catalog/install/db/".strtolower($DB->type)."/install.sql");
namespace Partner\Demosite;
class DemoData
To begin with, I advise you to use the standard module builder:
marketplace.1c-bitrix.ru/solutions/bitrix.mpbuilder
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question