PHP code example of stormmore / queries

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

    

stormmore / queries example snippets


use Stormmore\Queries\ConnectionFactory;
use Stormmore\Queries\StormQueries;

$connection = ConnectionFactory::createFromString("dsn", "user", "password");
$queries = new StormQueries($connection);

// Select
$product = $queries->find('products', ['id' => 5]);

// Insert
$id = $queries->insert('products', [
    'name' => 'Golden watch',
    'price' => 465
]);

// Update
$queries->update('products', ['id' => $id], ['name' => 'Renamed product']);

// Delete
$queries->delete('products', ['id' => $id]);

$product = $queries->find('products', ['id' => 5]);

$products = $queries->findAll('products', ['category_id' => 10]);

$count = $queries->count('products', ['in_sale' => true]);

$exists = $queries->exist('products', ['id' => 5]);

use Stormmore\Queries\Mapper\Map;

$product = $queries->find('products', ['id' => 5], Map::select([
    'product_id'   => 'id',
    'product_name' => 'name'
], UserProduct::class));

$customers = $queries
    ->select('customers c', Map::select([
        'customer_id'   => 'id',
        'customer_name' => 'name'
    ]))
    ->leftJoin('orders o', 'o.customer_id = c.customer_id', Map::many("orders", [
        'order_id' => 'id'
    ]))
    ->findAll();

$customer = $this->queries
    ->select('customers c', Map::select([
        'customer_id',
        'customer_name'
    ], Customer::class, classId: 'customer_id'))
    ->leftJoin('orders o', 'o.customer_id = c.customer_id', Map::many("orders", [
        'order_id'
    ], Order::class, classId: 'order_id'))
    ->where('c.customer_id', 90)
    ->find();

foreach ($customer->orders as $order) {
    echo $customer->customer_name . " - " . $order->id;
}


$connection->onSuccess(function(string $sql, DateInterval $interval) {
    // log successful queries
});

$connection->onFailure(function(string $sql, DateInterval $interval, Exception $e) {
    // log failed queries
});