Exemple #1
0
 public static function fromSQL($resource, $sql, $params = [])
 {
     $count = SQL::value(preg_replace(['(SELECT(.+)FROM)i', '(LIMIT\\s+\\d+\\s+)i', '(OFFSET\\s+\\d+\\s+)i'], ['SELECT COUNT(1) FROM', '', ''], $sql . ' '), $params, 0);
     $page = Filter::with(["api.{$resource}.page", "api.page"], max(1, Request::get('page', 1)));
     $limit = Filter::with(["api.{$resource}.limit", "api.limit"], max(1, Request::get('limit', 10)));
     $page = min($page, ceil($count / $limit));
     $offset = max(0, $page - 1);
     $sql = "{$sql} LIMIT {$limit} OFFSET {$offset}";
     return static::wrap($resource, SQL::each($sql, $params), $page, $limit, $count);
 }
                        $validations[] = 'required';
                    }
                    if (substr($e->Type, 0, 4) == 'enum') {
                        $validations[] = 'in_array:[' . str_replace("'", '"', substr($e->Type, 5, -1)) . ']';
                    }
                    $validations = implode(' | ', $validations);
                    $_s = str_repeat(' ', $ml - strlen($e->Field));
                    $d = " {$_s} = " . (is_numeric($e->Default) ? $e->Default : ($e->Default === null ? 'null' : "'{$e->Default}'"));
                    $valids[] = "'{$e->Field}' {$_s} => '{$validations}',\n";
                    $t = $e->Field == $last ? ';' : ',';
                    return "\${$e->Field}{$d}{$t} // {$e->Type}\n";
                }, $fields));
                if ($params) {
                    $params = "  public {$params}";
                }
                $create = str_replace(["CREATE TABLE", "\n"], ["CREATE TABLE IF NOT EXISTS", "\n      "], addslashes(SQL::value("show create table {$table}", [], 1)));
                $valids = rtrim(implode('      ', $valids), "\n");
                $date = date("Y-m-d H:i:s");
                $model->code = <<<EOC
<?php

/**
 * Model class for table {$table}
 * Automatically generated on {$date}
 */

class {$model->name} extends Model {
  const _PRIMARY_KEY_ = '{$table}.{$primary}';

{$params}
Exemple #3
0
 public static function count($where_sql = false, $params = [])
 {
     return (int) SQL::value('SELECT COUNT(1) FROM ' . static::persistenceOptions('table') . ($where_sql ? " where {$where_sql}" : ''), $params);
 }