Magento 1.9 – Fix Call to Undefined Method Collection::getData()

collection;databasemagento-1.9model

I use (Manthan_Marketplace) extension in my Magento store
I want to add a table to this extension
so create a custom table (photos) in Magento database with name:(manthan_marketplace_photos)
Table photos structure:
user_id…………………img_url
(id of user, primary)………….(image name, text)

and edit config.xml in (app\code\community\Manthan\Marketplace\etc\config.xml):

    <models>
        <marketplace>
            <class>Manthan_Marketplace_Model</class>
            <resourceModel>marketplace_mysql4</resourceModel>
        </marketplace>
        <marketplace_mysql4>
            <class>Manthan_Marketplace_Model_Mysql4</class>
            <entities>
                <seller>
                    <table>manthan_marketplace_seller</table>
                </seller>
                <photos>
                   <table>manthan_marketplace_photos</table><= I add this code 
                </photos>
                <vendorproduct>
                    <table>manthan_marketplace_vendorproduct</table>
                </vendorproduct>
                <review>
                    <table>manthan_marketplace_review</table>
                </review>
                <rating>
                    <table>manthan_marketplace_rating</table>
                </rating>
                <sellerrate>
                    <table>manthan_marketplace_seller_rated</table>
                </sellerrate>
                <payment>
                    <table>manthan_marketplace_payment</table>
                </payment>
            </entities>
        </marketplace_mysql4>
    </models>
    <resources>
        <marketplace_setup>
            <setup>
                <module>Manthan_Marketplace</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </marketplace_setup>
        <marketplace_write>
            <connection>
                <use>core_write</use>
            </connection>
        </marketplace_write>
        <marketplace_read>
            <connection>
                <use>core_read</use>
            </connection>
        </marketplace_read>
    </resources>..................some more code  

Then I create a Photos.php in (app\code\community\Manthan\Marketplace\Model):

class Manthan_Marketplace_Model_Photos extends Mage_Core_Model_Abstract {  
        protected function _construct() {
        $this->_init("marketplace/photos");
    }

}

and create Photos.php in(app\code\community\Manthan\Marketplace\Model\Mysql4):

class Manthan_Marketplace_Model_Mysql4_Photos extends Mage_Core_Model_Resource_Db_Abstract {
    protected function _construct() {
        $this->_init('marketplace/photos', 'user_id');
    }

}

and after that create Collection.php in(app\code\community\Manthan\Marketplace\Model\Mysql4\Photos):

class Manthan_Marketplace_Model_Mysql4_Photos_Collection extends Mage_Core_Model_Resource_Db_Abstract {
    public function _construct() {
        $this->_init('marketplace/photos');
    }

}

now when i use tis code :

$img = Mage::getModel('marketplace/photos');
   var_dump($img);  

it return:

object(Manthan_Marketplace_Model_Photos)#60 (15) { ["_eventPrefix":protected]=> string(13) "core_abstract" ["_eventObject":protected]=> string(6) "object" ["_resourceName":protected]=> string(18) "marketplace/photos" ["_resource":protected]=> NULL ["_resourceCollectionName":protected]=> string(29) "marketplace/photos_collection" ["_cacheTag":protected]=> bool(false) ["_dataSaveAllowed":protected]=> bool(true) ["_isObjectNew":protected]=> NULL ["_data":protected]=> array(0) { } ["_hasDataChanges":protected]=> bool(false) ["_origData":protected]=> NULL ["_idFieldName":protected]=> NULL ["_isDeleted":protected]=> bool(false) ["_oldFieldsMap":protected]=> array(0) { } ["_syncFieldsMap":protected]=> array(0) { } }

but when i use this code:

 $result=$img->getCollection()->getData();  

return:

Call to undefined method Manthan_Marketplace_Model_Mysql4_Photos_Collection::getData()

why getData() is undefined for this one but is ok for another model?
thanks a lot for your help!!

Best Answer

Because your collection should extend Mage_Core_Model_Resource_Db_Collection_Abstract, not Mage_Core_Model_Resource_Db_Abstract.

But be careful with Collection::getData(). I described the pitfalls with this method here: How to correctly select the first item from a filtered collection?

getData() on collections works different from other methods. It does not check if the collection is loaded, instead it checks if the _data property is empty. When you load a collection, _data is not populated, only when you call getData()!

[...]

This results in a second database query as soon as you call getData() and this time, your new filter is applied. But it does not change the _items property, so your previously loaded result is still used for everything else.

By the way, I still did not come across any reasonable use case for this method. It's used internally by load() but probably should not have been public. The core doesn't use it from outside the collection class anywhere as far as I see.

Use getItems() instead to inspect the items of the collection.