1

I have the below collection:

$this->_collection->getSelect()
                  ->order(new \Zend_Db_Expr('price_index.min_price / IF(e.unit_of_sale = NULL, 1, e.unit_of_sale) DESC'));

This is used for sorting by each price on the frontend and worked perfectly while product flat catalog was enabled. Now however with product flat catalog disabled the following error is shown:

Exception #1 (PDOException): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'e.unit_of_sale' in 'order clause'

I'm guessing i need to add the attribute so it can be used for the ORDER clause. I have tried the below however with no luck:

  1. ->addAttributeToSelect('unit_of_sale')
  2. ->addFieldToSelect('unit_of_sale')

Using ->addAttributeToFilter('unit_of_sale') gets rid of the error and so have just created a false filter ->addAttributeToFilter('unit_of_sale', array('neq' => '-1')) so that I can use the attribute for the order but would rather know how to add the attribute for order clause.

How can i add a custom attribute to a collection while flat catalog is disabled?

3 Answers 3

5

Adding the catalog_product_entity_varchar table as a second paramater to the addAttributeToSelect method call seemed to fix my issue:

->addAttributeToSelect('unit_of_sale', 'catalog_product_entity_varchar')
1
  • worked. It seems we have to pass the required table name based on the attribute type as the 2nd parametre Nov 2, 2023 at 11:01
0
            $collection = $productCollection->create()
                    ->addAttributeToFilter('is_featured', array('eq' => 1))
                    ->load();
1
  • Try to add some description to your answer.
    – Priyank
    Oct 26, 2017 at 11:03
0

Second parameter is join type, you can use 'left' to get extra field into the collection. eg. ->addAttributeToSelect(['price','special_price'],'left');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.