0

Sorry for asking, but I searched on google, but can't find the answer.

In app/core/Mage/Sales/Model/Convert/Order.php on line 513 there is this rule:

$this->_order = Mage::getModel('sales/order')->load($this->getOnepage()->getQuote()->getId(), 'quote_id');

But where is the second parameter for in the load function? As I know, one parameter gets the ID of the primary field right? Is this second parameter quote_id not the primary key, but it will load the record with $this->getOnepage()->getQuote()->getId()?

Thanks in advance.

2

The short answer is that the second parameter expects a field name that you want to load the product by. This allows you to load it by something other than just id, but it defaults to the id if nothing is provided. More information is below if you want details:

If you look in the module "sales/order" in app/code/core/Mage/Sales/Model/Order.php you'll find that it does not have a load function declared, so we go up the chain of it's inheritance to Mage_Sales_Model_Abstract and it also does not have a load function. Up one more level of inheritance to Mage_Core_Model_Abstract shows us this function:

public function load($id, $field=null)
{
    $this->_beforeLoad($id, $field);
    $this->_getResource()->load($this, $id, $field);
    $this->_afterLoad();
    $this->setOrigData();
    $this->_hasDataChanges = false;
    return $this;
}

If you look at the line that contains _getResource, you'll see that this second parameter is passed to the resource to help load it.

If you load the resource for "sales/order" in app/code/core/Mage/Sales/Resource/Order.php you'll not find a load function, so you must go a couple of steps up the inheritance to Mage_Core_Model_Resource_Db_Abstract to find the load function:

public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
{
    if (is_null($field)) {
        $field = $this->getIdFieldName();
    }

    $read = $this->_getReadAdapter();
    if ($read && !is_null($value)) {
        $select = $this->_getLoadSelect($field, $value, $object);
        $data = $read->fetchRow($select);

        if ($data) {
            $object->setData($data);
        }
    }

    $this->unserializeFields($object);
    $this->_afterLoad($object);

    return $this;
}

You can see in this function that if the field is not set, then it defaults to the id field by using $this->getIdFieldName().

$this->getLoadSelect uses the field to load the object you desire based on that field.

  • Ah clear, thanks. Can't really figure out how you come to the class Mage_Core_Model_Resource_Db_Abstract, but that's probably something to do with the fact I'm new to magento :) – Refilon Nov 18 '15 at 15:26
  • 1
    If you open the resource model we were looking at app/code/core/Mage/Sales/Resource/Order.php you'll not find the function we were looking for. This means it must inherit that function. Looking at the class declaration shows class Mage_Sales_Model_Resource_Order extends Mage_Sales_Model_Resource_Order_Abstract. So, we go to the class it extends and we don't find the function either, so we look and it extends Mage_Core_Model_Resource_Db_Abstract which has our function. – TJ Gamble at Jamersan Nov 18 '15 at 15:49
  • ah okay, clear on that one now as well, thanks! – Refilon Nov 19 '15 at 7:50

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.