Code Monkey home page Code Monkey logo

easy-query's Introduction

Easy Query

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

codecov Latest Stable Version Total Downloads License

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.

case1

Specify search conditions.

$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.

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.

$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.

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.

// 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.