Skip to content

Commit

Permalink
Merge pull request #41 from skipperbent/v4-development
Browse files Browse the repository at this point in the history
Version 4.1.0
  • Loading branch information
skipperbent authored Dec 11, 2017
2 parents 8903c67 + a0ef29a commit 055dfe0
Show file tree
Hide file tree
Showing 31 changed files with 3,421 additions and 3,371 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 77 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ This library is stable, maintained and are used by many sites, including:
**Requirements:**
- PHP version 7.0 or higher is required.

Versions prior to 3.x are available [here](https://github.com/skipperbent/pixie).

#### Feedback and development

If you are missing a feature, experience problems or have ideas or feedback that you want us to hear, please feel free to create an issue.
Expand All @@ -41,7 +43,7 @@ For example when pushing changes to version 3, the pull request should use the `

- When adding new stuff, please remember to add new unit-tests for the functionality.

#### Credits
#### Credits & features

This project is based on the original [Pixie project by usmanhalalit](https://github.com/usmanhalalit/pixie) but has some extra features like:

Expand All @@ -63,14 +65,10 @@ This project is based on the original [Pixie project by usmanhalalit](https://gi

Most importantly this project is used on many live-sites and maintained.

#### Versions prior to 3.x

Older versions prior to 3.x are available [https://github.com/skipperbent/pixie](https://github.com/skipperbent/pixie).

#### Note

`AliasFacade` used for calling the database-connection as a fixed constant has been removed to increase performance.
If this feature is required in your setup we encourage you to implement your own solution.
`Facades` and `Container` support has been removed to increase performance. To implement your own adapters, please extends the
`IConnectionAdapter` interface.

## Example
```php
Expand All @@ -79,7 +77,7 @@ require 'vendor/autoload.php';

// Create a connection, once only.
$config = array(
'driver' => 'mysql', // Db driver
'driver' => 'mysql', // Db driver or IConnectionAdapter class
'host' => 'localhost',
'database' => 'your-database',
'username' => 'root',
Expand Down Expand Up @@ -266,17 +264,23 @@ $queryBuilder
->join('table2', 'table2.person_id', '=', 'foo1.id');
```

You can change the alias anytime by using
You can change the alias anytime by using:

```php
$queryBuilder->alias($table, $alias);
$queryBuilder->alias('foo1', 'table1');

// Simplified way...

$queryBuilder->table('table1')->alias('foo1');
```

**Note:** If `$table` parameter is null - the querybuilder will use the table from latest call to `table($table)` method.

Output:

```sql
SELECT *
FROM `table1` AS foo1
FROM `table1` AS `foo1`
INNER JOIN `cb_table2` ON `cb_table2`.`person_id` = `cb_foo1`.`id`
```

Expand Down Expand Up @@ -704,20 +708,22 @@ are made.
Here's a basic transaction:

```php
$queryBuilder->transaction(function (QueryBuilderHandler $qb) {
$qb
->table('my_table')
->insert(array(
'name' => 'Test',
'url' => 'example.com'
);

$qb
->table('my_table')
->insert(array(
'name' => 'Test2',
'url' => 'example.com'
));
$queryBuilder
->transaction(function (Transaction $transaction) {

$transaction
->table('my_table')
->insert(array(
'name' => 'Test',
'url' => 'example.com'
);

$transaction
->table('my_table')
->insert(array(
'name' => 'Test2',
'url' => 'example.com'
));
});
```

Expand All @@ -730,22 +736,46 @@ If you wish to manually commit or rollback your changes, you can use the

```php
$queryBuilder
->transaction(function (qb)
->transaction(function (Transaction $transaction)
{
$queryBuilder
$transaction
->table('my_table')
->insert($data);

// Commit changes (data will be saved)

$queryBuilder->commit();
$transaction->commit();

// Rollback changes (data would be rejected)
$queryBuilder->rollback();
$transaction->rollback();
}
);
```

Transactions will automatically be used when inserting multiple records. For example:

```php
$queryBuilder->table('people')->insert([
[
'name' => 'Simon',
'age' => 12,
'awesome' => true,
'nickname' => 'ponylover94',
],
[
'name' => 'Peter',
'age' => 40,
'awesome' => false,
'nickname' => null,
],
[
'name' => 'Bobby',
'age' => 20,
'awesome' => true,
'nickname' => 'peter',
],
]);
```

### Get Built Query

Sometimes you may need to get the query string, it's possible.
Expand Down Expand Up @@ -876,21 +906,23 @@ Pixie comes with powerful query events to supercharge your application. These ev

#### Available Events

- after-*
- before-*
- before-select
- after-select
- before-insert
- after-insert
- before-update
- after-update
- before-delete
- after-delete
| Event constant | Event value/name | Description |
| :------------------------------------ | :------------- | :------------ |
| `EventHandler::EVENT_BEFORE_ALL` | `before-*` | Event-type that fires before each query. |
| `EventHandler::EVENT_AFTER_ALL` | `after-*` | Event-type that fires after each query. |
| `EventHandler::EVENT_BEFORE_SELECT` | `before-select` | Event-type that fires before select query. |
| `EventHandler::EVENT_AFTER_SELECT` | `after-select` | Event-type that fires after insert query. |
| `EventHandler::EVENT_BEFORE_INSERT` | `before-insert` | Event-type that fires before insert query |
| `EventHandler::EVENT_AFTER_INSERT` | `after-insert` | Event-type that fires after insert query. |
| `EventHandler::EVENT_BEFORE_UPDATE` | `before-update` | Event-type that fires before update query. |
| `EventHandler::EVENT_AFTER_UPDATE` | `after-update` | Event-type that fires after update query. |
| `EventHandler::EVENT_BEFORE_DELETE` | `before-delete` | Event-type that fires before delete query. |
| `EventHandler::EVENT_AFTER_DELETE` | `after-delete` | Event-type that fires after delete query. |

#### Registering Events

```php
$queryBuilder->registerEvent('before-select', 'users', function(QueryBuilderHandler $qb)
$queryBuilder->registerEvent(EventHandler::EVENT_BEFORE_SELECT, 'users', function(QueryBuilderHandler $qb)
{
$qb->where('status', '!=', 'banned');
});
Expand All @@ -906,7 +938,7 @@ If you want the event to be performed when **any table is being queried**, provi
After inserting data into `my_table`, details will be inserted into another table

```php
$queryBuilder->registerEvent('after-insert', 'my_table', function(QueryBuilderHandler $qb, $insertId)
$queryBuilder->registerEvent(EventHandler::EVENT_AFTER_INSERT, 'my_table', function(QueryBuilderHandler $qb, $insertId)
{
$qb
->table('person_details')->insert(array(
Expand All @@ -920,7 +952,7 @@ $queryBuilder->registerEvent('after-insert', 'my_table', function(QueryBuilderHa
Whenever data is inserted into `person_details` table, set the timestamp field `created_at`, so we don't have to specify it everywhere:

```php
$queryBuilder->registerEvent('after-insert', 'person_details', function(QueryBuilderHandler $qb, $insertId)
$queryBuilder->registerEvent(EventHandler::EVENT_AFTER_INSERT, 'person_details', function(QueryBuilderHandler $qb, $insertId)
{
$qb
->table('person_details')
Expand All @@ -934,7 +966,7 @@ $queryBuilder->registerEvent('after-insert', 'person_details', function(QueryBui
After deleting from `my_table` delete the relations:

```php
$queryBuilder->registerEvent('after-delete', 'my_table', function(QueryBuilderHandler $qb, $queryObject)
$queryBuilder->registerEvent(EventHandler::EVENT_AFTER_DELETE, 'my_table', function(QueryBuilderHandler $qb, $queryObject)
{
$bindings = $queryObject->getBindings();
$qb
Expand All @@ -958,7 +990,7 @@ Only on `after-*` events you get three parameters: **first** is the query builde
#### Removing Events

```php
$queryBuilder->removeEvent('event-name', 'table-name');
$queryBuilder->removeEvent($event, $table = null);
```

#### Some Use Cases
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
}
],
"require": {
"php": ">=7.0",
"usmanhalalit/viocon": "1.0.1"
"php": ">=7.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
Expand Down
Loading

0 comments on commit 055dfe0

Please sign in to comment.