i’m new to magento, not programming.
can someone explain why the following code returns a different result depending on where it’s called?
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollection->create();
$sql = $collection->setStore(1)
->addCategoriesFilter(['in' => 11])
->addAttributeToFilter('brand', array('eq' => 5460))
->getSelect();
echo $sql;
returns this when called from within my module
SELECT 1 AS `status`, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`created_at`, `e`.`updated_at`, `e`.`sku` FROM `catalog_product_flat_1` AS `e` WHERE (e.entity_id IN((SELECT `cat`.`product_id` FROM `catalog_category_product` AS `cat` WHERE (cat.category_id IN('11')))))
but
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollection->create();
$sql = $collection->setStore(1)
->addCategoriesFilter(['in' => 11])
->addAttributeToFilter('brand', array('eq' => 5460))
->getSelect();
echo $sql.',';
returns this from a standalone script
SELECT `e`.*, `at_brand`.`value` AS `brand` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_product_entity_int` AS `at_brand` ON (`at_brand`.`entity_id` = `e`.`entity_id`) AND (`at_brand`.`attribute_id` = '173') AND (`at_brand`.`store_id` = 0) WHERE (e.entity_id IN((SELECT `cat`.`product_id` FROM `catalog_category_product` AS `cat` WHERE (cat.category_id IN(11))))) AND (at_brand.value = 5460)
as you can see, when run standalone it applies the additional filters, but when ran in the module it doesn’t.
It’s probably some simple fundamental of magento that i haven’t grasped yet but that’s why i’m asking 🙂
thanks