cancel
Showing results for 
Search instead for 
Did you mean: 

SQL Query for product attributes

SQL Query for product attributes

Hi,
I really need some help with this one, since I spend a lot of time going through the magento database and could not figure out a way to do this.

 

Basically, I am trying to create a small php file that I give it the SKU of a product and it will run an SQL query which gives me the Name, the attribute set and the values of the attributes

 

Ex:
This is the magento setting: http://prntscr.com/qkg45l

 

In the php script it will give me something like this:

  • Color: GY
  • Connection: USB 3.0 Plug A, USB Type-C

My problem is that I could not find a way in the magento database where I can link the results together

 

Till now I have these tables:

  • m2catalog_product_flat_1 – attribute_set_id
  • m2eav_attribute_set – attribute_set_name
3 REPLIES 3

Re: SQL Query for product attributes

I have a same problem...please any solution?

Re: SQL Query for product attributes

Why are you using tables where Magento provide Collection and Module for the product.

 

Instead of using the object manager directly, inject the ProductFactory:

public function __construct(\Magento\Catalog\Model\ProductFactory $productFactory){
    $this->productFactory = $productFactory;
}

Then use it like this:

$product = $this->productFactory->create();
$product->loadByAttribute('sku', $sku);
$productName = $product->getName();
$attribue = $product->getData("<attribute name>");

 

Re: SQL Query for product attributes

Hey @robertbits,

 

First I am not getting which SQL query you need like select, update or delete. But I think if you want to only display the data then try this solution.

 

/**
 * @var \Magento\Catalog\Model\Product\Attribute\Repository $_productAttributeRepository
 */
protected $_productAttributeRepository;

/**
 * ...
 * @param \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository
 * ...
 */
public function __construct(
    ...
    \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository,
    ...
) {
    ...
    $this->_productAttributeRepository = $productAttributeRepository;
    ...
}

Then add this too.

$this->_productAttributeRepository->getCollection()->addFieldToFilter('attribute_set_id',$id)->addFieldToFilter('attribute_set_name',$name);

 

Hope this will help you.

 

Click KUDOS and accept as a solution.

Thank you!