Model – Fetch Single Record from Custom Module Table in Magento

modelmodule

I have a custom module with a custom DB. The DB consist of customer_id field and inserted customer_id into it.

How can I get a single record based on customer_id passed, whereby customer_id is the unique field value in my DB table.

For example:

Mage::getModel('module/model')->addFieldToFilter('customer_id', array('eq' => $customerId));

The above code should give me a single record instead of giving a collection.

If I use below code:

Mage::getModel('module/model')->getCollection()->addFieldToFilter('customer_id', array('eq' => $customerId));

it gives me a record in array, and I do not want to use a loop to fetch data.

Is it possible to load data by using my custom DB field name, like

$custommoduleobj= Mage::getModel(‘yourmodulename/modelname’)->loadByCustomerId($customerId);

Please Help.

Thanks.

Best Answer

Try this:

Mage::getModel('module/model')->addFieldToFilter('customer_id', array('eq' => $customerId))->getFirstItem();

or this:

Mage::getModel('module/model')->load($customerId, 'customer_id');