Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Retrieving class attribute data, per product

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 07-04-2019, 11:26 AM
 
dotancohen dotancohen is offline
 

Newbie
  
Join Date: Jun 2019
Posts: 2
 

Default Retrieving class attribute data, per product

I have added a product class "Foo" and that product class has a text attribute "bar". I can set a different value for "bar" for each product under the "Attributes -> Global" tab.

Now in a decorator for \XLite\Model\OrderItem->isValid() I would like to retrieve the value of "foo" for the item in question. How is this done?

I have tried several methods of accessing the database via getRepo(), but in no instance could I get the class attributes for a specific Product.

For instance, this returns all the class attributes for all the products, with no way to then loop for the correct product as there is nothing to identify the product by:
Code:
use XLite\Core\Database; use XLite\Core\CommonCell; $product = $this->getProduct(); $cnd = new CommonCell(); $cnd->productClass = $product->getProductClass(); $attributes = Database::getRepo('XLite\Model\Attribute')->search($cnd);


This returns the product-specific attributes for the correct product, but not the class attributes:
Code:
$cnd = new CommonCell(); $cnd->product = $this->getProduct(); $attributes = Database::getRepo('XLite\Model\Attribute')->search($cnd);


This returns the global attributes for all products, but not the class attributes, and anyway there is no way to tell which attributes belong to which products:
Code:
$cnd = new CommonCell(); $attributes = Database::getRepo('XLite\Model\Attribute')->search($cnd);

I did manage to get the data using an SQL query in the MySQL CLI:
Code:
SELECT avs.product_id, at.name as attribute, aot.name as value FROM xc_attribute_option_translations aot INNER JOIN xc_attribute_options ao ON aot.label_id=ao.id INNER JOIN xc_attribute_translations at ON ao.attribute_id=at.id INNER JOIN xc_attribute_values_select avs ON aot.label_id=avs.attribute_option_id WHERE at.name='bar' AND avs.product_id=5;

However, now how do I transform that SQL query into something that I can run in X-Cart?

I tried using Doctrine joins, but I have been unable to properly set up the class type hinting. If we look at just the first JOIN from above, this is my failed attempt to convert that to Doctrine:

Code:
$result = Database::getRepo('\\XLite\\Model\\AttributeOptionTranslation') ->createQueryBuilder('aot') ->linkLeft('aot.attribute_option', 'i') ->getResult();

I think that I did decorate the AttributeOptionTranslation class properly:
Code:
<?php namespace XLite\Module\DotanCohen\Testing\Model; class AttributeOptionTranslation extends \XLite\Model\AttributeOptionTranslation implements \XLite\Base\IDecorator { /** * Attribute option * @var \Doctrine\Common\Collections\Collection * @ManyToMany (targetEntity="XLite\Model\AttributeOption", mappedBy="label", cascade={"all"}) */ protected $attribute_option; }

Alas, I get this error:
Code:
ERROR: "0" (code N/A) Type of association must be one of *_TO_ONE or MANY_TO_MANY


My original intention was to use OneToMany but that returned the same error.

So my questions are:
1) Is there a proper way to retrieve the class attribute data, given a specific Product object or product_id integer?
2) If not, then how can I convert the SQL query into a Doctrine query?

Thank you
__________________
The requested URL /VERSION was not found on this server.
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 03:51 PM.

   

 
X-Cart forums © 2001-2020