Example of Code and Configuration Needed for a Simple Magento 2 CRUD Model?

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.


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.


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..