Y
Y
Yuriy2019-12-09 14:37:58
1C-Bitrix
Yuriy, 2019-12-09 14:37:58

Bitrix is ​​it possible to use the Bitrix API for the second connection to the database?

There is a second connection to the database specified in settings.php
I call the connection

$connection = Bitrix\Main\Application::getConnection('testdb');
  $sqlHelper = $connection->getSqlHelper();

in the second database of the same Bitrix, is it possible to execute CIBlockElement::GetList to a third-party database and how to do it?
or only SQL directly?
$sql = "SELECT ID, XML_ID FROM b_iblock_element WHERE IBLOCK_ID='17' AND ID = '12135'";

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Prokhorov, 2019-12-09
@pro6ka

Yes, you can. Absolutely nothing gets in the way.

$obElements = CIBlockElement::GetList(
    // ...
);
while ($element = $obElements->Fetch()) {
    // code here
}

S
serginhold, 2019-12-09
@serginhold

Is it possible to execute CIBlockElement::GetList to a third-party database and how to do it?

it’s unlikely that something in this craft has changed in a year, in my bookmarks I have a global $DB substitution, otherwise it doesn’t seem to crawl there, but if I changed it in the code, then when working with the default database, I will have to change it back
const CONN_DEFAULT = '';

private static $poolDB = [];

final public static function changeGlobalDB(string $name = '')
{
        global $DB;

        if (empty(self::$poolDB)) {
            self::$poolDB[self::CONN_DEFAULT] = $DB;
        }

        if (!isset(self::$poolDB[$name])) {
            $conn = Application::getConnection($name)->getConfiguration();
            self::$poolDB[$name] = new \CDatabase();
            self::$poolDB[$name]->Connect($conn['host'], $conn['database'], $conn['login'], $conn['password'], $name);
        }

        return $DB = self::$poolDB[$name];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question