Question

I have a custom model and resource model. I want to load a single instance of the model using more than 1 field.

The model has the following fields:

id
tag_name
custom_name
group_name

I want to load this model based on on tag_name, custom_name and group_name instead of id.

Currently i am using a collection and addFilter for each field. This works, but i wondered if there is a standard strategy for this type of thing in Magento?

EDIT

Core magento seems not to use collections for this scenario but instead it uses direct sql queries in the resource models.

an example of this is:

loadByAccountAndDate() in Mage_Paypal_Model_Resource_Report_Settlement

Is there a reason for this, when collections seem to be a more concise way, in terms of amount of code to be written

I just dont know why magento chooses to do it this way

Was it helpful?

Solution

I think this is a good approach. Maybe you need to create a wrapper in the model class so you will avoid writing the same thing over and over again.
Something like:

public function loadByMultiple($tag, $customName, $group){
    $collection = $this->getCollection()
            ->addFieldToFilter('tag_name', $tag)
            ->addFieldToFilter('custom_name', $customName)
            ->addFieldToFilter('group_name', $group);
    return $collection->getFirstItem();
}

And you can load the item like this in any other place:

$model = Mage::getModel('model/class_here')->loadByMultiple($tag, $customName, $group);
if ($model->getId()){
   //the instance exists
}
else{
    //not found
}

OTHER TIPS

Module/Model/SomeModel.php

public function loadByAttributes($attributes)
{
    $this->setData($this->getResource()->loadByAttributes($attributes));
    return $this;
}

Module/Model/Resource/SomeModel.php:

public function loadByAttributes($attributes)
    {
        $adapter = $this->_getReadAdapter();
        $where   = array();
        foreach ($attributes as $attributeCode=> $value) {
            $where[] = sprintf('%s=:%s', $attributeCode, $attributeCode);
        }
        $select = $adapter->select()
            ->from($this->getMainTable())
            ->where(implode(' AND ', $where));

        $binds = $attributes;

        return $adapter->fetchRow($select, $binds);
    }

And finally you can load model following:

$attributes = array('tag_name'=> 'any', 'custome_name'=> 'some','group_name'=>'some');
$model      = Mage::getModel('module/somemodel')->loadByAttributes($attributes);

Updated

By the way you can use this (loadByAttributes) method easily rather than collection and it is more understandable. Magento also dispatches some events while loading collection or entity and third party extension can update collection or entity by observer. If you load the entity via resource (given in example of mine and yours) no event/observers fire and you can get "clean" entity faster rather than collection. Also Magento not uses cached collection by this way, it loads it directly from db table.
Maybe that's reason of using this method by Magento core modules.

You are doing it right with addFilter. In Magento you can load by any attribute but not multiple attributes at once. By adding filters you achieve the same effect with no extra overhead.

Firstly - your strategy to filter a collection is correct. Because collections in Magento lazy-load you have the ability to create methods in your resource model to more tightly define the requirements of your custom load.

Without some of your code to sample, consider the following pseudo-method in your Resource Model:

<?php


class Marty_Wallace_Model_Resource_Method extends Mage_Core_Model_Resource_Db_Abstract{

    protected function _construct()
    {
        $this->_init('yourmodel/table', 'entity_id');
    }

    public function loadByCriteria(array $filter)
    {

        //$filter should be array('columnname'=>'value','columname'=>'value')

        $collection = Mage::getModel('yourmodel/class')->getCollection();

        foreach($filter as $column=>$value){
            $collection->addFieldToFilter($column,$value);
        }

        return $collection;

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top