Magento – Models, resource models, and collections

May 14th, 2012 by Laeeq | 3 comments

Here in this post we will discuss how to work with database in Magento. As we know that Magento is enrich with MVC (Model – View – Controller). So we will basically starts with some main concepts of models, resource models, and collections.

Basic concepts of models, resource models, and collections

A “model” is used to store data, and perhaps performs some business logics against that data.

A “resource model” is used to interact with the database on behalf of the “model”. The “resource model” actually performs the CRUD operations.

A “collection model” holds from one to many “models” and knows how to tell the “resource model” to get rows in the basis of information it is given.

There’s a basic ActiveRecord-like/one-object-one-table Model, and there’s also an Entity Attribute Value (EAV) Model.

Configure a database connection
  1. <resources>
  2. <affiliateplus_setup>
  3. <setup>
  4. <module>Phpzag_Affiliate</module>
  5. </setup>
  6. <connection>
  7. <use>core_setup</use>
  8. </connection>
  9. </affiliateplus_setup>
  10. <affiliateplus_write>
  11. <connection>
  12. <use>core_write</use>
  13. </connection>
  14. </affiliateplus_write>
  15. <affiliateplus_read>
  16. <connection>
  17. <use>core_read</use>
  18. </connection>
  19. </affiliateplus_read>
  20. </resources>

Create and register new entities
  1. <entities>
  2. <account>
  3. <table>affiliate_account</table>
  4. </account>
  5. </entities>

As we know that Zend_Db and its related classes provide a simple SQL database interface for Zend Framework. The Zend_Db_Adapter is the basic class you use to connect your PHP application to Mysql database.

Here we will use Zend_Db_Adapter_Pdo_Mysql classes to query the Database

  1. $db = new Zend_Db_Adapter_Pdo_Mysql(array(
  2. ‘host’ => ’127.0.0.1′,
  3. ‘username’ => ‘dbuser’,
  4. ‘password’ => ‘xxxxxxxx’,
  5. ‘dbname’ => ‘dbname’,
  6. ‘profiler’ => true,
  7. ));

Returns to the information of queries.

  1. $profiler = $db->getProfiler();

Zend_Db_Statement
  1. $sql = ‘SELECT * FROM order WHERE status = ?’;
  2. $stmt = new Zend_Db_Statement_Mysqli($db, $sql);

Zend_Db_Table:

Each class interacts with one table in the database, and you need to declare the database table for which this class define.
Example:

  1. class Orders extends Zend_Db_Table_Abstract
  2. {
  3. protected $_oname = ‘order’;
  4. }
  5. $table = new Order(array(‘db’ => $db));

With $table object, you can use some methods to operate with the database such as: insert, update, delete.

Zend_Db_Table_Row:

This will return the record object in the table

  1. $bugs = new Order();
  2. $row = $bugs->fetchRow($bugs->select()->where(‘order_id = ?’, 1));
  3. $rowArray = $row->toArray();

Zend_Db_Select:
  1. $select = $db->select()
  2. ->from( …specify table and columns… )
  3. ->where( …specify search criteria… )
  4. ->order( …specify sorting criteria… );

Database collection in Magento

The collection in Magento usually extends from class Mage_Core_Model_Resource_ Collection_Abstract or Mage_Core_Model_Mysql4_ Collection_Abstract. The collection has some methods for you to filter, sort and specify the selected values:

  • addFieldToFilter(,): used to filter data
  • setOrder(): used to sort data
  • getSelect(): returns the selected query (is instance object of class Varien_Db_Select) to this collections. And you are able to use it to add specific selected value.
  • Database resource
    • abstract class Mage_Core_Model_Resource_Abstract
    • abstract class Mage_Core_Model_Mysql4_Abstract
  • The database model and the collection connect to database through database resource layer. The resource class extends from an abstract class:

    In this class, you need to declare your database table and the id field of this table.

    For example:

    1. public function _construct(){
    2. $this->_init(‘affiliate/program’, ‘order_id’);
    3. }

    We can also use and resolve existing table names without hard coding them

    1. $resource = Mage::getSingleton(‘core/resource’);
    2. $eavAttributeTable = $resource->getTable(‘eav/attribute’);

    “eav/attribute” here is your configuration for table eav_entity_attribute in your database.

    Hope it will work for you!

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Get an array of billing addresses and shipping addresses in Magento
  • Duplicate Data Issue with Custom Magento Collection
  • Tips To Improve Your Ecommerce Site’s Security
  • Magento: Display New Products from Specific Category On Home Page
  • Dealing with Magento Model or Collections
  • Magento: Display New Products On Home Page
  • Magento: Display More Than 5 New Products
  • Override Controllers in Magento
  • Improve the performance of your Magento store
  • Magento Released Community Edition 1.7.0.1
  • Write custom title, keywords and description in Magento module
  • Moving Magento site from development to live server
  • Display Related products on product details page in Magento
  • Magento – Add thumbnail images to Magento admin grid
  • Simple url rewrite in magento
  • Generate CSV file in Magento
  • Resize image in Magento using Varien_Image class
  • AJAX requests in Magento
  • Category Navigation Listings in Magento
  • Magento- Create a Drop-Down of Countries
  •  

     

    1. nidhi
      May 20th, 2012 at 11:02 | #1

      nice tutorial.Please provide more knowledege about magento

    2. July 4th, 2012 at 16:45 | #2

      nice content. I agree with nidhi: please, continue with the tutorial, providing more knowledege about mage eav… =]

    3. Josua Marcel
      December 19th, 2012 at 05:32 | #3

      Nice ! this is the solution of my problem. Thank YOU!

    1. No trackbacks yet.