2

Is there any working example for CRUD operations step by step with code? I tried many links but not getting the accurate result to understand the steps of crud operations.

I want to make it an extension for it. Not to heavy code but make it very simple so that anyone can understand easily.

If anyone can make it than it will be a great help to beginners.

Thank you!

1

Here I display admin side CRUD operation with UI Component. I created one module named admin event management. Follow this file tree structure.

enter image description hereenter image description here

First create registration.php and composer.json

Chirag->Events->registration.php

 <?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Chirag_Events',
    __DIR__
);

Chirag->Events->composer.json

{
    "name": "chirag/module-events",
    "description": "",
    "type": "magento2-module",
    "license": "proprietary",
    "authors": [
        {
            "email": "chirag@czargroup.net",
            "name": "Czargroup"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4": {
            "Chirag\\Events\\": ""
        },
        "files": [
            "registration.php"
        ]
    }
}

Chirag->Events->etc->module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Chirag_Events" setup_version="1.0.1" />
</config>

Chirag->Events->etc->di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="ChiragEventsGridFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool">
        <arguments>
            <argument name="appliers" xsi:type="array">
                <item name="regular" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\RegularFilter</item>
                <item name="fulltext" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\FulltextFilter</item>
            </argument>
        </arguments>
    </virtualType>
    <virtualType name="ChiragEventsGridDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
        <arguments>
            <argument name="collection" xsi:type="object" shared="false">Chirag\Events\Model\ResourceModel\Events\Collection</argument>
            <argument name="filterPool" xsi:type="object" shared="false">ChiragEventsGridFilterPool</argument>
        </arguments>
    </virtualType>
    <virtualType name="Chirag\Events\Model\ResourceModel\Events\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">chirag_events_table</argument>
            <argument name="resourceModel" xsi:type="string">Chirag\Events\Model\ResourceModel\Events</argument>
        </arguments>
    </virtualType>
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="chirag_events_index_listing_data_source" xsi:type="string">Chirag\Events\Model\ResourceModel\Events\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

Chirag->Events->etc->adminhtml->menu.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Backend/etc/menu.xsd">
    <menu>
        <add id="chirag_events::core" title="Events Create" module="Chirag_Events" sortOrder="90" resource="Chirag_Events::sample"/>
        <add id="chirag_events::test" title="Display All Events" module="Chirag_Events" sortOrder="10" parent="chirag_events::core" action="chirag_events/items/" resource="Chirag_Events::items"/>
    </menu>
</config>

Chirag->Events->etc->adminhtml->routes.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="admin">
        <route id="chirag_events" frontName="chirag_events">
            <module name="Chirag_Events" />
        </route>
    </router>
</config>

Now create setup files. Chirag->Events->Setup->InstallSchema.php

<?php

namespace Chirag\Events\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        /**
         * Creating table chirag_events_table
         */
        $table = $installer->getConnection()->newTable(
            $installer->getTable('chirag_events_table')
        )->addColumn(
            'entity_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Chirag Events Id'
        )->addColumn(
            'reason',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => false],
            'Reason'
        )->addColumn(
            'date',
            \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
            null,
            ['nullable' => false],
            'Choose Date'
        )->addColumn(
            'status',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            11,
            ['nullable' => true,'default' => 1],
            'Status'
        )->addColumn(
            'created_at',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            ['nullable' => false],
            'Created At'
        )->addColumn(
            'updated_at',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            ['nullable' => false],
            'Updated At'
        )->setComment(
            'Chirag Events Table'
        );
        $installer->getConnection()->createTable($table);
        $installer->endSetup();
    }
}

Chirag->Events->Setup->UpgradeSchema.php

<?php

namespace Chirag\Events\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        /**
         * Creating table chirag_events_table
         */
        if (version_compare($context->getVersion(), '1.0.1') < 0) {
            $table = $installer->getConnection()->newTable(
                $installer->getTable('chirag_events_table')
            )->addColumn(
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'Chirag Events Id'
            )->addColumn(
                'reason',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => false],
                'Reason'
            )->addColumn(
                'date',
                \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
                null,
                ['nullable' => false],
                'Choose Date'
            )->addColumn(
                'status',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                11,
                ['nullable' => true,'default' => 1],
                'Status'
            )->addColumn(
                'created_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false],
                'Created At'
            )->addColumn(
                'updated_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false],
                'Updated At'
            )->setComment(
                'Chirag Events Table'
            );
            $installer->getConnection()->createTable($table);
        }
        $installer->endSetup();
    }
}

Chirag->Events->Model->Events.php

<?php

namespace Chirag\Events\Model;

use Magento\Framework\Model\AbstractModel;

class Events extends AbstractModel
{
    /**
     * Define resource model
     */
    protected function _construct()
    {
        $this->_init('Chirag\Events\Model\ResourceModel\Events');
    }
}

It is not possible to put whole code here. Follow this link for whole code and extension. http://chiragjdsofttech.blogspot.com/2019/12/magento-2-extension-for-admin-events.html

These are screenshots from this extension. I Hope it will help to beginners.

enter image description hereenter image description hereenter image description here

| improve this answer | |
0

Please follow this tutorial, I've also used it in my Magento 2 beginning, You will learn creating admin grid, its form, mainly CRUD operation also you will also learn ACL in Magento and how to display data on front-end, css, pagination etc. Please refer to this link you'll find the whole tutorial:

Magento 2 from scratch

| improve this answer | |
0

You can follow below tutorial. I have also used below reference for step by step learning in my Magento2 beginning.

Magento2 Admin Grid

| improve this answer | |
0

As others have not mentioned it, why not take all the pain away and use "Ultimate Module Creator"? It creates the whole thing for you with a couple of clicks... it has some limits but it takes minutes for something that often done manually can take hours and hours if just one part is wrong.

| improve this answer | |

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.