简体   繁体   中英

Fatal error: Call to a member function addFieldToFilter on a non-object

I created a custom module. The call which is failing is:

$collection = Mage::getModel('[modulename]/[model]')->getCollection(); 
$collection->addFieldToFilter('is_public', 1)->getSelect();
$collection = Mage::getModel('[modulename]/[model]')->getCollection()->getFirstItem();//

Obviously, I screwed something up in my models. Here they are:

Model/[Model].php

class [namespace]_[modulename]_Model_[model] extends Mage_Core_Model_Abstract {
     public function _construct() {
         parent::_construct();
         $this->_init('[modulename]/[model]');
     }   
}

Model/Mysql4/[Model].php

class [namespace]_[modulename]_Model_Mysql4_[model] extends Mage_Core_Model_Mysql4_Abstract        {
    public function _construct() {
       $this->_init('[modulename]/[model]', 'id');
     }   
 }

Model/Mysql4/[Model]/Collection.php

class [namespace]_[modulename]_Mysql4_[model]_Collection extends
Mage_Core_Model_Mysql4_Collection_Abstract {
       protected function _construct() {
          //parent::_construct();
          $this->_init('[modulename]/[model]');
       }   
}

The collection works, sort of.

$collection = Mage::getModel('[modulename]/[model]')->getCollection()->getData();

The code above returns all rows in the table. It fails as soon as I try to apply another function to it. From what I've read addFieldToFilter should just work for a properly set up collection.

This is in magento. Thanks for your help!

Check the following:

  1. What is the class of your Object in question and check if this class has getFirstItem() function in place.

Note: abstract class Mage_Core_Model_Resource_Db_Collection_Abstract extends Varien_Data_Collection_Db does not have getFirstItem()

class Varien_Data_Collection implements IteratorAggregate, Countable

has this function.

So check as stated above what is the class of your Object class in question.

Paste your specific code lines where this error is coming from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM