CRUD operation Magento 2

In this article, i will help you for get model collection in Magento 2 and perform CRUD operation on it. CRUD means Create, Read, Update and Delete operations.

CRUD is basic operations while we develop any module in Magento 2. Magento 2 gives some inbuilt methods for manage this CRUD operations. 

First We need to create model, resource model and collection files in our Magento 2 custom module and then we can create object using dependency injection for fetch collection from the model.

After model created in our module,  create Factory object of our model in block and using this object , we can perform select, insert, update and delete query easily in template or view file.


1. Select Data using model

In select, we have create object of Model Factory and call getCollection() method for get all data.

The code is given below.


 Block File

       

 <?php

    namespace Mageniks\Alltest\Block;


    class Alltest extends \Magento\Framework\View\Element\Template

    {

        protected $_moduleCollectionFactory;

        public function __construct(

            \Magento\Framework\View\Element\Template\Context $context,

            \Mageniks\Alltest\Model\AlltestFactory $moduleCollectionFactory,

            array $data = []

        ) {

            

            $this->_moduleCollectionFactory   = $moduleCollectionFactory;

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

           

        }

        

        public function getModuleCollection() 

        {

            $productCollection = $this->_moduleCollectionFactory->create()->getCollection();

            return $productCollection;

        }

    }

?>

we have create one function getModuleCollection() in block and this function can be called from template using $block object. 


View file.

       

<?php

 $collection = $block->getModuleCollection();


    foreach($collection as $item)

    {

        echo $item->getEmpname();

        echo $item->getEmpemail();

    }


 Now You can get all the table data.


 Explain code :

       

  $productCollection = $this->_moduleCollectionFactory->create()->getCollection();



  This is main code for select all collection of model. please first create Factory object and then call create() method for getting model object.


 2. Insert Data using model

 We have create one insert function setInsertVal()  in block.

 The code is given below.


 Block File

       

 <?php

namespace Mageniks\Alltest\Block;


class Alltest extends \Magento\Framework\View\Element\Template

{

    protected $_moduleCollectionFactory;

    public function __construct(

        \Magento\Framework\View\Element\Template\Context $context,

        \Mageniks\Alltest\Model\AlltestFactory $moduleCollectionFactory,

        array $data = []

    ) {

        

        $this->_moduleCollectionFactory   = $moduleCollectionFactory;

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

       

    }

    public function setInsertVal() 

    {

        $productCollection = $this->_moduleCollectionFactory->create();

        $productCollection->setEmpname('Niks');

        $productCollection->setEmpemail('Niks@gmail.com');

        $productCollection->save();

        return "Insert record successfully..!!";

    }

}

?>


we have call this block function in template same way as select data function.


View File

       

<?php


 $block->setInsertVal();

 Explain code :

    

       

    $productCollection = $this->_moduleCollectionFactory->create();

    $productCollection->setEmpname('Niks');

    $productCollection->setEmpemail('Niks@gmail.com');

    $productCollection->save();

    

-> This is a main code for Insert data into table.

-> As we know now, using FACTORY OBJECT we can get object of model and then use set methods for insert data into table.

   

 3. Update or Modify Data using model

  We have created one update function updateVal()  in block.

  The code is given below.


  Block File

       

<?php

namespace Mageniks\Alltest\Block;


class Alltest extends \Magento\Framework\View\Element\Template

{

    protected $_moduleCollectionFactory;

    public function __construct(

        \Magento\Framework\View\Element\Template\Context $context,

        \Mageniks\Alltest\Model\AlltestFactory $moduleCollectionFactory,

        array $data = []

    ) {

        

        $this->_moduleCollectionFactory   = $moduleCollectionFactory;

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

       

    }

    public function updateVal() 

    {

        $productCollection = $this->_moduleCollectionFactory->create()->load(4);

        $productCollection->setEmpname('bbb');

        $productCollection->setEmpemail('bbb@gmail.com');        

        $productCollection->save();     

    }

}

    

?>


View File

       

<?php

 $block->updateVal();


    Explain code below:

       

    $productCollection = $this->_moduleCollectionFactory->create()->load(4);

    $productCollection->setEmpname('bbb');

    $productCollection->setEmpemail('bbb@gmail.com');

    $productCollection->save();

       

    -> which we want to update the record, that record need to load using the load() method 

    -> After load the record, we will get object of this record.

    -> And then we can easily edit record using set methods.

4. Delete Data using model

  We have create one delete function deleteVal() in block.

  The code is given below.

  

  Block File

       

<?php

namespace Mageniks\Alltest\Block;


class Alltest extends \Magento\Framework\View\Element\Template

{

    protected $_moduleCollectionFactory;

    public function __construct(

        \Magento\Framework\View\Element\Template\Context $context,

        \Mageniks\Alltest\Model\AlltestFactory $moduleCollectionFactory,

        array $data = []

    ) {

        

        $this->_moduleCollectionFactory   = $moduleCollectionFactory;

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

       

    }

    public function deleteVal() 

    {

        $productCollection = $this->_moduleCollectionFactory->create()->load(5);

        $productCollection->delete();

    

    }

}

    

?>


View File

       

<?php

$block->deleteVal();


    Explain code below:   

       

    $productCollection = $this->_moduleCollectionFactory->create()->load(5);

    $productCollection->delete();

           

    -> which we want to delete the record, that record need to load using the load() method 

    -> After load the record , we will get object of this record.

    -> And then we can easily delete specific record using delete method.

we have call all these block function from template file using $block object.