2

I want to load custom module collection in module template file :

I have my custom module which has table name:tagdata

I want to load "tagdata" table data in my template file.

And yeah I created a grid with UI component.

Please check my model file :

<?php

namespace [vendor]\[module]\Model;

class Extratags extends \Magento\Framework\Model\AbstractModel
{
    function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
    }

    protected function _construct()
    {
        $this->_init('[vendor]\[module]\Model\ResourceModel\Extratags');
    }
}
5
  • You want custom collection in grid or in template file? Commented Jan 11, 2017 at 9:13
  • in template file, Grid is successfully created Commented Jan 11, 2017 at 9:14
  • You can create model of that custom extension, right? And you have block file of that template file, right? Commented Jan 11, 2017 at 9:18
  • yeah you can check my model file in question i just edited Commented Jan 11, 2017 at 9:21
  • I posted answer, please check. Commented Jan 11, 2017 at 9:29

2 Answers 2

1

To get collection you need to do something like this,

=> Block File :

 use [vendor]\[module]\Model\ExtratagsFactory;
use Magento\Framework\Data\Collection;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

       class Blockclass extends Template
       {
               protected $_modelextratagsFactory;

            public function __construct(Context $context, 
                    ExtratagsFactory $MainmodelextratagsFactory, 
                    array $data = [])
                {
                    $this->_modelextratagsFactory = $MainmodelextratagsFactory;

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

           public function yourfunction()
           {
              $MyCollection = $this->__modelextratagsFactory->create()
                            ->getCollection();
               return $MyCollection;

           }
        }

This is your Block file of your phtml file, and in phtml file you need to call like this,

=> phtml file :

$collection = $block->yourfunction();
7
  • got this error : Fatal error: Cannot access parent:: when current class scope has no parent Commented Jan 11, 2017 at 9:42
  • I update answer, please check. Commented Jan 11, 2017 at 9:44
  • Above code working fine with my custom module. If above code help you please accept it solution, So it will help other in community. Commented Jan 11, 2017 at 9:57
  • after update block file frontend not loaded it's display only loading nothing result. :( Commented Jan 11, 2017 at 10:08
  • Check your exception file, I will give you code that working fine with me at many places. Commented Jan 11, 2017 at 10:10
1

You need to create three files for getting collection from custom module

1) Model, 2) ResourceModel, 3) Collection

Namespace: Prince

Module: Hello

Table: hello

1) Create Hello.php Model file at

app/code/Prince/Helloworld/Model/Hello.php

<?php

namespace Prince\Helloworld\Model;

use Magento\Framework\Model\AbstractModel;

class Hello extends AbstractModel
{
    /**
     * Define resource model
     */
    protected function _construct()
    {
        $this->_init('Prince\Helloworld\Model\ResourceModel\Hello');
    }
}

2) Now create Hello.php ResourceModel at

app/code/Prince/Helloworld/Model/ResourceModel/Hello.php

<?php

namespace Prince\Helloworld\Model\ResourceModel;

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

class Hello extends AbstractDb
{
    /**
     * Define main table
     */
    protected function _construct()
    {
        $this->_init('hello', 'id'); //hello is table of module
    }
}

3) Now create Collection.php Collection file at

app/code/Prince/Helloworld/Model/ResourceModel/Hello/Collection.php

<?php

namespace Prince\Helloworld\Model\ResourceModel\Hello;

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

class Collection extends AbstractCollection
{
    /**
     * Define model & resource model
     */
    protected function _construct()
    {
        $this->_init(
            'Prince\Helloworld\Model\Hello',
            'Prince\Helloworld\Model\ResourceModel\Hello'
        );
    }
}

Now you can test your collection in Index.php controller app/code/Prince/Helloworld/Controller/Index/Index.php

<?php

namespace Prince\Helloworld\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Prince\Helloworld\Model\HelloFactory;

class Index extends Action
{
    protected $_modelHelloFactory;

    public function __construct(
        Context $context, 
        HelloFactory $modelHelloFactory
    ) {
        parent::__construct($context);
        $this->_modelHelloFactory = $modelHelloFactory;
    }

    public function execute()
    {

        $resultPage = $this->_modelHelloFactory->create();
        $collection = $resultPage->getCollection(); //Get Collection of module data
        var_dump($collection->getData());
        exit;

    }
}

If you want to get collection in template using block file

Another four files you need to add nad create

1. Block File

2. Layout File

3. Controller File

4. Templarte File

Create Block file Hello.php

namespace vendorName\ModuleName\Block\Index;

use Magento\Framework\App\RequestInterface;
use vendorName\ModuleName\Model\Hello;

class Hello extends \Magento\Framework\View\Element\Template 
{
    protected $helloModelFactory;
    protected $request;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        Hello $helloModelFactory,
        RequestInterface $request,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_helloModelFactory = $helloModelFactory;
        $this->_request = $request;
    }

    /**
     * Preparing global layout
     *
     * @return $this
     */
    protected function _prepareLayout() {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Display Collection'));
        return $this;
    }

    public function getHello()
    {
        $collection = $this->_helloModelFactory->getCollection();
        return $collection;
    }
}

Or Create Layout file routName_controllername_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
             <block class="vendorName\ModuleName\Block\Event\Index" name="newevent" template="vendorName_ModuleName::Index.phtml" />
        </referenceContainer>
    </body>
</page>

Create Controller File Index.php

<?php
namespace vendorName\ModuleName\Controller\Index;

use Magento\Framework\App\Action;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action 
{
    /**
     * @param Action\Context $context
     */

    protected $resultPageFactory;

    public function __construct(
        Action\Context $context,
        PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute() {

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

Create Template file Index.phtml

<?php

var_dump($block->getgetHello()->getData());

I hope it's work for ever

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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