from() публичный Метод

Create SELECT query from $table
public from ( string $table, integer $primaryKey = null ) : SelectQuery
$table string - db table name
$primaryKey integer - return one row by primary key
Результат SelectQuery
Пример #1
1
<?php

require 'concatToOneFile.php';
require 'FluentPDO.php';
$fpdo = new FluentPDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');
$q = $fpdo->from('sometable')->where('1=1');
echo $q->getQuery(), "\n";
var_dump($q->fetchAll());
echo "\n\n";
$q = $fpdo->from('sometable')->select('count(1)')->where('1=1');
echo $q->getQuery(), "\n";
var_dump($q->fetchAll());
echo "\n\n";
$q = $fpdo->query('SELECT * FROM sometable WHERE x=?', array('1'));
echo $q->getQuery(), "\n";
var_dump($q->fetchAll());
echo "\n\n";
Пример #2
0
 public function getAll($limit = null, $offset = null, $orderBy = null)
 {
     $query = $this->queryBuilder->from($this->getTableName());
     if (isset($limit)) {
         $query->limit($limit);
     }
     if (isset($offset)) {
         $query->offset($offset);
     }
     if (isset($orderBy)) {
         $query->orderBy($orderBy);
     }
     $dtos = $query->fetchAll();
     $entityClass = $this->getEntityClass();
     $result = [];
     foreach ($dtos as $dto) {
         $result[$dto['id']] = new $entityClass($dto);
     }
     return $result;
 }
Пример #3
0
 /**
  * Performs a select query using FluentPDO
  */
 public function select($table, $where, $select = '*', $asObjects = true)
 {
     if (!$this->isConnected()) {
         static::connect();
     }
     if ('*' !== $select) {
         $result = $this->fpdo->from($table)->where($where)->asObject($asObjects)->fetch($select);
     } else {
         $result = $this->fpdo->from($table)->where($where)->asObject($asObjects)->fetchAll();
     }
     $this->num_queries++;
     return $result;
 }
Пример #4
0
 public function from($table, $primaryKey = null)
 {
     return $this->db->from($table, $primaryKey);
 }
Пример #5
0
// Koristi :
// - SlimPHP - http://www.slimframework.com (kao minimalni frejmork)
// - FluentPDO - http://lichtner.github.io/fluentpdo (za rad sa bazom podataka)
// Ukljuci debug
// ini_set('display_errors',1);
// ini_set('display_startup_errors',1);
// error_reporting(-1);
$app = new \Slim\Slim();
// Dodaj JSON midleware globalno
$app->add(new \SlimJson\Middleware(array('json.status' => true, 'json.override_error' => true, 'json.override_notfound' => true)));
// Povezi se sa bazom
$pdo = new PDO("sqlite:db.sqlite");
$fpdo = new FluentPDO($pdo);
// API za pretragu umetnika
$app->post('/artists/search', function () use($app, $fpdo) {
    $count = $fpdo->from('artist')->where("Name LIKE '%" . $_POST['keyword'] . "%'")->count();
    if (isset($_POST['perPage']) && intval($_POST['perPage']) > 0) {
        $perPage = intval($_POST['perPage']);
    } else {
        $perPage = 5;
    }
    $pageCount = ceil($count / $perPage);
    if (isset($_POST['page']) && intval($_POST['page']) > 0) {
        $page = intval($_POST['page']);
        if ($page > $pageCount) {
            throw new \Exception('Page ' . $page . ' does not exist.');
        }
        $artists = $fpdo->from('artist')->where("Name LIKE '%" . $_POST['keyword'] . "%'")->limit($perPage)->offset(($page - 1) * $perPage)->fetchAll();
    } else {
        $page = 1;
        $artists = $fpdo->from('artist')->where("Name LIKE '%" . $_POST['keyword'] . "%'")->limit($perPage)->fetchAll();
Пример #6
0
<?php

require 'vendor/autoload.php';
// ini_set('display_errors',1);
// ini_set('display_startup_errors',1);
// error_reporting(-1);
$app = new \Slim\Slim();
// Dodaj JSON midleware globalno
$app->add(new \SlimJson\Middleware(array('json.status' => true, 'json.override_error' => true, 'json.override_notfound' => true, 'json.status' => false)));
// DB CONNECT
$pdo = new PDO("sqlite:db.sqlite");
$fpdo = new FluentPDO($pdo);
// GET /persons
$app->get('/persons', function () use($app, $fpdo) {
    $count = $fpdo->from('persons')->count();
    if (isset($_GET['perPage']) && intval($_GET['perPage']) > 0) {
        $perPage = intval($_GET['perPage']);
    } else {
        $perPage = 5;
    }
    $pageCount = ceil($count / $perPage);
    if (isset($_GET['page']) && intval($_GET['page']) > 0) {
        $page = intval($_GET['page']);
        if ($page > $pageCount) {
            throw new \Exception('Page ' . $page . ' does not exist.');
        }
        $persons = $fpdo->from('persons')->limit($perPage)->offset(($page - 1) * $perPage)->fetchAll();
    } else {
        $page = 1;
        $persons = $fpdo->from('persons')->limit($perPage)->fetchAll();
    }
Пример #7
0
<?php

require 'vendor/autoload.php';
require 'Random.php';
// Ukljuci debug
// ini_set('display_errors',1);
// ini_set('display_startup_errors',1);
// error_reporting(-1);
use sweelix\guid\Guid;
use Slim\Slim;
use SlimJson\Middleware;
$app = new Slim();
// Dodaj JSON midleware globalno
$app->add(new Middleware(array('json.status' => true, 'json.override_error' => true, 'json.override_notfound' => true)));
// Povezi se sa bazom
$pdo = new PDO("sqlite:db.sqlite");
$fpdo = new FluentPDO($pdo);
// API za generisanje slucajnog broja
$app->get('/match(/:id)', function ($id = '%NO-DATA%') use($app, $fpdo) {
    if ($id == '%NO-DATA%') {
        $id = Guid::v4();
    }
    $match = $fpdo->from('matches')->where('id', $id)->fetch();
    if (!$match) {
        $data = ['id' => $id, 'player1_id' => NULL, 'player2_id' => NULL, 'data' => implode(';', Random::generate($id, 500, 0, 100))];
        $query = $fpdo->insertInto('matches', $data)->execute();
        $match = $data;
    }
    $app->render(200, $match);
});
$app->run();