Objective : 

In this tutorial I am going to explain how you can perform insert, load, update, delete, search operation using Repository in Magento.

If you looking for how to create Repository in Magento, check my previous post

Complete Guide to Create Magento Repository: https://wp.me/p10lsw-4v

Explanation:

For better understanding and simplicity I will use the “Student Repository” that I have created in my previous tutorial.

So basically to perform Insert, load, update, delete, search we need to inject following class in your code

  1. Model Factory Class 
  2. Model Collection Factory Class
  3. Main Repository Class
  4. FilterBuilder class
  5. SearchCriteriaBuilder class

For “Student” entity the Constructor injection will be like this  

public function __construct(
...
...
\Magento4u\SampleRepository\Model\StudentFactory $student,
\Magento4u\SampleRepository\Model\ResourceModel\Student\CollectionFactory $studentCollection,
\Magento4u\SampleRepository\Model\StudentRepository $studentRepository,
\Magento\Framework\Api\FilterBuilder $filterBuilder,
\Magento\Framework\Api\Search\SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->pageFactory = $pageFactory;
$this->student = $student;
$this->studentCollection = $studentCollection;
$this->studentRepository = $studentRepository;
$this->filterBuilder = $filterBuilder;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
parent::__construct($context);
}

Insert Operation using Repository 

$student = $this->student->create();
$student->setName('Student 1');
$student->setRollNumber('435345431');
$this->studentRepository->save($student);

Load entity using Repository 

$student = $this->studentRepository->getById(1);
print_r($student->getData());

Update Operation using Repository 

$student = $this->studentRepository->getById(1);
$student->setName('Student New Name');
$student->setRollNumber('1111111111111');
$this->studentRepository->save($student);

Delete Operation using Repository 

$student = $this->studentRepository->getById(1);
$this->studentRepository->delete($student);

Search Operation using Repository 

$filter1 = $this->filterBuilder->setField('name')
->setConditionType('like')
->setValue('%Student%')
->create();
$this->searchCriteriaBuilder->addFilter($filter1);
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchResult = $this->studentRepository->getList($searchCriteria);
$items = $searchResult->getItems();
echo "Number of record== " . count($items);
echo "</br>";
foreach ($items as $item) {
echo "</br>";
echo "ID :" . $item->getId() . " ". "Name : ". $item->getName() . " Roll No : " . $item->getRollNumber();
echo "</br>";
}

Search Operation and Shorting using Repository

$filter1 = $this->filterBuilder->setField('name')
->setConditionType('like')
->setValue('%Student%')
->create();
$this->searchCriteriaBuilder->addFilter($filter1);
$this->searchCriteriaBuilder->addSortOrder('entity_id', SortOrder::SORT_DESC);
$this->searchCriteriaBuilder->setCurrentPage(1)->setPageSize(2);

$searchCriteria = $this->searchCriteriaBuilder->create();
$searchResult = $this->studentRepository->getList($searchCriteria);
$items = $searchResult->getItems();

echo "<pre>";
echo "Number of record == " . count($items);
echo "</br>";

foreach ($items as $item) {
echo "</br>";
echo "ID :" . $item->getId() . " " . "Name : " . $item->getName() . " Roll No : " . $item->getRollNumber();
echo "</br>";
}

Sample Module:

Sample module code can be downloaded from GitHub 

https://github.com/skar2019/SampleRepository

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Hello everyone, this is Suman Kar, I define myself as an E-commerce, AI, and Machine learning Enthusiastic

8X Adobe Commerce Certified, Certified Scrum Master, AWS Certified Cloud Practitioner, TOGAF Certified

15 Years of IT experience

Let’s connect