Last active
September 13, 2021 17:18
-
-
Save anoriar/7624de409566da5372678396e9cd3ad2 to your computer and use it in GitHub Desktop.
orm bitrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//количество всех элементов | |
$res = \Bitrix\Iblock\ElementTable::getList([ | |
'runtime' => [ | |
new Entity\ExpressionField('CNT', 'COUNT(*)') | |
], | |
'select' => ['CNT'], | |
'filter' => ["IBLOCK_ID" => 1, "ACTIVE" => "Y"], | |
'limit' => 10 | |
]); | |
while ($row = $res->fetch()) | |
{ | |
$rows[] = $row; | |
} | |
//сколько в каждом разделе находится элементов | |
$res = \Bitrix\Iblock\ElementTable::getList([ | |
'runtime' => [ | |
new Entity\ExpressionField('CNT', 'COUNT(*)') | |
], | |
'select' => ['IBLOCK_SECTION_ID', 'CNT'], | |
'filter' => ["IBLOCK_ID" => 1, "ACTIVE" => "Y"], | |
'group' => ['IBLOCK_SECTION_ID'], | |
'limit' => 10 | |
]); | |
while ($row = $res->fetch()) | |
{ | |
$rows[] = $row; | |
} | |
//вывод имени элемента и раздела к которому он принадлежит через объект | |
$query = new \Bitrix\Main\Entity\Query(Bitrix\Iblock\ElementTable::getEntity()); | |
$query | |
// поле section как ссылка на таблицу b_iblock_section | |
->registerRuntimeField("section", array( | |
// тип - сущность SectionTable | |
"data_type" => "Bitrix\Iblock\SectionTable", | |
// обратите внимание, что this.ID относится к таблице, относительно которой строится запрос | |
// т.е. b_iblock_element.IBLOCK_SECTION_ID = b_iblock_section.ID | |
'reference' => array('=this.IBLOCK_SECTION_ID' => 'ref.ID'), | |
) | |
) | |
->setSelect(array("ID", "NAME", 'SECTION_NAME' => 'section.NAME', 'SECTION_ID' => 'section.ID')) | |
->setFilter(array("IBLOCK_ID" => 1, "ACTIVE" => "Y")); | |
$res = $query->exec(); | |
while ($row = $res->fetch()) | |
{ | |
$rows[] = $row; | |
} | |
//вывод имени элемента и раздела к которому он принадлежит | |
$res = \Bitrix\Iblock\ElementTable::getList([ | |
'runtime' => [ | |
new Entity\ReferenceField('section', 'Bitrix\Iblock\SectionTable', ['=this.IBLOCK_SECTION_ID' => 'ref.ID'], | |
['join_type' => 'LEFT']) | |
], | |
'select' => ["ID", "NAME", 'SECTION_NAME' => 'section.NAME', 'SECTION_ID' => 'section.ID'], | |
'filter' => ["IBLOCK_ID" => 1, "ACTIVE" => "Y"], | |
'limit' => 10 | |
]); | |
while ($row = $res->fetch()) | |
{ | |
$rows[] = $row; | |
} | |
// группировка элементов по разделу | |
$query = new Entity\Query(\Bitrix\Iblock\ElementTable::getEntity()); | |
$query | |
->registerRuntimeField("cnt", array( | |
"data_type" => "integer", | |
"expression" => array("count(%s)", "IBLOCK_SECTION_ID") | |
) | |
) | |
->setSelect(["IBLOCK_SECTION_ID", "cnt"]) | |
->setFilter(["IBLOCK_ID" => 2]) | |
->setGroup(["IBLOCK_SECTION_ID"]); | |
Query(IblockTable) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment