I want to understand the actual crud operation from admin side with coding.
Magento 1.9 – Basic CRUD Operation from Admin Side
crudmagento-1.9
Related Solutions
$cancelId = 5;//id for entry to update
$cancel = $this->_cancelFactory->create();
$cancel->load($cancelId);
$cancel->setCancelStatus('Review');
$cancel->save();
But as explained in the answers in here: Deprecated save and load methods in Abstract Model, load
and save
are deprecated. You should create service contracts for your module.
Here I display admin side CRUD operation with UI Component. I created one module named admin event management. Follow this file tree structure.
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.
Best Answer
I Found the Perfect Answer for that
Create Controller from admin side app\code\local\Crud\Pro\controllers\Adminhtml\ProController.php
Create Block from admin side app\code\local\Crud\Pro\Block\Adminhtml\Pro.php
Create edit functionality app\code\local\Crud\Pro\Block\Adminhtml\Pro\Edit.php
Create a grid from admin side
app\code\local\Crud\Pro\Block\Adminhtml\Pro\Grid.php
Now Come to Edit Part 1.edit tab 2.edit form
app\code\local\Crud\Pro\Block\Adminhtml\Pro\Edit\Tabs.php
Create Edit form when click on grid part 1 the actual form is in part 2 app\code\local\Crud\Pro\Block\Adminhtml\Pro\Edit\Form.php
Create Edit form when click on grid part 2 app\code\local\Crud\Pro\Block\Adminhtml\Pro\Edit\Tab\Form.php
For Upload Image we want to create renderer folder app\code\local\Crud\Pro\Block\Adminhtml\Renderer\Image.php
app\code\local\Crud\Pro\Block\Adminhtml\Adminimage\Image.php use to show image thumbnail from admin grid side
app\design\adminhtml\default\default\layout\pro.xml