fuel / orm

Fuel PHP Framework - Fuel v1.x ORM
http://fuelphp.com/docs/packages/orm/intro.html
151 stars 95 forks source link

fix select with alias column #448

Closed ysshir closed 3 years ago

ysshir commented 3 years ago

Previous PR https://github.com/fuel/orm/pull/432 doesn't work right now.

sample code.

class Model_Temp extends Orm\Model {

    public static function _init() {
        static::$_table_name = $table = 'temp_' . time();

        DB::query(<<<SQL
                CREATE TEMPORARY TABLE {$table} (
                    `id`            int(11) not null auto_increment,
                    `value`         varchar(255) null,
                    primary key (`id`)
                );
                SQL
        )->execute();
    }

    protected static $_table_name  = null;
    protected static $_primary_key = ['id'];
    protected static $_properties  = [
        'id',
        'value',
    ];
}

Model_Temp::forge(['value' => 10])->save();
Model_Temp::forge(['value' => 20])->save();
Model_Temp::forge(['value' => 30])->save();
Model_Temp::flush_cache();

$data = Model_Temp::query(['select' => [[DB::expr('sum(value)'), 'total']]])
                  ->get_one();

echo $data->total;
ysshir commented 3 years ago

I needed to think about selecting DB::expr() without alias. so sorry...

$data = Model_Temp::query(['select' => [DB::expr('sum(value)'), [DB::expr('sum(value)'), 'total']]])
                  ->get_one();