Skip to main content
1 of 2

Using repository in admin form magento 2

I using admin form from this example

But i want change modelFactory from model to repository

But i don`t know how do it.

I need to change this files from model to repository

<?php

namespace Jeff\Contacts\Controller\Adminhtml\Index;

use Exception; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Magento\Framework\View\Result\PageFactory; use Jeff\Contacts\Model\ContactFactory;

class Delete extends Action {

const ADMIN_RESOURCE = 'Index';

protected $resultPageFactory;
protected $contactFactory;

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

public function execute()
{
    $id = $this->getRequest()->getParam('id');

    $contact = $this->contactFactory->create()->load($id);

    if(!$contact)
    {
        $this->messageManager->addError(__('Unable to process. please, try again.'));
        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect->setPath('*/*/', array('_current' => true));
    }

    try{
        $contact->delete();
        $this->messageManager->addSuccess(__('Your contact has been deleted !'));
    }
    catch(\Exception $e)
    {
        $this->messageManager->addError(__('Error while trying to delete contact'));
        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect->setPath('*/*/index', array('_current' => true));
    }

    $resultRedirect = $this->resultRedirectFactory->create();
    return $resultRedirect->setPath('*/*/index', array('_current' => true));
}

}

And this file:

<?php

namespace Jeff\Contacts\Controller\Adminhtml\Index; use Exception; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Magento\Framework\View\Result\PageFactory; use Jeff\Contacts\Model\ContactFactory;

class Save extends Action {

const ADMIN_RESOURCE = 'Index';

protected $resultPageFactory;
protected $contactFactory;

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

public function execute()
{
    $resultRedirect = $this->resultRedirectFactory->create();
    $data = $this->getRequest()->getPostValue();

    if($data)
    {
        try{
            $id = $data['jeff_contacts_contact_id'];

            $contact = $this->contactFactory->create()->load($id);

            $data = array_filter($data, function($value) {return $value !== ''; });

            $contact->setData($data);
            $contact->save();
            $this->messageManager->addSuccess(__('Successfully saved the item.'));
            $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false);
            return $resultRedirect->setPath('*/*/');
        }
        catch(Exception $e)
        {
            $this->messageManager->addError($e->getMessage());
            $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData($data);
            return $resultRedirect->setPath('*/*/edit', ['id' => $contact->getId()]);
        }
    }

    return $resultRedirect->setPath('*/*/');
}

}