|
1 | 1 | # Pixie WPDB Query Builder for WordPress |
2 | 2 |
|
3 | | - |
4 | | - |
5 | | -[]() |
| 3 | +[](https://github.com/gin0115/pixie-wpdb/releases) |
6 | 4 |  |
7 | 5 | [](https://codecov.io/gh/gin0115/pixie-wpdb) |
8 | 6 | [](https://scrutinizer-ci.com/g/gin0115/pixie-wpdb/?branch=master) |
| 7 | +[](https://github.com/gin0115/pixie-wpdb/issues) |
| 8 | +[]() |
9 | 9 |
|
| 10 | +An expressive, query builder for WordPRess it can also be referred as a Database Abstraction Layer. Pixie WPDB supports WPDB ONLY and it takes care of query sanitization, table prefixing and many other things with a unified API. |
| 11 | + |
| 12 | +> **Pixie WPDB** is an adaption of `pixie` originally written by [usmanhalalit](https://github.com/usmanhalalit). [Pixie](https://github.com/usmanhalalit/pixie) is no longer under active development. |
| 13 | +
|
| 14 | +# Features |
| 15 | +* [Fluent API](https://github.com/gin0115/pixie-wpdb/wiki/Query%20Methods) |
| 16 | +* [Nested Queries](https://github.com/gin0115/pixie-wpdb/wiki/Sub%20&%20Nested%20Queries) |
| 17 | +* [Multiple Connections](https://github.com/gin0115/pixie-wpdb/wiki/Home#setup-connection) |
| 18 | +* [Sub Queries](https://github.com/gin0115/pixie-wpdb/wiki/Sub%20&%20Nested%20Queries) |
| 19 | +* [JSON Support](https://github.com/gin0115/pixie-wpdb/wiki/Json%20Methods) |
| 20 | +* [Model Hydration](https://github.com/gin0115/pixie-wpdb/wiki/Result%20Hydration) |
| 21 | +* [Custom Alias Facade](https://github.com/gin0115/pixie-wpdb/wiki/Home#connection-alias) |
| 22 | +* [Raw SQL Expressions](https://github.com/gin0115/pixie-wpdb/wiki/Bindings%20&%20Raw%20Expressions) |
| 23 | +* [Value Type Binding](https://github.com/gin0115/pixie-wpdb/wiki/Bindings%20&%20Raw%20Expressions) |
| 24 | +* [Transaction Support](https://github.com/gin0115/pixie-wpdb/wiki/Transactions) |
| 25 | +* [Query Events](https://github.com/gin0115/pixie-wpdb/wiki/Query%20Events) |
| 26 | + |
| 27 | +```php |
| 28 | +$thing = QB::table('someTable')->where('something','=', 'something else')->first(); |
| 29 | +``` |
10 | 30 |
|
11 | | -A lightweight, expressive, query builder for WordPRess it can also be referred as a Database Abstraction Layer. Pixie WPDB supports WPDB ONLY and it takes care of query sanitization, table prefixing and many other things with a unified API. |
| 31 | +# Install |
12 | 32 |
|
13 | | -> **Pixie WPDB** is an adaption of `pixie` originally written by [usmanhalalit](https://github.com/usmanhalalit). [Pixie is not longer under active development ](https://github.com/usmanhalalit/pixie) |
| 33 | +## Perquisites |
14 | 34 |
|
15 | | -## Requirements |
16 | | - - PHP 7.1+ |
17 | | - - MySql 5.7+ or MariaDB 10.2+ |
| 35 | +* WordPress 5.7+ (tested upto 5.9) |
| 36 | +* PHP 7.1+ (includes support for PHP8) |
| 37 | +* MySql 5.7+ or MariaDB 10.2+ |
| 38 | +* Composer (optional) |
18 | 39 |
|
19 | | -> Tested all combinations of PHP 7.1, 7.2, 7.3, 7.4, 8.0, 8.1 with MySql 5.7 & MariaBD 10.2, 10.3, 10.4, 10.5, 10.6, 10.7 |
| 40 | +## Using Composer |
20 | 41 |
|
| 42 | +The easiest way to include Pixie in your project is to use [composer](http://getcomposer.org/doc/00-intro.md#installation-nix). |
21 | 43 |
|
22 | | -It has some advanced features like: |
| 44 | +```bash |
| 45 | +composer require gin0115/pixie-wpdb |
| 46 | +``` |
23 | 47 |
|
24 | | - - Query Events |
25 | | - - Nested Criteria |
26 | | - - Sub Queries |
27 | | - - Multiple Database Connections. |
| 48 | +## Static Loader |
28 | 49 |
|
29 | | -Additional features added to this version of Pixie |
30 | | - - JSON Support (Select, Where) |
31 | | - - Aggregation methods (Min, Max, Average & Sum) |
32 | | - - Custom Model Hydration |
33 | | - - Date based Where (Month, Day, Year & Date) |
| 50 | +If you are planning to just inlcude Pixie direct in your plugin, you can extract the `src` directory and add this to your `functions.php` or similar. |
34 | 51 |
|
35 | | -The syntax is quite similar to Laravel's query builder. |
| 52 | +```php |
| 53 | +require_once '/path/to/src/loader.php'; |
36 | 54 |
|
37 | | -## Example |
38 | | -```PHP |
39 | | -// Make sure you have Composer's autoload file included |
40 | | -require 'vendor/autoload.php'; |
| 55 | +``` |
| 56 | +> Each class is checked if already loaded, to avoid conflicts if used on multiple plugins. |
41 | 57 |
|
42 | | -// Create a connection, once only. |
43 | | -$config = [ |
44 | | - Connection::PREFIX => 'cb_', // Table prefix, optional |
45 | | -]; |
| 58 | +# Setup Connection |
| 59 | + |
| 60 | +If you are only planning on having a single connection, you will only need to configure the connection once. |
46 | 61 |
|
47 | | -// Get the current (gloabl) WPDB instance, or create a custom one |
| 62 | +```php |
| 63 | +# Basic setup |
| 64 | + |
| 65 | +// Access the global WPDB or a custom instance for additional tables. |
48 | 66 | global $wpdb; |
49 | 67 |
|
50 | | -// Give this instance its own custom class alias (for calling statically); |
51 | | -$alias = 'QB'; |
| 68 | +// Configure the builder and/or internal WPDB instance |
| 69 | +$connection_config = [Connection::PREFIX => 'gin0115_']; |
| 70 | + |
| 71 | +// Give a *single* Alias |
| 72 | +$builder_alias = 'Gin0115\\DB'; |
52 | 73 |
|
53 | | -new \Pixie\Connection($wpdb, $config, $alias); |
| 74 | +new Connection( $wpdb, $connection_config, $builder_alias ); |
54 | 75 | ``` |
55 | 76 |
|
56 | | -**Simple Query:** |
| 77 | +This would then give access to an instance of the QueryBuilder using this connection, via the alias defined `Gin0115\DB` |
57 | 78 |
|
58 | | -The query below returns the row where id = 3, null if no rows. |
59 | | -```PHP |
60 | | -$row = QB::table('my_table')->find(3); |
| 79 | +```php |
| 80 | +$foos = Gin0115\DB::table('foo')->where('column', 'red')->get(); |
61 | 81 | ``` |
62 | 82 |
|
63 | | -**Full Queries:** |
| 83 | +> Generated & executed query :: "SELECT * FROM gin0115_foo WHERE column = 'red'; " |
64 | 84 |
|
65 | | -```PHP |
66 | | -$query = QB::table('my_table')->where('name', '=', 'Sana'); |
| 85 | +## Connection Config |
| 86 | + |
| 87 | +It is possible to configure the connection used by your instance of the query builder. |
| 88 | + |
| 89 | +Values |
67 | 90 |
|
68 | | -// Get result |
69 | | -$query->get(); |
| 91 | +| Key | Constant | Value | Description | |
| 92 | +| ----------- | ----------- |----------- |----------- | |
| 93 | +| prefix | Connection:: PREFIX | STRING | Custom table prefix (will ignore WPDB prefix)| |
| 94 | +| use_wpdb_prefix | Connection:: USE_WPDB_PREFIX | BOOL | If true will use WPDB prefix and ignore custom prefix |
| 95 | +| clone_wpdb | Connection:: CLONE_WPDB | BOOL | If true, will clone WPDB to not use reference to the instance (usually the $GLOBAL)| |
| 96 | +| show_errors | Connection:: SHOW_ERRORS | BOOL | If set to true will configure WPDB to show/hide errors | |
| 97 | + |
| 98 | + |
| 99 | +```php |
| 100 | +$config = [ |
| 101 | + Connection::PREFIX => 'acme_', |
| 102 | + Connection::USE_WPDB_PREFIX => true, |
| 103 | + Connection::CLONE_WPDB => true, |
| 104 | + Connection::SHOW_ERRORS => false, |
| 105 | +]; |
70 | 106 | ``` |
71 | 107 |
|
72 | | -**Query Events:** |
| 108 | +> [More details on the config](https://github.com/gin0115/pixie-wpdb/wiki#connection-config) |
| 109 | +
|
| 110 | +## Connection Alias |
73 | 111 |
|
74 | | -After the code below, every time a select query occurs on `users` table, it will add this where criteria, so banned users don't get access. |
| 112 | +When you create a connection: |
75 | 113 |
|
76 | 114 | ```PHP |
77 | | -QB::registerEvent('before-select', 'users', function($qb) |
78 | | -{ |
79 | | - $qb->where('status', '!=', 'banned'); |
80 | | -}); |
| 115 | +new Connection($wpdb, $config, 'MyAlias'); |
81 | 116 | ``` |
82 | 117 |
|
| 118 | +`MyAlias` is the name for the class alias you want to use (like `MyAlias::table(...)` ), you can use whatever name (with Namespace also, `MyNamespace\\MyClass` ) you like or you may skip it if you don't need an alias. Alias gives you the ability to easily access the QueryBuilder class across your application. |
| 119 | + |
| 120 | +# Usage |
| 121 | + |
| 122 | +Once a connection is created, the builder can be accessed either directly using the Alias Facade or by creating an instance. |
| 123 | + |
| 124 | +## Static Usage |
| 125 | + |
| 126 | +The easiest way to use Pixie is to use the alias facade provided. This allows you to access a builder instance anywhere, much like WPDB. |
83 | 127 |
|
84 | | -There are many advanced options which are documented below. Sold? Let's install. |
| 128 | +```php |
| 129 | +// Create the connection early on. |
| 130 | +$connection = new Connection($wpdb, $config, 'Alias'); |
85 | 131 |
|
86 | | -## Installation |
| 132 | +// Insert some data to bar. |
| 133 | +Alias::table('bar')->insert(['column'=>'value']); |
| 134 | +``` |
| 135 | + |
| 136 | +## None Static Usage |
| 137 | + |
| 138 | +When not using an alias you can instantiate the QueryBuilder handler separately, helpful for Dependency Injection and Testing. |
| 139 | + |
| 140 | +```PHP |
| 141 | +// Create connection and builder instance. |
| 142 | +$connection = new Connection($wpdb, $config); |
| 143 | +$qb = new QueryBuilderHandler($connection); |
| 144 | + |
| 145 | +$query = $qb->table('my_table')->where('name', '=', 'Sana'); |
| 146 | +$results = $query->get(); |
| 147 | +``` |
87 | 148 |
|
88 | | -Pixie uses [Composer](http://getcomposer.org/doc/00-intro.md#installation-nix) to make things easy. |
| 149 | +`$connection` here is optional, if not given it will always associate itself to the first connection, but it can be useful when you have multiple database connections. |
89 | 150 |
|
90 | | -To install run `composer require gin0115/pixie-wpdb` |
| 151 | +# Credits |
91 | 152 |
|
92 | | -Library on [Packagist](https://packagist.org/packages/gin0115/pixie-wpdb). |
| 153 | +This package began as a fork of [Pixie](https://github.com/usmanhalalit/pixie) originally written by [usmanhalalit](https://github.com/usmanhalalit) |
| 154 | +A few features have been inspired by the [Pecee-pixie](https://github.com/skipperbent/pecee-pixie/) fork and continuation, especially the extended aggregate methods. |
93 | 155 |
|
94 | | -## Full Usage API |
95 | | -For the full usage docs, please see the wiki. |
96 | 156 |
|
97 | 157 | ## Changelog |
| 158 | +* 0.0.2 - Improvements to the `updateOrInsert()` method |
98 | 159 | * 0.0.1 - Various external and interal changes made to the initial code written by [Muhammad Usman](http://usman.it/) |
99 | 160 | ___ |
100 | 161 | If you find any typo then please edit and send a pull request. |
|
0 commit comments