Skip to content

dczech/fluentpdo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FluentPDO (pgsql)

Build Status

FluentPDO - smart SQL builder for PHP.

FluentPDO is small PHP library for rapid query building. Killer feature is "Smart join builder" which generates joins automatically.

Features

  • Fluent interface for creating queries step by step
  • Smart join builder
  • Simple API based on PDO and SQL syntax
  • Build SELECT, INSERT, UPDATE & DELETE queries
  • Small and fast
  • Type hinting with code completion in smart IDEs
  • Requires PHP 5.1+ with any database supported by PDO

Install

Composer

The preferred way to install FluentPDO is via composer.

Add in your composer.json:

"require": {
	...
	"dczech/fluentpdo": "dev-pgsql"	
}

then update your dependencies with composer update.

Copy

If you are not familiar with composer just copy /FluentPDO directory into your libs/ directory then:

include "libs/FluentPDO/FluentPDO.php";

Start usage

$pdo = new PDO("mysql:dbname=fblog", "root");
$fpdo = new FluentPDO($pdo);

First example

FluentPDO is easy to use:

$query = $fpdo->from('article')
            ->where('published_at > ?', $date)
            ->orderBy('published_at DESC')
            ->limit(5);
if ($user_id) {
    $query = $query
            ->where('user_id', $user_id)
            ->select('user.name');        // this join table user
}
foreach ($query as $row) {
    echo "$row[name] - $row[title]\n";
}

And executed query is:

SELECT article.*, user.name
FROM article
        LEFT JOIN user ON user.id = article.user_id
WHERE published_at > ? AND user_id = ?
ORDER BY published_at DESC
LIMIT 5

Full documentation can be found on the FluentPDO homepage

Simple Query Examples

SELECT
$query = $fpdo->from('article')->orderBy('published_at DESC')->limit(5);
// or if you want to one row by primary key
$query = $fpdo->from('user', 2);
INSERT
$values = array('title' => 'article 1', 'content' => 'content 1');
$query = $fpdo->insertInto('article')->values($values);
// or shortly
$query = $fpdo->insertInto('article', $values);
UPDATE
$set = array('published_at' => new FluentLiteral('NOW()'));
$query = $fpdo->update('article')->set($set)->where('id', 1);
// or shortly
$query = $fpdo->update('article', $set, 'id', 1);
DELETE
$query = $fpdo->deleteFrom('article')->where('id', 1);
// or shortly
$query = $fpdo->deleteFrom('article', 'id', 1);

Note: INSERT, UPDATE and DELETE will be executed after ->execute():

$fpdo->deleteFrom('article', 'id', 1)->execute();

Full documentation can be found on the FluentPDO homepage

Licence

Free for commercial and non-commercial use (Apache License or GPL).

About

FluentPDO, smart SQL builder for PHP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • PHP 100.0%