atk4\data\Persistence_SQL::action PHP 메소드

action() 공개 메소드

Executing $model->action('update') will call this method.
public action ( Model $m, string $type, array $args = [] ) : atk4\dsql\Query
$m Model
$type string
$args array
리턴 atk4\dsql\Query
    public function action($m, $type, $args = [])
    {
        if (!is_array($args)) {
            throw new Exception(['$args must be an array', 'args' => $args]);
        }
        $q = $this->initQuery($m);
        switch ($type) {
            case 'insert':
                return $q->mode('insert');
                // cannot apply conditions now
            // cannot apply conditions now
            case 'update':
                $q->mode('update');
                break;
            case 'delete':
                $q->mode('delete');
                $this->initQueryConditions($m, $q);
                $m->hook('initSelectQuery', [$q, $type]);
                return $q;
            case 'select':
                $this->initQueryFields($m, $q, isset($args[0]) ? $args[0] : null);
                break;
            case 'count':
                $this->initQueryConditions($m, $q);
                $m->hook('initSelectQuery', [$q]);
                $q->reset('field')->field('count(*)');
                return $q;
            case 'field':
                if (!isset($args[0])) {
                    throw new Exception(['This action requires one argument with field name', 'action' => $type]);
                }
                $field = is_string($args[0]) ? $m->getElement($args[0]) : $args[0];
                $m->hook('initSelectQuery', [$q, $type]);
                $q->reset('field')->field($field);
                $this->initQueryConditions($m, $q);
                $this->setLimitOrder($m, $q);
                return $q;
            case 'fx':
                if (!isset($args[0], $args[1])) {
                    throw new Exception(['fx action needs 2 arguments, eg: ["sum", "amount"]', 'action' => $type]);
                }
                $fx = $args[0];
                $field = is_string($args[1]) ? $m->getElement($args[1]) : $args[1];
                $this->initQueryConditions($m, $q);
                $m->hook('initSelectQuery', [$q, $type]);
                $q->reset('field')->field($q->expr("{$fx}([])", [$field]));
                return $q;
            default:
                throw new Exception(['Unsupported action mode', 'type' => $type]);
        }
        $this->initQueryConditions($m, $q);
        $this->setLimitOrder($m, $q);
        $m->hook('initSelectQuery', [$q, $type]);
        return $q;
    }