1

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:

<models>
    <custommodule>
        <class>TestNS_CustomModule_Model</class>
        <resourceModel>custommodule_resource</resourceModel>
    </custommodule>
    <custommodule_resource>
        <class>TestNS_CustomModule_Model_Resource</class>
        <entities>
            <mymodel>
                <table>testns_custommodule_moduletable</table>
            </mymodel>
        </entities>
    </custommodule_resource>
</models>

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
1

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
    Mage_Core_Model_Resource_Db_Collection_Abstract
{
    public function _construct()
    {
        $this->_init('custommodule/mymodel');
    }
}

This will make sure your filter works. Good luck.

UPDATE

Your resource model should extend from here:

Mage_Core_Model_Resource_Db_Abstract

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
0

xenon, Try changing in ur resource model file

Mage_Core_Model_Mysql4_Abstract

to

Mage_Core_Model_Resource_Db_Abstract
  • 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.