Code Monkey home page Code Monkey logo

dbalmanager's Introduction

DBALManager

SensioLabsInsight Build Status

Set of helper classes for Doctrine DBAL. It has been made maily to ease creating bulk imports. It provides a method to execute INSERT ... ON DUPLICATE KEY UPDATE query on MySQL-compatible databases, which is what I miss in Doctrine's MySQL driver.

Symfony installation

To use this class in Symfony 2/3, please look at DBALManagerBundle.

In Symfony 4, thanks to autowiring you are not forced to use a bundle, just add these lines in your services.yaml:

services:
    _defaults:
        autowire: true
        
    JarJak\DBALManager: ~

Integration with other frameworks

Run:

composer require jarjak/dbal-manager

The class is PSR-0/PSR-4 compatible, so it can be integrated easily with any modern framework. Here is an example for Silex:

//Application.php

$app['dbal_manager'] = $app->share(function ($app) {
    $manager = new JarJak\DBALManager($app['db']);
    return $manager;
});

Simple example:

You want to insert data or update them if row already exists.

$sqlArray = [
	'id' => 1,
	'username' => 'JohnKennedy',
	'email' => '[email protected]'
];

/* @var $manager JarJak\DBALManager */
$manager->insertOrUpdate('user', $sqlArray);

Or you want to just skip this row if it exists:

$manager->insertIgnore('user', $sqlArray);

Advanced example:

Lets say we have user table with:

  • unique usernames and emails
  • column active can contain only 0 or 1 (not nullable)
  • column address can be null
$sqlArray = [
    'username' => 'JohnKennedy',
    'email' => '[email protected]',
    'password' => $password,
    'address' => '',
    'active' => 0,
];

/* @var $manager JarJak\DBALManager */
$manager->insertOrUpdate('user', $sqlArray, 2, ['active']);

Dumping Queries

DBALManager can use VarDumper to dump SQL queries from QueryBuilder ready to be copypasted into database server (with parameters already included).

/* @var QueryBuilder $queryBuilder */
\JarJak\SqlDumper::dumpQuery($queryBuilder);

If you don't use QueryBuilder you can still dump parametrized SQL with:

\JarJak\SqlDumper::dumpSql($sql, $params);

Testing

Run tests with:

composer run-script test

Fix code style with:

composer run-script csfix

dbalmanager's People

Contributors

jarjak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

michielw

dbalmanager's Issues

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.