CakePHP behavior plugin for easily some complicated queries like upsert, bulk upsert and bulk insert.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/Model/Behavior
tests
.gitignore
.semver
.travis.yml
CHANGES.md
LICENSE
README.md
composer.json
phpstan.neon
phpunit.xml.dist

README.md

Easy Query

CakePHP behavior plugin for easily some complicated queries like upsert, bulk upsert and bulk insert.

Build Status codecov Latest Stable Version Total Downloads License

Requirements

  • PHP 7.0+
  • CakePHP 3.5+
  • MySQL 5.6+

Installation

composer require itosho/easy-query

Usage

Upsert

$this->Tags = TableRegistry::get('Tags');
$this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [
    'uniqueColumns' => ['name'],
    'updateColumns' => ['description', 'modified']
]);

$data = [
    'name' => 'cakephp',
    'description' => 'php web framework'
];
$entity = $this->Tags->newEntity($data);
$this->Tags->upsert($entity);

Bulk Upsert

$this->Tags = TableRegistry::get('Tags');
$this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [
    'updateColumns' => ['description', 'modified']
]);

$data = [
    [
        'name' => 'cakephp',
        'description' => 'php web framework'
    ],
    [
        'name' => 'rubyonrails',
        'description' => 'ruby web framework'
    ]
];
$entities = $this->Tags->newEntities($data);
$this->Tags->bulkUpsert($entities);

Bulk Insert

$this->Articles = TableRegistry::get('Articles');
$this->Articles->addBehavior('Itosho/EasyQuery.Insert');

$data = [
    [
        'title' => 'First Article',
        'body' => 'First Article Body',
        'published' => '1'
    ],
    [
        'title' => 'Second Article',
        'body' => 'Second Article Body',
        'published' => '0'
    ]
];
$entities = $this->Articles->newEntities($data);
$this->Articles->bulkInsert($entities);

Advanced

Need to use Timestamp behavior, if you want to update created and modified fields automatically. And you can change the action manually by using event config like this.

// default value is true
$this->Articles->addBehavior('Itosho/EasyQuery.Insert', [
    'event' => ['beforeSave' => false]
]);

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/itosho/easy-query.

License

The plugin is available as open source under the terms of the MIT License.