Pregunta

/Block/Reviews.php

<?php
namespace Training\Reviews\Block;
use Training\Reviews\Model\ResourceModel\Reviews\Collection;

use Magento\Framework\View\Element\Template;

class Reviews extends \Magento\Framework\View\Element\Template
{

public function _prepareLayout()
{
    $this->pageConfig->getTitle()->set(__('Users Reviews'));

    return parent::_prepareLayout();
}

protected $_productCollectionFactory;
protected $_productVisibility;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Training\Reviews\Model\ResourceModel\Reviews\CollectionFactory 
$productCollectionFactory,
    array $data = []
) {
    $this->_productCollectionFactory = $productCollectionFactory;

    parent::__construct($context, $data);
}

public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addFieldToFilter('result', ['like' => '%likes%']);
    $collection->count();
    return $collection;
 }
}

/view/frontend/templates/reviews.php

<?php
$productCollection = $this->getProductCollection();
foreach ($productCollection as $product) {
print_r($product->getData());
echo "<br>";
}
?>

This function outputs rows and information about them, I only need the number of rows.

I would appreciate your help!

¿Fue útil?

Solución

Use count() function.

$c = count($productCollection);
echo "totle count is ".$c;

Otros consejos

Use $productCollection->getSize() if you don't want to load the collection. It's for a better performance. Take a look at class \Magento\Framework\Data\Collection\AbstractDb::getSize() to see more details.

Difference between getSize() and count() on collection

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top