1

In Magento\Framework\Data\Collection there is a method getFirstItem which calls $this->load()

public function getFirstItem()
{
    $this->load();
...}

load method from the same class:

/**
 * Load data
 *
 * @param bool $printQuery
 * @param bool $logQuery
 * @return $this
 */
public function load($printQuery = false, $logQuery = false)
{
    return $this->loadData($printQuery, $logQuery);
}

loadData from this class:

/**
 * Load data
 *
 * @param bool $printQuery
 * @param bool $logQuery
 * @return $this
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
public function loadData($printQuery = false, $logQuery = false)
{
    return $this;
}

it seems that load does nothing useful but when I comment it out it affects execution of code. How does calling this method trigger any action?

Are there any magic php methods that are executed on return $this;?

1

In collection, load() method does the below things as per my understanding that could help you.

  1. _beforeLoad and _afterLoad event fires.
  2. order and limit set in the query.
  3. Build the collection into object and return

For more you can check the class Magento\Framework\Data\Collection\AbstractDb which extends the Magento\Framework\Data\Collection

  • thanks. Looking at the class Magento\Framework\Data\Collection, it doesn't implement AbstractDb in M2.2.3. Maybe I'm missing something but even if it did how could it run methods from the abstract parent class if they're already defined in the Collection/child class? – Alan Jun 14 '18 at 13:35
  • 1
    No, Actually class AbstractDb extends the class Magento\Framework\Data\Collection so here is AbstractDb is child class. In general, every collection class extents \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection which extends Magento\Framework\Data\Collection\AbstractDb where load() method functionality written. – Prasanta Hatui Jun 14 '18 at 14:35

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.