Archive for the 'howto' Category
Howto: Repackageable custom extension development in Magento – Part 6 – CRUD – Delete
Author: gaweee | Filed under: development, howtoCRUD – Delete
- The simplest step thus far, add a new function to your
AdminController.php1 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
<?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(); } }
» Almost there … CRUD – Create
Howto: Repackageable custom extension development in Magento – Part 5 – CRUD – Retrieve
Author: gaweee | Filed under: development, howtoCRUD – Retrieve
- Lets add a new block to the file structure
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 - 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.phpWe’ll create folder called
Adminto store all the admin related blocks. Inside we’ll have a Main.php file that contains theGrid Container. Why do we need a grid container? To put the title andCreatebutton! -
The
Main.phpcode1 2 3 4 5 6 7 8 9 10 11 12 13
<?php class SavantDegrees_Twits_Block_Admin_Main extends Mage_Adminhtml_Block_Widget_Grid_Container { public function __construct() { $this->_addButtonLabel = Mage::helper('twits')->__('Add New Tip'); parent::__construct(); $this->_blockGroup = 'twits'; $this->_controller = 'admin_main'; $this->_headerText = Mage::helper('twits')->__('Tips(s)'); } }
Here you see a first use of the helper, which will assist in translation
-
The
Grid.phpitself1 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
<?php class SavantDegrees_Twits_Block_Admin_Main_Grid extends Mage_Adminhtml_Block_Widget_Grid { public function __construct() { parent::__construct(); $this->setId('tipsGrid'); $this->_controller = 'twits'; } protected function _prepareCollection() { $model = Mage::getModel('twits/tip'); $collection = $model->getCollection(); $this->setCollection($collection); return parent::_prepareCollection(); } protected function _prepareColumns() { $this->addColumn('tip_id', array( 'header' => Mage::helper('twits')->__('ID'), 'align' => 'right', 'width' => '50px', 'filter_index' => 'dt.tip_id', 'index' => 'tip_id', )); $this->addColumn('name', array( 'header' => Mage::helper('twits')->__('Title'), 'align' => 'left', 'width' => '150px', 'filter_index' => 'dt.title', 'index' => 'title', 'type' => 'text', 'truncate' => 50, 'escape' => true, )); $this->addColumn('tags', array( 'header' => Mage::helper('twits')->__('Author'), 'align' => 'left', 'filter_index' => 'dt.author', 'index' => 'author', 'type' => 'text', 'escape' => true, )); $this->addColumn('summary', array( 'header' => Mage::helper('twits')->__('Contents'), 'align' => 'left', 'filter_index' => 'dt.contents', 'index' => 'contents', 'type' => 'text', 'escape' => false, )); $this->addColumn('action', array( 'header' => Mage::helper('twits')->__('Action'), 'width' => '150px', 'type' => 'action', 'getter' => 'getTipId', 'actions' => array( array( 'caption' => Mage::helper('twits')->__('Edit'), 'url' => array( 'base'=>'*/*/edit' ), 'field' => 'id' ), array( 'caption' => Mage::helper('twits')->__('Delete'), 'url' => array( 'base'=>'*/*/delete' ), 'field' => 'id' ) ), 'filter' => false, 'sortable' => false )); return parent::_prepareColumns(); } public function getRowUrl($row) { return $this->getUrl('*/*/edit', array( 'id' => $row->getTipId(), )); } }
If you scrutinize the code (which i’d advise against), you’ll see most of the code is geared towards the setup of the grid columns and the edit/delete urls i’ll eventually call to perform the other
CRUDfunctions.
But whats going on? Where do we use this code? The main code (being aGrid_Containerautomatically adds the childGridcode associated to it. By child we’re talking about theGrid.phpfile in theMainfolder. The folder’s name follows the file. So if you changed the folder’s name toMain2, it wouldnt work. That being said, you can just put any block code in the folder and expect it to run. The reason why this all works now is cause the geniuses at Magento already did alot of work to call the Grid child to run. They have no super time travel powers to tell what else you’re gonna put in there. So, it simply wont work (yet). - Now lets make our
adminController.phpuse that block instead of Hello world.1 2 3 4 5 6 7 8 9
class SavantDegrees_Twits_AdminController extends Mage_Adminhtml_Controller_Action { public function indexAction() { $this->loadLayout() ->_addContent($this->getLayout()->createBlock('twits/admin_main')) ->renderLayout(); } }
Well it says
createBlockdoesnt it? What do you think it does?twits/admin_mainas always refers to the twits namespace and theadmin/main.phpblock file. - Viola! Your very very very hard earned grid!
» Anytime now … Part 6 – CRUD – Delete
Howto: Repackageable custom extension development in Magento – Part 4 – Model
Author: gaweee | Filed under: development, howtoPrepare the model
- Back to the file structure drawing board
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/ - 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.phpNotice the new folder
Modeland the associated files inside. Lets clarify:
Model/Tip.phpis the logical model that holds code regarding business logic and relations.
Model/Mysql4/Tip.phpis the data implementation model that associates the model with the table and its primary key.
Model/Mysql4/Tip/Collection.phpis the data implementation model of a collection ofTip. Similar to arecordset/resultsetin.Net/Java. This will be used with a Grid later. - Even more
config.xmlmagic. Yay! More hairloss!<?xml version="1.0"?> <config> <modules> <SavantDegrees_Twits> <version>0.2.0</version> </SavantDegrees_Twits> </modules> <global> <models> <twits> <class>SavantDegrees_Twits_Model</class> <resourceModel>twits_mysql4</resourceModel> </twits> <twits_mysql4> <class>SavantDegrees_Twits_Model_Mysql4</class> <entities> <tip> <table>tips</table> </tip> </entities> </twits_mysql4> </models> <blocks> <twits> <class>SavantDegrees_Twits_Block</class> </twits> </blocks> <helpers> <twits><class>SavantDegrees_Twits_Helper</class></twits> </helpers> <resources> <twits_setup> <setup> <module>SavantDegrees_Twits</module> </setup> <connection> <use>core_setup</use> </connection> </twits_setup> <twits_write> <connection> <use>core_write</use> </connection> </twits_write> <twits_read> <connection> <use>core_read</use> </connection> </twits_read> </resources> </global> <adminhtml> <menu> <twits translate="title" module="twits"> <title>Twits</title> <sort_order>100</sort_order> <action>twits/admin</action> </twits> </menu> </adminhtml> <frontend> <routers> <SavantDegrees_Twits> <use>standard</use> <args> <module>SavantDegrees_Twits</module> <frontName>twits</frontName> </args> </SavantDegrees_Twits> </routers> </frontend> </config>
The
global > modelsfragment define thetipstable, thetip modeland itsresourceModelthat we will be using shortly. - Now for the
model/Tip.phpcode:<?php class SavantDegrees_Twits_Model_Tip extends Mage_Core_Model_Abstract { protected function _construct() { $this->_init('twits/tip'); } }
Whats the point you ask!? The code does nothing but binds the model to the Twits namespace under the model twits. In Java that line would have been
package Twits;. *i assume/think/foretold*
(quickly moving on…) - Then
Model/Mysql4/Tip.php<?php class SavantDegrees_Twits_Model_Mysql4_Tip extends Mage_Core_Model_Mysql4_Abstract { protected function _construct() { $this->_init('twits/tip', 'tip_id'); } }
The code above tells Magento to hunt for the twits application’s twits table in the configuration. Yes i know the names are confusing now. Lets say instead of
twits/tipit istwits/foo. In that case the system will find the configuration file associated with twits (which is our app/code/local/SavantDegrees/Twits/etc/config.xml), and look underconfig > global > models > twits_mysql4 > entities > footo figure out its configuration instructions. You’ll see next that we’ll request for the table from that same configuration file. Capiche?
As aresourceModel, the code binds the model to a underlyingMysql4 table. - Lastly,
Model/Mysql4/Tip/Collection.php<?php class SavantDegrees_Twits_Model_Mysql4_Tip_Collection extends Varien_Data_Collection_Db { protected $_tipTable; public function __construct() { $resources = Mage::getSingleton('core/resource'); parent::__construct($resources->getConnection('twits_read')); $this->_tipTable= $resources->getTableName('twits/tip'); $this->_select->from( array('tip'=>$this->_tipTable), array('*') ); $this->setItemObjectClass(Mage::getConfig()->getModelClassName('twits/tip')); } }
Basically the model files created at this phase are simply trying to tie the model (singular and collection) to the underlying
Mysql resource. The entire mechanism is half tied in theconfig.xmltable declaration, the model individual instructions (from identifying the namespace to actually declaring the primary key of the table) - Viola. Models are ready
» Arent we there yet? Part 5 – CRUD – Retrieve




