PHP code example of cocur / nqm

1. Go to this page and download the library: Download cocur/nqm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cocur / nqm example snippets


use Cocur\NQM\NQM;
use Cocur\NQM\QueryLoader\FilesystemQueryLoader;

$loader = new FilesystemQueryLoader(__DIR__.'/queries');
$pdo = new \PDO(...);
$nqm = new NQM($pdo, $loader);

$nqm->getQuery('find-all-users');

$stmt = $nqm->prepare('find-all-users');
$stmt->execute();

$stmt = $nqm->execute('find-user-by-id', [':id' => 42]);

use Cocur\NQM\QueryLoader\CacheQueryLoader;
use Cocur\NQM\QueryLoader\FilesystemQueryLoader;

$loader = new FilesystemQueryLoader(__DIR__.'/queries');
$cache = new CacheQueryLoader($loader);

$pdo = new \PDO(...);
$nqm = new NQM($pdo, $cache);

use Cocur\NQM\QueryLoader\ApcQueryLoader;
use Cocur\NQM\QueryLoader\FilesystemQueryLoader;

$loader = new FilesystemQueryLoader(__DIR__.'/queries');
$apc = new ApcQueryLoader($loader);

$pdo = new \PDO(...);
$nqm = new NQM($pdo, $apc);

use Cocur\NQM\QueryLoader\ApcQueryLoader;
use Cocur\NQM\QueryLoader\CacheQueryLoader;
use Cocur\NQM\QueryLoader\FilesystemQueryLoader;

$loader = new FilesystemQueryLoader(__DIR__.'/queries');
$apc = new ApcQueryLoader($loader);
$cache = new CacheQueryLoader($apc);

$pdo = new \PDO(...);
$nqm = new NQM($pdo, $cache);

use Cocur\NQM\QueryLoader\ArrayQueryLoader;

$loader = new ArrayQueryLoader(['foo' => 'SELECT ...;']);

use Cocur\NQM\QueryCollection;

$collection = NQMQueryCollection($nqm);
// Just prepare the statements
$statements = $collection->prepare('drop-and-create-user-table');

// Prepare and execute the statements
$statements = $collection->execute('drop-and-create-user-table', ['foo'=>'bar']);

$statements->all(); // -> \PDOStatement[]
$statements->first(); // -> \PDOStatement
$statements->last(); // -> \PDOStatement

use Cocur\NQM\Bridge\Doctrine\NQMFactory;

$nqm = NQMFactory::createFromEntityManager($entityManager, $queryLoader);
// NQM(...) object