Magento ORM Allows the Developers to Store/Retrive Data using the load and save methods without writing Direct SQL Query.
However the load Method works only based on ID, To Fetch a Particular row the developer needs to know the id value or fetch all records using magento collection and check if the specified value exists. This approach has a drawback as it fetches all the records instead of a single record. Sometimes we may need to load our model class object based on other fields such as name,mail etc.
To load our model class object based on other fields we need to add some additional methods in our model class.
Step 1: Suppose if we have a custom module and we need to load our Model Class Object based on email or name. Add the corresponding method in your model class.
Here we are just calling our Model Resource class by passing our customfield as parameter. In our case it is email.
Step 2: Now we need to write the actual query in our Model Resource class. A Model Resource class in our custom module is the class which extends Mage_Core_Model_Mysql4_Abstract
Step 3: In the first part we are writing our database query to fetch the required record. The ReadAdapter method returns the connection object and then returns the db select object.
getMainTable will return the database table name,in the where condition we need to specify the database fieldname and corresponding value to be checked.
In the Second part we are using fetchone method to fetch the first column i.e id field of our custom module.
After getting the id field we are loading the model class object and returning it.
Finally we can load our custom module obj as