6

I'd like to create a custom model that connects to an API instead of the Magento database. It would also be nice to be able to use (or extend) Magento's collection classes. We created a custom module not so long ago that stores information in the database but we're moving this sub-system outside of Magento and making it accessible through an API to Magento and other apps.

If I can somehow extend Magento's core abstract classes (or maybe inplement some interfaces) I think I can manage to swap my model classes without changing most of the other code that already exists in controllers, blocks, views, etc. Right now, I've created a class that connects to the API and makes the returned data accessible in an object, and a (sort of) collection class that is iterable, but I have to go through the existing code to change how everything works because these are not children of Mage_Core_Model_Abstract.

Has anyone done this?

Is this viable? I wonder if implementing these clases would take too much work, maybe nearly as much as just rewriting them from scratch.

5

The answer is Yes! It is possible. The resource models inside Magento are responsible for the CRUD operations of a module. So, based upon that, all you have to do is leverage the save(), delete(), and load() methods inside of your resource model and collection, following the same definition of each method from its parent.

You can still use Mage_Core_Model_Abstract, Mage_Core_Model_Resource_Db_Abstract, and Mage_Core_Model_Resource_Db_Collection_Abstract.

Make sure you are not calling parent methods in overridden method definitions. You are replacing functionality, not appending it.

Here is an oversimplified example:

Model

You won't really have to do anything different here. The CRUD operations in the model will reach out to the resource model.

class Coolryan_Module_Model_Example extends Mage_Core_Model_Abstract {

    protected function _construct() {
        $this->_init('coolryan_module/example'); // define your resource model
    }
}

Resource Model

The resource model is where you will implement most of your functionality.

class Coolryan_Module_Model_Resource_Example extends Mage_Core_Model_Resource_Db_Abstract {

    protected function _construct() {
        // you may want to connect to your api here...
    }

    public function save(Mage_Core_Model_Abstract $object) {
        // save operations here
        // make sure to update your model with the data!
    }

    public function load(Mage_Core_Model_Abstract $object, $value, $field = null) {
        // loading operations here
        // make sure to update your model with the data!
    }

    public function delete(Mage_Core_Model_Abstract $object) {
        // delete operations here
    }
}

Resource Collection

Collections are pretty straightforward. The only method you will need to override by default is the load() method. The save operation is not necessary, due to the fact that saving iterates over each item in the collection and calls the save() method against each of them.

class Coolryan_Module_Model_Resource_Example_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {

    public function load($printQuery = false, $logQuery = false) {
        // load collections of data here...
    }
}

I hope this helps! Happy coding!

  • 1
    Man, this sounds way easier. I ended up doing something different which is basically rewriting them from scratch but I've only implemented fetching. Now I'll have to drop all that work because this makes a lot more sense xD Oh well. Occupational hazards. – Vic Aug 12 '15 at 16:06

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.