Giter VIP home page Giter VIP logo

Comments (2)

mvorisek avatar mvorisek commented on September 3, 2024

@mkrecek234 thank you for describing the problem, please add here a small repro code that can be copied and run.

from data.

mkrecek234 avatar mkrecek234 commented on September 3, 2024

Please check this sample code based on a MySQL database as described showing the error:

class Account extends Model
{
    public $table = 'account';

    protected function init(): void
    {
        parent::init();
        $this->addFields(['name', 'fx_rate' => ['type' => 'float']]);
    }
}

class Booking extends Model
{
    public $table = 'booking';

    protected function init(): void
    {
        parent::init();
        $this->addFields(['booking_group', 'value' => ['type' => 'float']]);
        $this->hasOne('account_id', ['model' => [Account::class]])->addField('fx_rate');
    }
}
    public function testAggregate1078(): void
    {
        $m = new Model($this->db, ['table' => 'account']);
        $m->addField('name');
        $m->addField('fx_rate', ['type' => 'float']);
        $this->createMigrator($m)->create();

        $m->import([
            ['name' => 'Test Account', 'fx_rate' => 1.2],
        ]);

        $m = new Model($this->db, ['table' => 'booking']);
        $m->addField('account_id', ['type' => 'integer']);
        $m->addField('booking_group');
        $m->addField('value', ['type' => 'float']);
        $this->createMigrator($m)->create();

        $m->import([
            ['account_id' => 1, 'booking_group' => 'Group1', 'value' => 123],
            ['account_id' => 1, 'booking_group' => 'Group1', 'value' => 456],
            ['account_id' => 1, 'booking_group' => 'Group2', 'value' => 789],
        ]);

        $accountModel = new Account($this->db);
        $bookingModel = new Booking($this->db);

        $bookingAggregate = new AggregateModel($bookingModel);
        $bookingAggregate->setGroupBy(['booking_group'], [
            'total' => ['expr' => 'sum([value])'], ]);

        foreach ($bookingAggregate as $booking) {
            echo 'Result: ' . $booking->get('booking_group') . ' | ' . $booking->get('total');
        }

        echo 'That worked..';

        $bookingAggregate = new AggregateModel($bookingModel);
        $bookingAggregate->setGroupBy(['booking_group'], [
            'total' => ['expr' => 'sum([fx_rate] * [value])'], ]);

        foreach ($bookingAggregate as $booking) {
            echo 'Result: ' . $booking->get('total');
        }
        echo 'That did not work..';
    }

from data.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.