Custom module collection array not return
-
18-03-2021 - |
문제
I have created a custom module and I have return collection array but it does not return foreach array in Magento 2.3.2. it returns the only foreach last value.
Block Code
<?php
/**
* @copyright Devidas
*
* @see PROJECT_LICENSE.txt
*/
namespace Developer\Infographics\Block\Index;
use Magento\Framework\View\Element\Template;
use Magento\Catalog\Model\CategoryFactory;
class Leftinfographics extends Template {
protected $_resource;
protected $session;
/**
* @param \Magento\Framework\Registry $registry
*/
private $_registry;
/**
* @param \Magento\Framework\Registry $registry
*/
protected $_catalogSession;
/**
* @param \Magento\Catalog\Model\CategoryFactory $categoryfactory
*/
private $_categoryFactory;
/**
* @param Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
*/
protected $_productCollectionFactory;
/**
*@param Magento\Catalog\Model\Category $catgory
**/
protected $_category;
/**
* Constructor
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Catalog\Model\Session $catalogSession
**/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Developer\Infographics\Model\InfographicsFactory $collectionFactory,
\Magento\Framework\View\Page\Config $pageConfig,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Framework\Registry $registry,
\Magento\Catalog\Model\Session $catalogSession,
\Magento\Framework\Session\SessionManagerInterface $session,
\Magento\Catalog\Model\Category $catgory,
CategoryFactory $categoryfactory,
\Magento\Framework\App\ResourceConnection $resource,
array $data = [])
{
parent::__construct($context, $data);
$this->pageConfig = $pageConfig;
$this->collectionFactory = $collectionFactory;
$this->_registry = $registry;
$this->_catalogSession = $catalogSession;
$this->session = $session;
$this->_categoryFactory = $categoryfactory;
$this->_productCollectionFactory = $productCollectionFactory;
$this->_category = $catgory;
$this->_resource = $resource;
}
public function getCategoryViceversaCollection()
{
$collection = array();
$finalsearch = 'test,magento,abc,xyz'
$commaList = explode(',', $finalsearch);
foreach ($commaList as $key => $value) {
$collection = $this->collectionFactory->create()->getCollection()->addFieldToFilter('tags', array('like' => '%'.$value.'%'));
}
return $collection;
}
}
Template Code
$productxyzdata = $block->getXYZ();
var_dump($productCollectiondata->getData());
올바른 솔루션이 없습니다
다른 팁
this is because you are creating a new collection with every iteration, thus only having the last one
$collection = array();
$finalsearch = 'test,magento,abc,xyz'
$commaList = explode(',', $finalsearch);
$collection = $this->collectionFactory->create();
foreach ($commaList as $key => $value) {
$collection->addFieldToFilter('tags', array('like' => '%'.$value.'%'));
}
return $collection;
Try using this way
$collection = [];
$finalsearch = 'test,magento,abc,xyz';
$commaList = explode(',', $finalsearch);
foreach ($commaList as $key => $value) {
$collection[] = $this->collectionFactory->create()->getCollection()->addFieldToFilter('tags', array('like' => '%'.$value.'%'));
}
return $collection;
Try this below code :
$collection = [];
$finalsearch = 'test,magento,abc,xyz';
$commaList = explode(',', $finalsearch);
foreach ($commaList as $key => $value) {
$collection[] = $this->collectionFactory->create()->addFieldToFilter('tags', ['like' => '%' . $value . '%'])->getData();
}
return $collection;
make sure your collection class should be Developer\Infographics\Model\ResourceModel\Infographics\CollectionFactory
제휴하지 않습니다 magento.stackexchange