1

My custom collection is not working in block file Helloworld.php

<?php
namespace New\MyModule\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;

class Helloworld extends Template
{
    protected $_modelFactory;

    public function __construct(Context $context,
                                New\MyModule\Model\DataFactory $modelFactory,
                                array $data = array())
    {
        $this->_modelFactory = $modelFactory;
        parent::__construct($context, $data);
    }


    public function getHelloCollection()
        {
            return $this->_modelFactory->create()->getCollection();
        }
}

Model files:

Model\Data.php

<?php


namespace New\Model;

use Magento\Framework\Model\AbstractModel;

    class Data extends AbstractModel
    {   
        protected function _construct()
        {
            $this->_init('New\MyModule\Model\ResourceModel\Data');
        }
    }

Resource model:

Model\ResourceModel\Data.php

<?php

namespace New\MyModel\Model\ResourceModel;


use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Data extends AbstractDb

{

    protected function _construct()

    {

        $this->_init('blog_posts', 'post_id');

    }

}

Collection file:

New\MyModule\model\ResourceModel\Data\Collection.php

<?php


namespace New\MyModule\Model\ResourceModel\Data;


use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;


class Collection extends AbstractCollection

{

    protected function _construct()
    {

        $this->_init(
        'New\MyModule\Model\Data',
        'New\MyModule\Model\ResourceModel\Data');

    }

}

I need to get the collection into my block to fetch the database values. pls help me.

2

i tested this code and find out only one error

Change

public function __construct(Context $context,
                                New\MyModule\Model\DataFactory $modelFactory,
                                array $data = array())
    {
        $this->_modelFactory = $modelFactory;
        parent::__construct($context, $data);
    }

to

public function __construct(Context $context,
                                \New\MyModule\Model\DataFactory $modelFactory,
                                array $data = array())
    {
        $this->_modelFactory = $modelFactory;
        parent::__construct($context, $data);
    }

Below is my code

QaisarSatti\HelloWorld\Model\Helloworld.php

<?php


     namespace QaisarSatti\HelloWorld\Model;



    class Helloworld extends \Magento\Framework\Model\AbstractModel  
    {   
        protected function _construct()
        {
            $this->_init('QaisarSatti\HelloWorld\Model\ResourceModel\Helloworld');
        }
    }

QaisarSatti\HelloWorld\Model\ResourceModel\Helloworld.php

<?php

namespace QaisarSatti\HelloWorld\Model\ResourceModel;


use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Helloworld extends AbstractDb

{

    protected function _construct()

    {

        $this->_init('blog_posts', 'post_id');

    }

}

QaisarSatti\HelloWorld\Model\ResourceModel\Helloworld\Collection.php

<?php


namespace QaisarSatti\HelloWorld\Model\ResourceModel\Helloworld;


use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;


class Collection extends AbstractCollection

{

    protected function _construct()
    {

        $this->_init(
        'QaisarSatti\HelloWorld\Model\Helloworld',
        'QaisarSatti\HelloWorld\Model\ResourceModel\Helloworld');

    }

}

BLock file

QaisarSatti\HelloWorld\Block\HelloWorld.php

<?php 


namespace QaisarSatti\HelloWorld\Block;

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

    protected $_helloworldFactory;
   public function __construct(
       \Magento\Backend\Block\Template\Context $context,
      \QaisarSatti\HelloWorld\Model\HelloworldFactory $helloworldFactory,      
       array $data = []
   ) {


       $this->_helloworldFactory = $helloworldFactory;

       parent::__construct($context, $data);
   }
   public function getHelloCollection()
        {
            return $this->_helloworldFactory->create()->getCollection();
        }

}

Source link

  • thanks, Everything i did the same.. but still i can't able to get the collection in my block... Can i see ur block file.. it will be helpful. – Selva Kumar Nov 27 '17 at 10:43
  • @SelvaKumar add the block file too and don't forgot to delete var/generation folder – Qaisar Satti Nov 27 '17 at 10:45
  • I am following your steps. Now I am the getcollection method as deprecated with strikeout line. – Selva Kumar Nov 27 '17 at 11:00
  • @SelvaKumar which magento version? – Qaisar Satti Nov 27 '17 at 11:03
  • magento version 2.2 – Selva Kumar Nov 27 '17 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.