In general, this article can help you learn and decide which method should be used for which case.
But to directly answer your question, when it comes to product (or any entity) retrieval, using Repository is the best approach.
Let us prove it.
Here is the load function (located in Magento 2 core) that you called by using Factory:
/**
* Load object data
*
* @param integer $modelId
* @param null|string $field
* @return $this
* @deprecated 100.1.0 because entities must not be responsible for their own loading.
* Service contracts should persist entities. Use resource model "load" or collections to implement
* service contract model loading operations.
*/
public function load($modelId, $field = null)
{
$this->_getResource()->load($this, $modelId, $field);
return $this;
}
This function is very straight-forward: it loads the product model and its attribute vales from the database, nothing more.
As you can see this method is deprecated, and the doc block explains why, generally because using this will break the overall architecture, encourage bad practices and present potential issues.
In my experience, always try your best to avoid using deprecated functions or classes, you will thank yourself later on for having done this.
Now let's take a look at the getById in Repository:
public function getById($productId, $editMode = false, $storeId = null, $forceReload = false)
{
$cacheKey = $this->getCacheKey([$editMode, $storeId]);
if (!isset($this->instancesById[$productId][$cacheKey]) || $forceReload) {
$product = $this->productFactory->create();
if ($editMode) {
$product->setData('_edit_mode', true);
}
if ($storeId !== null) {
$product->setData('store_id', $storeId);
}
// Loads the product the same way as Factory does
$product->load($productId);
if (!$product->getId()) {
throw new NoSuchEntityException(
__("The product that was requested doesn't exist. Verify the product and try again.")
);
}
// Caches the loaded product for later quick retrieval
$this->cacheProduct($cacheKey, $product);
}
return $this->instancesById[$productId][$cacheKey];
}
Repository work looks a bit more complicated, but not very. What it does is, it checks if the product with the given ID has already been cached, then it will return the cached product straight away, otherwise load it from the scratch, then cache it once loaded.
Besides from Repository is a highly recommended approach, it allows you to quickly retrieve the same products after their first load, meaning this will technically save you time.
At this point, I can answer to your second question: Yes, you can expect both methods would consume the same DB call. No difference in terms of performance, but after the first load, Repository does not need to make call to DB anymore, instead it looks for result from cache, thus returns faster.
You might wonder, why is the Repository utilizing the deprecated load function from Factory, while I told you to avoid using deprecated functions as of above? Short answer is, this was written by the Magento core team, we should not worry about this too much, unless you want to contribute to the source code and make it better, but what we should pay attention to is the code we implement on our own, always follow best recommendations and guidelines, avoid anything that looks "unsafe".
Since you're new, maybe it's worth having a look at Magento Coding Standards documentation.
Happy coding!
$this->$_modelFactoryinstead of$this->_modelFactory? (without the additional $ sign) – FlorinelChis Apr 07 '23 at 06:55