PHP code example of olajoscs / querybuilder

1. Go to this page and download the library: Download olajoscs/querybuilder 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/ */

    

olajoscs / querybuilder example snippets


  $myDIContainer->singleton('connection', function() {
    // Create an own config object, which extends the ConnectionConfig abstract class, or implements the Config interface
    $configArray = Factory = new \OlajosCs\QueryBuilder\ConnectionFactory();
    
    // Use the ConnectionFactory to create the Connection object by the PDO
    $connection = $connectionFactory->create($pdo);
    
    return $connection;
  });

  $connection->transaction(function() use ($connection) {
    $connection->get(...);
  });

  $connection
    ->select(['field', 'list'])
    ->from('table_name')
    ->where('field', '=', $value)
    ->groupBy('field')
    ->orderBy('field')
    ->get();

  $connection
    ->update('table_name')
    ->set(
      [
        'value1' => 1,
        'value2' => 2
      ]
    )
    ->where('id', '=', 1)
    ->execute();

  $connection
    ->insert(
      [
        'field1' => 1,
        'field2' => 2
      ]
    )
    ->into('table_name')
    ->execute();  
  // OR
  $connection
    ->insert()  
    ->into('table_name')
    ->values(  
      [
        'field1' => 1,
        'field2' => 2
      ]
    )
    ->execute();  

  $connection
    ->insert(
      [
        [
          'field1' => 1,
          'field2' => 2
        ],
        [
          'field1' => 3,
          'field2' => 4
        ],
      ] 
    )
    ->into('table_name')
    ->execute();

  $connection
    ->delete()
    ->from('table_name')
    ->where('id', '=', 1)
    ->execute();

  // for MySQL
  $connection
    ->select($connection->createRawExpression('count(1) as counter'))
    ->from('tests')
    ->getOneField('counter');

  $connection
    ->select() // empty means *
    ->from('tests')
    ->where($connection->createRawExpression('select count....... = 1')
    ->get();