8

How can I, an end-user-programmer, create a new "CRUD" model in Magento 2? That is, I want to create the PHP class files and XML configuration files that will

  1. Let me Create, Read, Update and Delete data from a database table
  2. Let me create the initial database table(s) that will store this information.

In Magento 1 this was handled by a Model, Resource Model, Setup Resource Model, and various nodes in config.xml. It's not 100% clear how this is handled in Magento 2.

Is there a sample module or clear tutorial out there yet for this? Or is working backwards from an existing core model (like CMS page) our only option?

4

Tracked this one down on my own.

First, Magento 2 requires no additional XML for creating a CRUD model. Magento 2 automatically knows how to create the needed resource model and collection objects based on a naming convention.

Magento 2 does require a

  • Model Class
  • Resource Model Class
  • Collection Model Class
  • A Schema install class

The specifics of each of these are beyond the scope of a single Stack Exchange answer, but the CMS Page model offers a simple example of each class.

https://github.com/magento/magento2/blob/develop/app/code/Magento/Cms/Model/Page.php

https://github.com/magento/magento2/blob/develop/app/code/Magento/Cms/Model/ResourceModel/Page.php

https://github.com/magento/magento2/blob/develop/app/code/Magento/Cms/Model/ResourceModel/Page/Collection.php

https://github.com/magento/magento2/blob/develop/app/code/Magento/Cms/Setup/InstallSchema.php

Also, my Magento 2 code generation tool pestle now has a generate_crud_model command.

3

To create initial database tables you need to create an InstallSchema.php file in the Setup folder of your extension. This needs to implement the InstallSchemaInterface.

Did you checkout this extension: https://github.com/tzyganu/Magento2SampleModule? It helped me to understand a lot about Magento 2 CRUD. I think it's compatibel with Magento 2.0.0, but I'm not sure. You can also find an example of the InstallSchema there.

  • Magento recently updated our own sample modules, which you can check out also. – Steve Johnson Jan 7 '16 at 15:01
  • @SteveJohnson Is there a CRUD model example in that link? I don't see it. – Alan Storm Jan 7 '16 at 16:28
  • I'm told that there is a module that is waiting to be merged but I do not know the exact details. It's PR#2 on the internal GitHub. – Steve Johnson Jan 8 '16 at 18:11
2

My way follow steps by steps
1- Registering a module (registration.php and module.xml)
2- Creating a system configuration file (system.xml)
3- Creating a configuration file (config.xml)
4- Creating access control lists (acl.xml)
5- Creating an installation script (InstallSchema.php)
6- Managing entity persistence (model, resource, collection)
7- Building a frontend interface
8- Building a backend interface

From step 5 i think it pretty get much time there. Example you can see in samples from core magento. It depends on what you need archivement
Simple flat table? See in module like cms. This module is clearest to understand
EAV tables? See in modules catalog, customer, sales, tax..

2

For Magento 2. I'm using below CRUD generator

https://mage2gen.com/

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.