Magento 2 Object Repository Design Pattern – Example
- Define object data api
- define public repository methods
- Create your repository
- Example loading and saving
Define the methods you want to make public via your Data API interface, your model will be implimenting this
/** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace CompanyExtensionApiData; interface ObjectInterface { /**#@+ * Constants for keys of data array. Identical to the name of the getter in snake case */ const OBJECT_ID = 'obj_id'; const IDENTIFIER = 'obj_id'; const CONTENT = 'obj_content'; const IS_ACTIVE = 'is_active'; /**#@-*/ /** * Get ID * * @return int|null */ public function getId(); /** * Get identifier * * @return string */ public function getIdentifier(); public function getContent(); public function setContent(): } // end class
Define the methods you want to make public for your Object Repository
namespace ComapnyExtensionApi; /** * Object CRUD interface. * @api * @since 100.0.2 */ interface ObjectRepositoryInterface { /** * Save Object. * * @param MagentoCmsApiDataObjectInterface $object */ public function save(DataObjectInterface $object); /** * Retrieve Object. * * @param int $objectId * @return NamespaceExtensionApiDataObjectInterface */ public function getById($objectId); /** * Delete Object. * * @param NamespaceExtensionApiDataObjectInterface $object * @return bool true on success */ public function delete(DataObjectInterface $object); } // end class
Create your Object Repository class
For reference CompanyExtensionModelResourceModelObject extends MagentoFrameworkModelResourceModelDbAbstractDb
namespace CompanyExtensionModel; use CompanyExtensionApiDataObjectInterface; use CompanyExtensionApiObjectRepositoryInterface; use CompanyExtensionModelObjectFactory; class ObjectRepository implements ObjectRepositoryInterface { /** * @var CompanyExtensionModelObjectFactory */ protected $_objectFactory; /** * @var CompanyExtensionModelResourceModelObject */ protected $_resourceModel; /** * Object constructor. * @param CompanyExtensionModelObjectFactory $objectFactory * @param CompanyExtensionModelResourceModelObject $objectResource */ public function __construct( CompanyExtensionModelObjectFactory $objectFactory, CompanyExtensionModelResourceModelObject $objectResource ) { $this->_objectFactory = $objectFactory; $this->_resourceModel = $objectResource; } // end /** * Save a object * * @param ObjectInterface $object * @return CompanyExtensionApiDataObjectInterface|void * @throws MagentoFrameworkExceptionCouldNotSaveException */ public function save(ObjectInterface $object) { try { $this->_resourceModel->save($object); } catch (Exception $e) { throw new Exception(__('Unable to save object')); } // end } // end /** * Delete a object * * @param ObjectInterface $object * @return bool|void * @throws Exception */ public function delete(ObjectInterface $object) { $this->_resourceModel->delete($object); } // end /** * Return a object by Object ID * * @param int $objectId * @return $this|CompanyExtensionApiDataObjectInterface */ public function getById($objectId) { $object = $this->_objectFactory->create(); $this->_resourceModel->load($object, $objectId); return $object; } // end } // end
Now you can type the following to do CRUD stuff with your object
/** @var CompanyExtensionModelObject $object */ $object = $this->_objectRepository->getById($objectId); // if we have an existing object with an ID attach the customer to it if ($object->getId() != null && $object->getId() > 0 ) { $object->setContent("content here"); // attempt to save the updated object $this->_objectRepository->save($object); } // end if
Gareth
Related Posts |
---|
Magento 2 – Fatal error: Uncaught TypeError: Argument 2 passed to __construct() |
./bin/magento Exceptions – Magento 2 |