I've created a Model and a Resource Model. I tried to query for records from the database through the Mage::getModel() function like this:

$model = Mage::getModel('custommodule/mymodel')->getCollection();
$model = $model->addFieldToFilter("model_id", 1);

But I keep getting this error:

Fatal error: Call to a member function addFieldToFilter() on boolean in C:\wamp\www\samplestore\testing.php on line 19

model_id is my primary key. I've no problem retrieving data when I do it this way:

$model = Mage::getModel('custommodule/mymodel');
$model = $model->load(1);

This is how my resource model look:

class TestNS_CustomModule_Model_Resource_MyModel extends Mage_Core_Model_Mysql4_Abstract
    public function _construct()
        $this->_init('custommodule/mymodel', 'model_id');

And this is how my config.xml look:


I can get the data if I do a load(1), but not when I call . It seems like getCollection() is returning a false boolean, why is this so?

Why am I getting an error when calling addFieldToFilter()? If I can't do it this way, how can I add in a "where" clause to select a record from the model?

  • Can you show us resource model? – Adarsh Khatri May 24 '16 at 6:33
  • @AdarshKhatri Sure, I've just updated my question with my resource model. – xenon May 24 '16 at 6:35

I think you have missed a Collection.php file. You need this file in order to get or filter the collection.

class TestNS_CustomModule_Model_Resource_Mymodel_Collection
    public function _construct()

This will make sure your filter works. Good luck.


Your resource model should extend from here:


So your Mymodel.php should be

class TestNS_CustomModule_Model_Resource_Mymodel extends Mage_Core_Model_Resource_Db_Abstract
    public function _construct()
        $this->_init('custommodule/mymodel', 'model_id');
  • Thanks, but I'm still getting the same error after adding in a Collection.php in my Resource folder. – xenon May 24 '16 at 6:44
  • Check the naming, you cannot have MyModel.php, you should have Mymodel.php only. Same applies for all file and folder inside CustomModule (you can have CustomModule). And your extend is wrong. – Adarsh Khatri May 24 '16 at 6:46
  • My resource model and actual mode have the same file name as MyModel.php, but one is in the resource folder while the other is in the Model folder. Is this acceptable? – xenon May 24 '16 at 6:49
  • You should have Mymodel.php and Resource/Mymodel/Collection.php. Also can you show your etc/config.xml too? – Adarsh Khatri May 24 '16 at 6:53
  • @AdarshKahtr, I've added my config.xml to my question too. – xenon May 24 '16 at 7:01

xenon, Try changing in ur resource model file



  • Thanks. I've actually tried this earlier, but I'm getting the same error. :( – xenon May 24 '16 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.