Magento good practices

How can I write this same query, but with magento language ?

        $table = $resource->getTableName('sales/order');

        $total_quadrante = $readConnection->fetchAll('SELECT base_subtotal FROM ' . $table
        . ' WHERE '
        . ' customer_id = ' . (int)$customerId
        . ' AND (created_at BETWEEN '
        . '"' . $date_begin . '"'
        . ' AND '
        . '"' . $date_end . '"' . ')');

And I have this loop to get the sum from the orders from $total_quadrante:

    foreach($collection as $child) {
        $gasto += $child['base_subtotal'];
    }

Solutions

Try this code

$collection = Mage::getModel('sales/order')->getCollection();
$collection->addFieldToSelect('base_subtotal');
$collection->addFieldToFilter('customer_id', array('eq'=> $customerId));
$collection->addFieldToFilter('created_at', array('from' => $date_begin, 'to' => $date_end));

I haven't test the code, try and let me know.

Tags: Magento 1.x / Query

Similar questions

Magento best practices: try / catch in templates
I have a question about best practices in templates. Let's assume I am editing the product details template file. I would like to call a method in the product model which can raise an exception. What is the best way to proceed :
Best practices of 404 product page redirection in magento
We have so many product which are no longer there in our website, but they are already crawled in google. I need to know what are the best practices to redirect them to any other page or something, which will prevent our visitors from 404 error page. Any suggestions on this. Thanks
Any freely available Magento 2 theme's that demonstrate current best practices?
Any freely available Magento 2 theme's that demonstrate the 2.3 best practices? https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/theme-best-practice.html Ideally a theme that modifies the header, footer, and navigation, but even one of those done properly might help. Just to clarify, that means a theme that inherits from blank.
Best practices/methods for adding 100k products in Magento 2?
I have to add 100k products by the end of this week. Most of them are simple products but 30% are configurable products which each have 8 simple products, total 100,000 SKUs. I used Magmi (in Mage 1) which worked quite well for importing CSV files, but with no updated version for Mage 2 it seems most people use default Import functionality in admin...
Magento 2 What is the best practices to override the luma theme
I have created a custom theme, parent theme is luma. I want to override the luma theme and implement a new design on custom theme.I know the _extend.less is the file to write extended css, I have added @layout__max-width:1170px; in _extend.less and it's working, but not sure that how to override the top category navigation, should I override the ex...
Magento 2 Component less/css editing best practices
Please help to understand below 2: 1. How to override component css using less files? 2. Is anyone can provide some of examples to write css in _extend.less file.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.