Magento Forum

   
1.4.1.0: Replacement for addExpressionAttributeToSelect in sales/order_collection
 
MageTitan
Member
 
Avatar
Total Posts:  58
Joined:  2009-10-16
Salt Lake City, Utah USA
 

I used to use addExpressionAttribute to pull a custom order report and send the email. The report includes count of sales, total sales amount, so I would do something like this:

$_collection Mage::getResourceModel('sales/order_collection')
        ->
addExpressionAttributeToSelect('total_sales''SUM({{grand_total}})''grand_total')
        ->
addExpressionAttributeToSelect('order_count''COUNT({{grand_total}})''grand_total');

Is there a replacement way of adding custom expressions to the select statement now for the flat sales table?

 
Magento Community Magento Community
Magento Community
Magento Community
 
colinmollenhour
Member
 
Avatar
Total Posts:  50
Joined:  2009-05-01
 

Looks like there is actually another bug in Magento related to this:

app/code/core/Mage grep -rF addExpression .
....
./
Sales/Model/Order/Api.php:            ->addExpressionAttributeToSelect('billing_name',
./
Sales/Model/Order/Api.php:            ->addExpressionAttributeToSelect('shipping_name',
./
Sales/Model/Order/Api/V2.php:            ->addExpressionAttributeToSelect('billing_name',
./
Sales/Model/Order/Api/V2.php:            ->addExpressionAttributeToSelect('shipping_name',
....

These instances also use

Mage::getResourceModel('sales/order_collection')
which as you know no longer contains an addExpressionAttributeToSelect method!! DOH!

However, from what I can tell you *might* be able to use instead

Mage::getModel('sales_entity/order_collection')
since that class does contain that method. However depending on your use case it might break something else to use that.. I have not done any tests yet. If you try this please report back if it worked.

 
Magento Community Magento Community
Magento Community
Magento Community
 
colinmollenhour
Member
 
Avatar
Total Posts:  50
Joined:  2009-05-01
 

Looks like there is a fix in 1.4.1.1 which should be released soon
http://www.magentocommerce.com/bug-tracking/issue?issue=9377

They added a method addExpressionFieldToSelect to the core resource collection model.

/**
     * Add attribute [removed]SUM, COUNT, etc)
     *
     * Example: ('sub_total', 'SUM({{attribute}})', 'revenue')
     * Example: ('sub_total', 'SUM({{revenue}})', 'revenue')
     *
     * For some functions like SUM use groupByAttribute.
     *
     * @param string $alias
     * @param string $expression
     * @param array $fields
     * @return Mage_Eav_Model_Entity_Collection_Abstract
     */
    public function addExpressionFieldToSelect($alias$expression$fields)
    
{
        
// validate alias
        
if(!is_array($fields)) {
            $fields 
= array($fields=>$fields);
        
}

        $fullExpression 
$expression;
        foreach(
$fields as $fieldKey=>$fieldItem{
            $fullExpression 
str_replace('{{' $fieldKey '}}'$fieldItem$fullExpression);
        
}

        $this
->getSelect()->columns(array($alias=>$fullExpression));

        return 
$this;
    
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
retros
Jr. Member
 
Total Posts:  3
Joined:  2010-04-12
 

Some body found the solution for addExpressionFieldToSelect no for addExpressionAttribute
thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
WebBloomer
Jr. Member
 
Avatar
Total Posts:  9
Joined:  2010-01-26
 

I am running 1.4.1.1 and am running into the same problem.  When I try to connect via the magento api for my store, I get this error:
Message from Magento: Call to undefined method Mage_Sales_Model_Mysql4_Order_Collection::addExpressionFieldToSelect()

After reading the above solutions, I’m not quite clear on exactly how to fix the problem.  Can someone tell me exactly what files and parts of the files to replace?  Any additional help here would be hugely appreciated.  Thanks to anyone who might be able to help!  : )

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top