Magento – Accessing data from database in magento

ce-1.9.0.1database

The code to fetch data from columns is:

<?php
    /**
      * Get the resource model
      */
    $resource = Mage::getSingleton('core/resource');

    /**
     * Retrieve the read connection
     */
    $readConnection = $resource->getConnection('core_read');

    /**
     * Retrieve our table name
     */
    $table = $resource->getTableName('catalog/product');

    /**
     * Execute the query and store the results in $results
     */
    $sku = $readConnection->fetchCol('SELECT sku FROM ' . $table . ');

    /**
     * Print out the results
     */
     var_dump($results);  

So what is the code for fetching data from a row? Is it like this?

<?php
    /**
      * Get the resource model
      */
    $resource = Mage::getSingleton('core/resource');

    /**
     * Retrieve the read connection
     */
    $readConnection = $resource->getConnection('core_read');

    /**
     * Retrieve our table name
     */
    $table = $resource->getTableName('catalog/product');

    /**
     * Execute the query and store the results in $results
     */
    $sku = $readConnection->fetchRow('Random Query');

    /**
     * Print out the results
     */
     var_dump($results);

Or is there any special procedure for it?

Best Answer

//get the parameter from the request
    $param = $this->getRequest()->getParam('manufacturer');

    //instantiate the brand/brand model, and use 
    //its `getCollection` method to return a collection
    //object
    $collection = Mage::getModel('brands/brands')->getCollection();

    //add the paramater as a filter
    $collection->addFieldToFilter('attributelabelid', $param);

    //get the first item of the collection (load will be called automatically)
    $extrabrand = $collection->getFirstItem();

    //look at the data in the first item
    var_dump($extrabrand->getData());

Or you can use different attribute sets to make filtration on the row data entity you need.

 $table = $resource->getTableName('catalog/product');

you can use a number of addtofilter or attribute type search

$collection = Mage::getModel('module/model_name')->getCollection()
    ->addAttributeToSort('order', 'ASC')
    ->addAttributeToSort('last_name', 'ASC')
    ->addAttributeToSort('first_name', 'ASC')

This is the way by which we can use the attribute label to sort.

Related Topic