Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builder macros #2

Merged
merged 4 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 1,38 @@
language: php

php:
- 7.0
- 7.1
- 7.2

sudo: true

env:
global:
- DB_PASSWORD=''
- DB_USERNAME=root

cache:
directories:
- $HOME/.composer/cache

before_script:
- travis_retry composer self-update
- travis_retry composer update --no-interaction --prefer-source
- sudo mysql_upgrade
- sudo service mysql restart
- mysql -e 'CREATE DATABASE IF NOT EXISTS dbalcore_testing;'
- composer install --no-interaction
- composer dump-autoload

script:
- vendor/bin/codecept run --coverage --coverage-xml coverage.xml
- vendor/bin/phpunit --coverage-clover=coverage.xml
after_success:
- bash <(curl -s https://codecov.io/bash)
services:
- mysql

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.xml
addons:
apt:
sources:
- mysql-5.7-trusty
packages:
- mysql-server
- mysql-client
131 changes: 131 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 5,134 @@
[![Code Coverage](https://scrutinizer-ci.com/g/Napp/dbalcore/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Napp/dbalcore/?branch=master)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)


This packages extend the Laravel Query Builder, has a nice `Base Repository` and has a collection of helpful `Criteria` to build queries.



## Repositories

The Basereposity has various helpful methods.

### Transactions

```php
return $this->transaction(function () use ($data) {
User::update($data);
});
```


## Criteria

A `Criterion` is a way to build custom query logic in its own class and reuse within your project.
Use it together with the BaseRepository to

```php
$this->criteriaCollection = new CriteriaCollection();
$this->criteriaCollection
->reset()
->add(new WithRelationCriterion('contentGroups'))
->add(new WithRelatedUserCriterion($request->user()))
->add(new WithSearchQueryCriterion('foobar', 'name'));

$forms = $this->formsRepository->getAllMatchingCriteria($this->criteriaCollection);
```


## QueryBuilder Usage


This package extends the Laravel QueryBuilder by the following methods:

### Replace

Makes it possible to use the `REPLACE INTO` MySQL grammar in Laravel. Simply do:

```php
User::replace($data);
```


### insertOnDuplicateKey

Call `insertOnDuplicateKey` or `insertIgnore` from a model with the array of data to insert in its table.

```php
$data = [
['id' => 1, 'name' => 'name1', 'email' => '[email protected]'],
['id' => 2, 'name' => 'name2', 'email' => '[email protected]'],
];

User::insertOnDuplicateKey($data);

User::insertIgnore($data);
```

#### Customizing the ON DUPLICATE KEY UPDATE clause

##### Update only certain columns

If you want to update only certain columns, pass them as the 2nd argument.

```php
User::insertOnDuplicateKey([
'id' => 1,
'name' => 'new name',
'email' => '[email protected]',
], ['name']);
// The name will be updated but not the email.
```

##### Update with custom values

You can customize the value with which the columns will be updated when a row already exists by passing an associative array.

In the following example, if a user with id = 1 doesn't exist, it will be created with name = 'created user'. If it already exists, it will be updated with name = 'updated user'.

```php
User::insertOnDuplicateKey([
'id' => 1,
'name' => 'created user',
], ['name' => 'updated user']);
```

The generated SQL is:

```sql
INSERT INTO `users` (`id`, `name`) VALUES (1, "created user") ON DUPLICATE KEY UPDATE `name` = "updated user"
```

You may combine key/value pairs and column names in the 2nd argument to specify the columns to update with a custom literal or expression or with the default `VALUES(column)`. For example:

```php
User::insertOnDuplicateKey([
'id' => 1,
'name' => 'created user',
'email' => '[email protected]',
'password' => 'secret',
], ['name' => 'updated user', 'email]);
```

will generate

```sql
INSERT INTO `users` (`id`, `name`, `email`, `password`)
VALUES (1, "created user", "[email protected]", "secret")
ON DUPLICATE KEY UPDATE `name` = "updated user", `email` = VALUES(`email`)
```

### Pivot tables

Call `attachOnDuplicateKey` and `attachIgnore` from a `BelongsToMany` relation to run the inserts in its pivot table. You can pass the data in all of the formats accepted by `attach`.

```php
$pivotData = [
1 => ['expires_at' => Carbon::today()],
2 => ['expires_at' => Carbon::tomorrow()],
];

$user->roles()->attachOnDuplicateKey($pivotData);

$user->roles()->attachIgnore($pivotData);
```
Loading