This entry was posted on Tuesday, March 31st, 2009 at 4:27 am and is filed under development, howto. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Mar 31, 2009
Howto: Repackageable custom extension development in Magento – Part 8 – CRUD – Update
Author: gaweee | Filed under: development, howtoCRUD – Update
Very similar to the Create phase, this phase basically requires the Form and Form Container blocks, and the controller to save them. Thats all!
- Repeat the file structure goodness:
app/ etc/ modules/ - SavantDegrees_All.xml (Or what ever your company name might be) code/ local/ SavantDegrees/ (Or what ever your company name might be) Twits/ (Or whatever your module name might be) Block/ Admin/ - Main.php Main/ - Grid.php - Edit.php Edit/ - Form.php - New.php New/ - Form.php - HelloWorld.php controllers/ - AdminController.php - IndexController.php etc/ - config.xml Helper/ - Data.php Model/ - Tip.php Mysql4/ - Tip.php Tip/ - Collection.php sql/ twits_setup/ - mysql4-install-0.2.0.php - mysql4-upgrade-0.1.0-0.2.0.phpHere we’ve added the block for the
Editcontainer andForm - Looking at the
Edit.phpcode:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php class SavantDegrees_Twits_Block_Admin_Edit extends Mage_Adminhtml_Block_Widget_Form_Container { public function __construct() { parent::__construct(); $this->_blockGroup = 'twits'; $this->_mode = 'edit'; $this->_controller = 'admin'; if( $this->getRequest()->getParam($this->_objectId) ) { $tip = Mage::getModel('twits/tip') ->load($this->getRequest()->getParam($this->_objectId)); Mage::register('frozen_tip', $tip); } } public function getHeaderText() { return Mage::helper('twits')->__("Edit Tip'%s'", $this->htmlEscape(Mage::registry('frozen_tip')->getName())); } }
The additional code loads the right
Tipdata based on theRequestparameter, then stores that data in theMageregistry. We’ll be using that data on theFormpage again later to populate the form.
Notice that the saving code isMage::registerand the retrieving code isMage::registry - Then the accompanying
Edit/Form.phpcode1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<?php class SavantDegrees_Twits_Block_Admin_Edit_Form extends Mage_Adminhtml_Block_Widget_Form { protected function _prepareForm() { $form = new Varien_Data_Form(); $fieldset = $form->addFieldset('edit_tip', array('legend' => Mage::helper('twits')->__('Tip Details'))); $fieldset->addField('title', 'text', array( 'name' => 'title', 'title' => Mage::helper('twits')->__('Title'), 'label' => Mage::helper('twits')->__('Title'), 'maxlength' => '250', 'required' => true, )); $fieldset->addField('author', 'text', array( 'name' => 'author', 'title' => Mage::helper('twits')->__('Author'), 'label' => Mage::helper('twits')->__('Author'), 'maxlength' => '250', 'required' => true, )); $fieldset->addField('contents', 'textarea', array( 'name' => 'contents', 'title' => Mage::helper('twits')->__('Contents'), 'label' => Mage::helper('twits')->__('Contents'), 'style' => 'width: 98%; height: 200px;', 'required' => true, )); $form->setMethod('post'); $form->setUseContainer(true); $form->setId('edit_form'); $form->setAction($this->getUrl('*/*/save')); $form->setValues(Mage::registry('frozen_tip')->getData()); $this->setForm($form); } }
-
Then lastly the
adminControllerwith itseditActionandsaveActionto load and save the form respectively1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
<?php class SavantDegrees_Twits_AdminController extends Mage_Adminhtml_Controller_Action { public function indexAction() { $this->loadLayout() ->_addContent($this->getLayout()->createBlock('twits/admin_main')) ->renderLayout(); } public function deleteAction() { $tipId = $this->getRequest()->getParam('id', false); try { Mage::getModel('twits/tip')->setId($tipId)->delete(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('twits')->__('Tip successfully deleted')); $this->getResponse()->setRedirect($this->getUrl('*/*/')); return; } catch (Exception $e){ Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } $this->_redirectReferer(); } public function newAction() { $this->loadLayout() ->_addContent($this->getLayout()->createBlock('twits/admin_new')) ->renderLayout(); } public function postAction() { if ($data = $this->getRequest()->getPost()) { $tip = Mage::getModel('twits/tip')->setData($data); try { $tip->save(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('twits')->__('Tip successfully saved')); $this->getResponse()->setRedirect($this->getUrl('*/*/')); return; } catch (Exception $e){ Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } $this->getResponse()->setRedirect($this->getUrl('*/*/')); return; } public function editAction() { $this->loadLayout(); $this->_addContent($this->getLayout()->createBlock('twits/admin_edit')); $this->renderLayout(); } public function saveAction() { $tipId = $this->getRequest()->getParam('id', false); if ($data = $this->getRequest()->getPost()) { $tip = Mage::getModel('twits/tip')->load($tipId)->addData($data); try { $tip ->setId($tipId)->save(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('twits')->__('Tip successfully saved')); $this->getResponse()->setRedirect($this->getUrl('*/*/')); return; } catch (Exception $e){ Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } $this->_redirectReferer(); } }
-
Ok i admit the code is a tad inefficient. We have 2 separate functions that both does save. In fact,
postActionandsaveActiononly differ that saveAction loads the right record via the->load($tipId)function. Thats separates aCREATEfrom anUPDATErequest.And we’re finally finally finally done! To test out the edit function, simply click on any row from the
Grid! Thats assuming of course that you actually have some data.
» For frontend stuff, carry on … Fontend – List
Otherwise, if it works for you, leave a comment! =)
read users' comments (16)
16 Responses to “Howto: Repackageable custom extension development in Magento – Part 8 – CRUD – Update”
Leave a Reply
Most Popular
- HOWTO: PHP and jQuery upload progress bar (48)
- JQuery Progress Bar 1.1 (42)
- Howto: Repackageable custom extension development in Magento - Part 2 - Admin Controller (24)
- Howto: Repackageable custom extension development in Magento - Part 8 - CRUD - Update (16)
- HOWTO: struts 2 i18n (13)
- JQuery Progress Bar 2.0 (12)
- JQuery Progress Bar 1.2 (11)
- Howto: Repackageable custom extension development in Magento (9)
- Howto: Repackageable custom extension development in Magento - Part 3 - Database (9)
- Howto: Repackageable custom extension development in Magento - Part 9 - Frontend - List (8)
Recent Comments
- learning methods: All the posts you talk
- best travel agency: Wow! I really enjoyed this
- Muzafar Ali: Hi, thanks for sharing a beautiful
- andy65007: After the progress bar
- Mark: Excellent tutorials. Most "how to
- Mark: As Jim said before, you
- Jeremy Roberts: I tried and it works
- Silas Nordes: Came to this site by
- 150cc mopeds: That was a superb blog
- Jason: if the situation is like
Latest Entries
- jQuery Progress Bar Configuration
- Extracting email addresses from inbox
- 10 Good (Free and Legal) Source for Photos and Images
- Howto: Backup Microsoft SQL Server Database, as in Dump it to a SQL Script (like MYSQL's sqldump)
- Managing client's expectation with wireframe software
- Howto: Repackageable custom extension development in Magento - Part 9 - Frontend - List
- JQuery Progress Bar 2.0
- HOWTO: Find icons for your new prototype system
- Google Maps Helper
- laying the cornerstones

March 25th, 2010 at 8:38 pm
Yup, i’ve cleaned up the article for Magento 1.41
March 25th, 2010 at 10:58 am
Hi, thanks for a brilliant tutorial!
I need a view action – I’ve found the Mage_Adminhtml_Block_Widget_View_Container class but can’t figure out how to use it properly – any pointers? Thanks again for your time writing this tutorial.
Col.
March 22nd, 2010 at 3:06 pm
This is a fantastic!!!
Does anyone know if the anything in the massive change list for magento 1.4 has affected the accuracy of this tutorial?
January 2nd, 2010 at 8:00 pm
Thank you a lot for spending time to write this very very useful tutorial!
September 17th, 2009 at 12:53 am
Thanks very much. What a nice tutorial.
But perhaps Admin Panel coundn’t search/filter yet !?
August 2nd, 2009 at 1:51 am
Hi Brendan
frozen_twitis the object that is stored in Mage registry for access via different classes. Think of it as a better way of doingglobalTo save something in the registry we use
Mage::registerand to retrieve it, we us
Mage::registryAugust 1st, 2009 at 6:54 am
hi,
really great tutorial.
one question.
what/how does the following code work?
$form->setValues(Mage::registry(‘frozen_twit’)->getData());
I dont understand the “frozen_twit”
is this referencing a call somewhere else?
brendan
July 8th, 2009 at 1:33 pm
Thanks so much for this tutorial. It really helped with beginning to understand Magento module Development.
June 22nd, 2009 at 4:46 pm
Thanks Jim. The tutorial was simply superb
June 17th, 2009 at 12:19 pm
Many thanks for such an informative tutorial. Have you thought about moving it over to the Magento Wiki so that it can get more exposure to the community?
May 19th, 2009 at 12:36 pm
Wow,
great tutorial. Followed to the end and able to create the admin component.
This is the first first tutorial that successfully worked for me.
Great for putting time and effort to write these 8 blogs.
April 21st, 2009 at 10:06 am
Many thx for this howto
April 16th, 2009 at 4:23 am
Hi Jim
the addField is implmented in the Varien_Data_Form code. See the docs at http://docs.magentocommerce.com/Varien/Varien_Data/Varien_Data_Form_Abstract.html
It adds a Varien Form Element. See the children of the varien form abstract element at http://docs.magentocommerce.com/Varien/Varien_Data/Varien_Data_Form_Element_Abstract.html
If you feel up to it, you can view the source files at the
/lib/Varien/Data/Formfolder.April 15th, 2009 at 6:35 am
hi once again gawee
Another thing, I’d be very interested in learing where you found the details on the addField function.. I have tried changing the format from text to date and get an error about other formatting it needs but finding any docs on magento regarding addField has been a nightmare
April 13th, 2009 at 1:14 pm
hi again,
o.k I have been searching around for this solution however still not got any closer…
I notice that at the end of this tutorial I can see view the hello world on the screen by going store/twit …. so i tried this..
1. created app/frontend/default/mytheme/templates/savantdegrees/twit/index.phtml and in it just entered “test”
2. on my home page CMS content area added :
lock type=”savantdegrees_twit/index” my_custom=”Test” template=”twit/index.phtml” />
This didnt work, nothing displayed on my home page.. doh.. but no errors…
Am I at all close?
cheers again,
Jimm
April 12th, 2009 at 3:43 pm
hi again gawee
wow I got to the end.. a really great tutorial… you have put a lot of time in i can see, thanks.
I do have a question though…. the whole setup of the documented twits module is geared towards displaying the twits in a grid within the admin pages…. do you have any information on displaying the twits outside of the grid format, within the front end of the store? Maybe, in block form in static blocks for example?
Thanks again,
Jimm!