Search This Blog

Wednesday, February 23, 2011

Magento insert update edit CRUD operations for dummies and beginers


Todo module for dummies: from the horse’s mouth :)

Some information  before we stare at the code:

Magento Path: phtmlàBlockàHelper->IndexControlleràphtml(updated)
Module naming: Modulename/Packagename e.g Shiva/Todo

Important  Folder info:

Adminhtml:contains config for admin menus like edit, grid, form, tab etc...

Block:contains front end outputs i.e the actions like getPostAction, editAction, prepareLayout etc..

Controllers:for interacting with the modules 

eg. indexcontroller : the action associated with the form like createAction, editAction, deleteAction

Adminhtml/XXXController : action for the backend like save edit,delete, massdelete etc... where XXX is your package name, here Todo

ETC contains the config directory
            config.xml default setting for module
            system.xml admininistration settings and admin form option ofr modules
Helper: helpers for module, covering areas such as translation e.g create, delete, edit functions are here Data.php

Model
Mysql4: (The top ORM model directory)---database related activity
 
Sql:mysql4-install-0.1.0.php contains database tables information required

Creating folder structure
To start with creating a Todo application in magento we need to build a module that becomes a part of the site that has its own URL. We will be creating our own module that will run on \todo URL structure.
Create the following folders in local directory [app\code\local\]
/Shiva
            /Todo
                        /Block
                        /etc
                        /Helper
/Model
/sql
/controllers
In etc/modules create Shiva_Todo.xml
etc/modules
- Shiva_Todo.xml
Tell magento that the new module is here
<?xml version="1.0"?>
<config>
    <modules>
        <Shiva_Todo>
            <active>true</active>
            <codePool>local</codePool>
        </Shiva_Todo>
    </modules>
</config>

Clear the cache and check in site->admin->system->Configuration->Advanced

Check if the module is “Enabled”.

For the frontend part

Your todo form is located in your theme
Path:-app\design\frontend\base_new\default\template\Todo\todo.phtml

Check the code in file
To view the formpage in address bar, check the form name front end by going to : Shiva/Todo/etc/config.xml and check the <frontend> tag

The form will be displayed as ‘todo’ as the name given in < frontName> tag

Some more file info…..

Helper class:

\Shiva\Todo\Helper

Helper:helpers for module, covering areas such as translation e.g create, delete, edit functions are here Data.php

Create a php file named Data.php

That file has a helper class named Shiva_Todo_Helper_Data in [Shiva/Todo/Helper/Data.php]

Check the code.

If you come across the code [‘todo/index/create’] it simply means in the todo module it has an Index controller and Create action [module / controller / action].

Check the necessary functions to get the id and perform respective actions for create, edit and delete.
Block class:
\Shiva\Todo\Block

Block: In Todo.php the class extend the template in the class and define the post Url actions
Grid.php
Location: \Adminhtml\Todo\Grid.php->define the display gird functions here

Edit.php
Location : \Adminhtml\Todo\ -> define the edit functions here
……..

Controllers:

IndexController.php
Location : \Shiva\Todo\controllers -> defines the actions here which are defined in Block and triggered from helper class
Admin side:
TodoController.php
Location : Shiva\Todo\controllers\Adminhtmlà for the admin actions like edit save etc….
Frontend part:
feedback.xml
Location: app\design\adminhtml\base_new\default\layout\Todo.xml
feedback.phtml
app\design\frontend\base_new\default\template\Todo\Todo.phtml
Do not forget to create a table name ‘todo’ in database
Form actions…………
To  create a record :
The flow

Magento Path: phtml-->Block-->Helper->IndexController-->phtml(updated)

Todo Path: todo.phtml---(on submit)--->(Todo.php)Block---$id-->Data.php)Helper->(indexcontroller.php)IndexController---redirect-->phtml(updated
)

Check the code in todo.phtml,Todo.php,Data.php,Indexcontroller.php
The screen shots:












Develop your custom module from the scratch with this demo and learn Magento
Download the source code here
The code shared is purely for education purpose only
# If you find any misleading info then tell us else tell others.





4 comments:

  1. Good tutorial but where is download link...
    It displays download link as a simple text

    ReplyDelete
  2. where is the download link for code ?, please give me link.

    ReplyDelete
  3. please give me the download link

    thanks a lot

    ReplyDelete