A
A
Anton Shelestov2017-10-11 15:38:52
1C-Bitrix
Anton Shelestov, 2017-10-11 15:38:52

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;
    }

in the lib folder there is a DemoData.php file with the DemoData class and the AddDemoDataOptions function.
in include.php
use Bitrix\Main\Loader;

Loader::registerAutoLoadClasses("partner.demosite", array(
    'DemoData' => 'lib/DemoData.php',
));

event OnPageStart indicated for the test and by the way which one is correct to use in this case?
but nothing happens, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Nikolaev, 2017-10-11
@gromdron

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");

Pay attention to: strtolower($DB->type), if you plan to make a module, leave at least a loophole so as not to redo it later.
4) Go to d7 already!
In the module folder in include.php leave an empty comment with php tags
And in /lib/demodata.php write:
namespace Partner\Demosite;

class DemoData

Then you will not need to edit the autoloader when a new class appears, and there will be less hemorrhoids
5) After step 4, look at the new loader (and since you write in the old fashioned way, here is the old code, you can look at the new one in the same module of the sales catalog):
PS I hope you delete event subscriptions in uninstall.
And now the answer to the question: if you want to load some data into the database, do it in the DoInstall () method at the very end after the module has been successfully registered, and not on events. If there are too many of them, select a few steps.

A
Alexey Emelyanov, 2017-10-11
@babarun

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 question

Ask a Question

731 491 924 answers to any question