L
L
lie22020-05-15 10:46:43
1C-Bitrix
lie2, 2020-05-15 10:46:43

How to get comments from CBlogComment and link them to CBlogPost?

Good day everyone.
I am using code:

<?CModule::IncludeModule("blog"); 

// выберем все комментарии к сообщению с идентификатором 1
$arOrder = Array(
        "ID" => "desc",
        "DATE_CREATE" => "desc"
    );
$arFilter = Array(

    );
$arSelectedFields = Array("ID", "BLOG_ID", "POST_ID", "PARENT_ID", "AUTHOR_ID", "AUTHOR_NAME", "AUTHOR_EMAIL", "AUTHOR_IP", "AUTHOR_IP1", "TITLE", "POST_TEXT", "DATE_CREATE","USER_NAME");

$dbComment = CBlogComment::GetList($arOrder, $arFilter, false, false, $arSelectedFields);
while ($arComment = $dbComment->Fetch())
{
?>
<div><div>
  <strong style="color: slategray;"><?=$arComment["AUTHOR_NAME"]?></strong><strong><?=$arComment["USER_NAME"]?></strong>
  <p style="    background-color: #fff4ca;    padding-top: 2px;    padding-bottom: 3px;
    padding-left: 4px;    padding-right: 4px;    color: #767676;    display: inline;">
<?=$arComment["DATE_CREATE"]?></p> товару ///<?=$arPost["TITLE"]?>///</div>

<br>
<?=$arComment["POST_TEXT"]?><br>
<?=$arComment["POST_ID"]?>
</div>
<hr>
<br>
<br>
<?
}
?>

Everything works, but there is the following difficulty: you need to display the name of the product and a link to it.
The necessary info is stored in CBlogPost: TITLE and URL.
I also know that the ID field in CBlogPost = POST_ID in CBlogComment.

How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PetrPo, 2020-05-16
@lie2

Something like that

\Bitrix\Main\Loader::includeModule('iblock');
\Bitrix\Main\Loader::includeModule('blog');

$iblockId = 2;
$arPostsId = $arElements = $arPosts = [];

$dbRes = \Bitrix\Iblock\PropertyTable::getList([
    'select' => ['ID', 'CODE'],
    'filter' => [
        '=IBLOCK_ID' => $iblockId,
        '=PROPERTY_TYPE' => \Bitrix\Iblock\PropertyTable::TYPE_NUMBER,
        '=MULTIPLE' => 'N',
        '=CODE' => [CIBlockPropertyTools::CODE_BLOG_POST]
    ]
]);

if($arRes = $dbRes->Fetch()) {
    $propertyPostId = $arRes['ID'];
}

$dbRes = CIBlockElement::GetList(
    [],
    ['=IBLOCK_ID' => $iblockId, '!PROPERTY_'.$propertyPostId => false],
    false,
    false,
    ['ID', 'NAME', 'DETAIL_PAGE_URL', 'PROPERTY_'.$propertyPostId]
);

while($arRes = $dbRes->GetNext()) {
    $propertyPostValue = $arRes['PROPERTY_'.$propertyPostId.'_VALUE'];

    $arPostsId[] = $propertyPostValue;
    $arElements[$propertyPostValue] = $arRes;
}

$arPostsId = array_unique($arPostsId);

$dbRes = CBlogPost::GetList(
    ['ID' => 'DESC'],
    ['ID' => $arPostsId]
);

while($arRes = $dbRes->Fetch()) {
    $arPosts[$arRes['ID']] = $arRes;
}

$dbRes = CBlogComment::GetList(
    ['ID' => 'DESC'],
    ['POST_ID' => $arPostsId]
);

while($arRes = $dbRes->Fetch()) {
    $arPosts[$arRes['POST_ID']]['COMMENTS'][] = $arRes;
    $arPosts[$arRes['POST_ID']]['ELEMENT'] = $arElements[$arRes['POST_ID']];
}

print_r($arPosts);

UPD added CBlogPost::GetList

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question