Views: 3434
Last Modified: 30.03.2022

ORM is also suitable for such exotic queries for data retrieval targeting not the table, but stored procedures. Such procedures can be created in MSSQL-database.

Indicate the function name in the method getTableName:

public static function getTableName()
{
        // return "foo_table_name"
        return "foo_table_procedure()";
}

This code won't work as demonstrated. The reason: when using the connection Bitrix\Main\DB\MssqlConnection all table name entries are pre-screened. An attempt to directly execute such query will cause a thrown exception:

MS Sql query error: Invalid object name 'foo_table_procedure()'. (400)

SELECT

[base].[bar] AS [BAR],

[base].[baz] AS [BAZ],

FROM [foo_table_procedure()] [base]

Getting a required result is prevented only by characters [ and ], used by MssqlSqlHelper to protect the "table" name. You can resolve this issue by creating a custom Connection and SqlHelper.

Alternate solution: install a server mssql extension and implement the following architecture:

class MssqlSqlHelper extends Bitrix\Main\DB\SqlHelper
{
    public function quote($identifier)
    {
        if (self::isKnowFunctionalCall($identifier))
        {
            return $identifier
        }
        else
        {
            return parent::quote($identifier);
        }
    }
}

Where self::isKnownFunctionCall is a verification method, returning true, if $identifier is located in “foo_table_procedure()”.




Courses developed by Bitrix24