PHP code example of 1owe1 / query-box

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

    

1owe1 / query-box example snippets


// PHP version

//  "default_user";
$_ENV["DB_PASS"] = "password";
$_ENV["DB_NAME"] = "default";

// optional params
$_ENV["DB_TYPE"] = "mysql"; // or "pgsql" ("mysql" by default)
$_ENV["DB_HOST"] = "127.0.0.1"; // localhost by default
$_ENV["DB_PORT"] = "3306"; // 3306 by default

// extra params
$_ENV["LOG_QUERY_RESULTS"] = "true" // false by default
$_ENV["LOG_PATH"] = "/my/log/path" // by default use stdOut
$_ENV["DB_IMMUTABLE"] = "false" // manage type of connection with db (persistence or immutable)

 declare(strict_types=1)

namespace App\Models;

use QueryBox\QueryBuilder\QueryBuilder;
// interface for migration
use QueryBox\Migration\MigrateAble;

class MyTable extends QueryBuilder implements MigrateAble
{

  // MigrateAble implement
  static function migrationParams(): array
  {
    return [
      "fields" => [
        "id" => "BIGINT NO NULL",
        "foreign_id" => "INT NOT NULL",
        "field" => "CHAR(10)",
      ],
      "foreign" {
        "foreign_id" => [ForeignTable::class, "id"],
      }
    ]
  }
}



 declare(strict_types=1);

use QueryBox\Migration\MetaTable;
use QueryBox\DBFacade;

use App\Models\MyTable;

MetaTable::migrateFromMigrateAble(DBFacade::getDBInstance(), MyTable::class);


 declare(strict_types=1);

use App\Models\MyTable;
use App\Models\AnotherTable;

$queryResult = MyTable::select(["id", "field"])
  ->leftJoin([AnotherTable::table()], ["foreign_id", "id"])
  ->where("field", "LIKE", "something")
  ->orderBy("id")
  ->limit(1)
  ->save();

var_dump($queryResult->fetchAll());