Giter Site home page Giter Site logo

fluentpdo's Introduction

FluentPDO

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": {
	...
	"lichtner/fluentpdo": "dev-master"	
}

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).

fluentpdo's People

Contributors

jkufner avatar cj-clx avatar mta59066 avatar pkoutsias avatar

Watchers

Neal Brown avatar James Cloos avatar  avatar Jim avatar Chris Savio avatar Philip Leicht avatar Patrick Donahue avatar Front Gate Tickets avatar  avatar Evan Furman avatar Quin Adney avatar  avatar  avatar  avatar  avatar Brendan Moore avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.