This entry was posted on Tuesday, March 31st, 2009 at 4:18 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 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
7 Responses to “Howto: Repackageable custom extension development in Magento – Part 5 – CRUD – Retrieve”
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

June 14th, 2010 at 7:46 am
Hi,
thanks for sharing a beautiful article
January 4th, 2010 at 1:52 pm
Thnak you! I was struggling with a grid whos collection had a join which resulted in an ambigous column in the where clause. While not directly addressing this issue your example used the “filter_index” property which was exactly what I was looking for!
August 2nd, 2009 at 1:58 am
Hi 23,
what you’re looking for is relatively tricky i assume. Magento’s way of piecing the
Sales OrdersandInvoicestogether is quite complicated.I have not dived that deep into the
modelsmanipulation yet so i’m uncertain how to do the advanced grouping/reporting required.But it should be doable. Where possible you’ll have to learn to work with the
model's collectionandfilters, then go the rest of the way by your own calculations. =)For example you can get a
collectionofsales invoices, filter them by date, then add up the totals together. to get your periodic revenue, then go into the items to calculate the periodic cost and finally get the profit.Sounds easier than it really its. =p
July 31st, 2009 at 4:37 am
hey gaweee -
thanks for your advices. my problem is that I can not find the depending collection.php or the place where I have to put the code in. I am also not able to find a fitting sql query, which I can manipulate like “SELECT ‘cost’ FROM …” or how to accumulate the costs over a specific time (i.e. monthly costs / profit) and calculate the periodic profit.
I expect that I am too dumb to understand the needed framework and programming knowledge.
gaweee – do you have a donate-button somewhere on your page and 10mins time?
regards.
July 30th, 2009 at 2:57 pm
Hey 23,
There are some events of a
Gridthat gets triggered after loading theCollection_afterLoador something. you can always override those methods and manipulate theModel‘sCollectioninto theGrid‘s data. then point the rightIndexthere. But that’s a really dirty way to do it.The right way is to use
GridFiltersandRenderers. Example:As well as
Then you can add the column in the
Gridthis way:July 28th, 2009 at 6:20 am
Hi – Thanks a lot for your awesome howtos! Nice job!
My question is the following:
I try to expand the sales report by two columns in order to show the cost and calculate the net profit.
The changes in the app/code/local/mage/adminhtml/block/report/sales/sales/grid.php worked well.
I added the following lines:
$this->addColumn(‘cost’, array(
‘header’ => Mage::helper(‘sales’)->__(‘cost’),
‘index’ => ‘cost’,
‘type’ =>’currency’
));
$this->addColumn(‘profit’, array(
‘header’ => Mage::helper(‘sales’)->__(‘net profit’),
‘index’ => ‘shipping_description’ //shipping_discription for testing purposes
));
I would like to know which entries for ‘index’ are available in this report and how to expand the corresponding collection.php to fetch the product relevant costs and calculate the net profit and paste it in a var.
Maybe somebody would help me
Kind Regards,
23
April 21st, 2009 at 10:03 am
I got an error while filtering results.
Removing prefix “dt.” in filter_index’s value of $this->addColumn solved the problem.