Check if Customer Logged In and get the details from Session

 
<?php
 
        // require_once '/var/www/magento/app/Mage.php';
        //  Mage::init();
 
    function getSession()
    {
        return Mage::getSingleton('customer/session');
    }
 
        if (!getSession()->isLoggedIn()) {
            $this->_redirect('customer/account/login');
            return;
        }
 
        if (getSession()->isLoggedIn()) {
            echo 'loggedIn';
 
            $customerData = Mage::getSingleton('customer/session')->getCustomer();
 
            printf("%s\n %s\n %s\n %s",
                $customerData->getId(),
                $customerData->getWebsiteId(),
                $customerData->getStoreId(),
                $customerData->getIsActive());
            //Zend_Debug::dump($customerData);
        }

Magento Core Tables

1) eav_attribute
whenever you create a attribute this table gets entry which stores all important data which helps to make a relation, like entity_type_id,attribute_code,backend_type and many more but this are the important fields.

2) eav_entity_type

Magento has 8 types of entity, by which, type of attribute is decided. in your case it is catalog_product

1) customer
2) customer_address
3) catalog_category
4) catalog_product
5) order
6) invoice
7) creditmemo
8) shipment

catalog_product_entity is the primary table for product, our product id is nothing but a value of entity_id of this table.

Comparing EAV to Relational Table

EAV makes a lot of sense for a generic e-commerce solution. A store that sells laptops (which have a CPU speed, color, ram amount, etc) is going to have a different set of needs than a store that sells yarn (yarn has a color, but no CPU speed, etc.). Even within our hypothetical yarn store, some products will have length (balls of yarn), and others will have diameter (knitting needles).

Entity = Table / Tablename
Entity is somewhat of a “data item”. In Magento, those are for example Categories, Products and Customers.

Columns are called attributes in EAV
Attribute = Field / Fieldname
Attribute is again a “data item”, but to differ it from the Entity, in Magento, attributes are for example: Title, Description, Price, etc. for each Product entity

Value = Field Value
And Value for simplicity of explanation represents “real value” that is attached to Entity’s Attribute

Running Raw SQL Query

Place this function to Resource Model

 
public FUNCTION getMarkers($lat, $lng) {
        /**
         * Search radius in km
         */
	$searchRadius = 10;
 
        /**
         * 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('storelocator/entity');
 
        /**
         * Build the query
         */
        $distanceCalc = 'SQRT(POW(lat - ' . $lat . ', 2) + POW(lng - ' . $lng . ' , 2)) * 100';
 
        $query = 'SELECT ROUND('.$distanceCalc. ',2) AS distance_km, '
            . ' name, lat, lng FROM ' . $table
            . '  WHERE distance_km < ' . $searchRadius;
 
        /**
         * Execute the query and return the results
         */
        RETURN $readConnection->fetchAll($query);
}

How-to get SQL query string from Mage

 
<?php
 
        $collection = Mage::getResourceModel('mybrands/orders')
            ->addFieldToSelect('name')
	    ->addFieldToSelect('order_id');
 
        echo '<pre>';
        print_r($collection->getSelect()->__toString());

How-to magento translate

1. in phtml file do:

<?php echo $this->__('Stores found')?>

2. In Vendor/Module/etc/config.xml do:

    ...
    <frontend>
        <translate>
            <modules>
                <VisionDirect_Globalcollect>
                    <files>
                        <default>VisionDirect_Globalcollect.csv</default>
                    </files>
                </VisionDirect_Globalcollect>
            </modules>
        </translate>
	<routers>
	...
	</routers>
     </frontend>
     ...

3. app/locale/de_DE/VisionDirect_StoreLocator.csv
“My name is Imran”,”Maine name ist Imran”
“That is a wall”,”Dast ist fenster”

4. Switch domain name of your webstie from english to German and see the output

How-to get current locale of the store

<?php echo Mage::getStoreConfig('general/locale/code') ?>

Rendering layouts

hen we do toHtml(), we render only single block, for example in Adminhtml sorting, when we click on field we want to render only single block not whole page

    public function gridAction()
    {
        $this->loadLayout();
//        $this->getResponse()->setBody(
//            $this->getLayout()->createBlock('storelocator/adminhtml_entity_grid')->toHtml()
//        );
//        return $this;
    }

but if we dont do toHtml() and follow with renderLayout() then it renders whole blocks of the page, like this:

    public function indexAction()
    {
        $this->_initAction();
//        $this->_addContent($this->getLayout()->createBlock('storelocator/adminhtml_entity'));
//        $this->renderLayout();
//        return $this;
    }

Block – In Magento, a Block is a piece of your website works “alone”, you can insert just anywhere. In fact, a block is a kind of “mini controller” that you can add anywhere in your site. According to the page on your site, you can assign it different templates based on the view that you want to give it.

Database Connections In Magento

By default, Magento will automatically connect to it’s database and provide two separate resources which you can use to access data: “core_read” and “core_write”.

<?php	
	/**
	 * Get the resource model
	 */
	$resource = Mage::getSingleton('core/resource');
 
	/**
	 * Retrieve the read connection
	 */
	$readConnection = $resource->getConnection('core_read');
 
	/**
	 * Retrieve the write connection
	 */
	$writeConnection = $resource->getConnection('core_write');