Views: 262
Last Modified: 21.03.2022

  Global data area

When required, a single table can be described by several entities, by splitting records into segments:

    class Element4Table extends \Bitrix\Iblock\ElementTable
    {
        public static function getTableName()
        {
            return 'b_iblock_element';
        }
        
        public static function setDefaultScope(Query $query)
        {
            $query->where("IBLOCK_ID", 4);
        }
    }
    
    class Element5Table extends \Bitrix\Iblock\ElementTable
    {
        public static function getTableName()
        {
            return 'b_iblock_element';
        }
        
        public static function setDefaultScope(Query $query)
        {
            $query->where("IBLOCK_ID", 5);
        }
    }

Method setDefaultScope will be executed on each query, skipping the query object. It can set both filter and any other query parameters.

  Local data area

Starting from version 20.5.500 you can pre-set data via methods with*. This is similar to setDefaultScope, but not at the global level, but at the user's level: call when necessary. After describing the method in the entity you can call it in the query constructor:

    class UserTable
    {
        public static function withActive(Query $query)
        {
            $query->where('ACTIVE', true);
        }
    }
    
    $activeUsers = UserTable::query()
       ->withActive()
       ->fetchCollection();

    // WHERE `ACTIVE`='Y'

The object Bitrix\Main\ORM\Query\Query is used as an argument: you can set filter and any other query parameters. Additionally, method can be supplemented by your arguments, also passed when calling from query constructor:

    class UserTable
    {
        public static function withActive(Query $query, $value)
        {
            $query
                ->addSelect('LOGIN')
                ->where('ACTIVE', $value);
        }
    }
    
    $activeUsers = UserTable::query()
        ->withActive(false)
        ->fetchCollection();
        
    // SELECT `LOGIN` ... WHERE `ACTIVE`='N




Courses developed by Bitrix24