2017 © Pedro Peláez
 

cakephp-plugin strawberry-cake

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

image

itosho/strawberry-cake

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

  • Sunday, September 3, 2017
  • by itosho
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Easy Query

CakePHP behavior plugin for easily generating some complicated queries like (bulk) insert/upsert etc., (*1)

codecov Latest Stable Version Total Downloads License, (*2)

Requirements

  • PHP 8.1+
  • CakePHP 5.0+
  • MySQL 8.0+ / MariaDB 10.4+

Notice

  • For CakePHP4.x, use 3.x tag.
  • For CakePHP3.x, use 1.x tag.

Installation

composer require itosho/easy-query

Usage

Upsert

$this->Tags = TableRegistry::getTableLocator()->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::getTableLocator()->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::getTableLocator()->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);

Insert Select

For inserting a record just once., (*3)

case1

Specify search conditions., (*4)

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

$data = [
    'title' => 'New Article?',
    'body' => 'New Article Body?',
];
$entity = $this->Articles->newEntity($data);
$condition = ['title' => 'New Article?'];

$this->Articles->insertOnce($entities);

Generated SQL is below., (*5)

INSERT INTO articles (title, body)
SELECT 'New Article?', 'New Article Body?' FROM tmp WHERE NOT EXISTS (    
    SELECT * FROM articles WHERE title = 'New Article?'
)

case2

Auto set search conditions with a inserting record., (*6)

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

$data = [
    'title' => 'New Article',
    'body' => 'New Article Body',
];
$entity = $this->Articles->newEntity($data);

$this->Articles->insertOnce($entities);

Generated SQL is below., (*7)

INSERT INTO articles (title, body)
SELECT 'New Article', 'New Article Body' FROM tmp WHERE NOT EXISTS (    
    SELECT * FROM articles WHERE title = 'New Article' AND body = 'New Article Body'
)

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., (*8)

// 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., (*9)

License

The plugin is available as open source under the terms of the MIT License., (*10)

The Versions

03/09 2017

dev-master

9999999-dev https://github.com/itosho/strawberry-cake

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

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp behavior bulk insert upsert

03/09 2017

v0.4.3

0.4.3.0 https://github.com/itosho/strawberry-cake

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

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp behavior bulk insert upsert

03/09 2017

v0.4.2

0.4.2.0 https://github.com/itosho/strawberry-cake

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

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp behavior bulk insert upsert

03/09 2017

v0.4.1

0.4.1.0 https://github.com/itosho/strawberry-cake

CakePHP behavior plugin to simplify some complicated query.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp behavior bulk upsert

03/09 2017

v0.4.0

0.4.0.0 https://github.com/itosho/strawberry-cake

CakePHP behavior plugin to simplify some complicated query.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp behavior bulk upsert

03/09 2017

v0.3.0

0.3.0.0 https://github.com/itosho/strawberry-cake

CakePHP behavior plugin to simplify some complicated query.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp behavior bulk upsert

02/09 2017

v0.2.1

0.2.1.0 https://github.com/itosho/strawberry-cake

CakePHP behavior plugin to simplify some complicated query.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp behavior bulk upsert

02/09 2017

v0.2.0

0.2.0.0 https://github.com/itosho/strawberry-cake

CakePHP behavior plugin to simplify some complicated query.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp behavior bulk upsert