Getting A Database Adapter (Read or Write)
To read:
Mage::getSingleton('core/resource')->getConnection('core_read');
To write:
Mage::getSingleton('core/resource')->getConnection('core_write');
Related cards:
Enabling MySQL Query Logging
In /lib/Varien/Db/Adapter/Pdo/Mysql.php change this property to true:
/**
* Write SQL debug data to file
*
* @var bool
*/
protected $_debug = false;
To enable logging for queries slower than a certain ti...
Getting A Products URL
Potentially confusing due to the 3 methods you could use, all of which are in Mage_Catalog_Model_Product
:
public function getUrlPath($category=null)
public function getUrlInStore($params = array())
public function getProductUrl($use...
Show All Indexes On a DB
Again, not Magento. Very handy though!
SELECT DISTINCT
TABLE_NAME,
INDEX_NAME
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'your_schema';
Then you can use show indexes from table_name
to see more info abo...
Fetching All Attributes For An Entity From The DB In One Query
Using the database to inspect EAV-based entity types can be a real pain in the ass, so here are a bunch of queries that let you inspect some popular EAV entity types easily. This practice can be applied to any EAV entity type, however as there are...
Using Design Exceptions To Serve a Mobile Theme
In order to serve a different package, or any element of a theme (translations, templates, skin or layout) you can use design exceptions, which allow you to specify different values for each of the design-related settings by using user-agent strin...
Adding A CSS Class To The Body Tag
To add a class via layout XML:
<reference name="root">
<action method="addBodyClass"><classname>whatever</classname></action>
</reference>
Or to add via code, from a controller or block
if ($root = $this->getLayout()->getBlo...
Adding A Layout Handle From A Controller
Doing this is a slight pain, to quote what Alan Storm said on StackOverflow:
- If you add your handle before you call $this->loadLayout() from a controll...
Creating a Datetime attribute that stores time
Magento has a Datetime backend model but formats a Zend_Date
object without the time. So if you want to store datetime (actually with time) you will need to setup your own backend model.
Here is how i've done it:
class Namespace_Module_Mod...
Database Schema
This site shows details of the Magento database schema for differing versions - it shows foreign key relationships in a nice drag-and-drop interface