0

I want to create a custom cart item collection model.

I want to get already added cart items collection from the database.

0

You need 3 files:

  • Model

  • Resource Model

  • Resource Model Collection

Assuming your module name is: Amir/Prince => app/code/Amir/Prince

So :

1) Model : name : Go => app/code/Amir/Prince/Model/Go.php

<?php
namespace Amir\Prince\Model;
class Go extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{
    const CACHE_TAG = 'amir_prince_post';
    protected $_cacheTag = 'amir_prince_go';
    protected $_eventPrefix = 'amir_prince_go';

    protected function _construct()
    {
        $this->_init('Amir\Prince\Model\ResourceModel\Go');
    }

    public function getIdentities()
    {
        return [self::CACHE_TAG . '_' . $this->getId()];
    }

    public function getDefaultValues()
    {
        $values = [];

        return $values;
    }
}

2) Resource Model : name : Go => app/code/Amir/Prince/Model/ResourceModel/Post.php

<?php
namespace Amir\Prince\Model\ResourceModel;


class Go extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context
    )
    {
        parent::__construct($context);
    }

    protected function _construct()
    {
        $this->_init('amir_prince_go', 'go_id'); //go_id : Primary key of your database table
    }

}

3) Resource Model Collection : name : Go/Collection => app/code/Amir/Prince/Model/ResourceModel/Go/Collection.php

<?php
namespace Amir\Prince\Model\ResourceModel\Go;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    protected $_idFieldName = 'go_id';
    protected $_eventPrefix = 'amir_prince_go_collection';
    protected $_eventObject = 'go_collection';

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Amir\Prince\Model\Go', 'Amir\Prince\Model\ResourceModel\Go');
    }

}

Now to call your collection, you need to call this class Amir\Prince\Model\GoFactory where you want to use it, GoFactory => model class name + Factory word.

Exemple in your controller :

app/code/Amir/Prince/Controller/Index/Index.php

 

<?php
namespace Amir\Prince\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    protected $_goFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Amir\Prince\Model\GoFactory $goFactory // This One
        )
    {
        $this->_pageFactory = $pageFactory;
        $this->_goFactory = $goFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        $post = $this->_goFactory->create();
        $collection = $post->getCollection();
        foreach($collection as $col){
            var_sump($col->getData());
        }
        exit();
        return $this->_pageFactory->create();
    }
}
  • I will check and update you soon. – Masud Shaikh Nov 16 '18 at 10:56

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.