PHP code example of garder500 / sqliberty
1. Go to this page and download the library: Download garder500/sqliberty 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/ */
garder500 / sqliberty example snippets
QLiberty\Database;
$db = new Database("host","database","user","password","port");
QLiberty\Database;
use SQLiberty\Schema;
$db = new Database("host","database","user","password","port");
$users = $db->model(function(Schema $table){
$table->int("id");
$table->varchar("name");
$table->varchar("email");
$table->varchar("password");
$table->datetime("created_at");
$table->datetime("updated_at");
});
QLiberty\Database;
use SQLiberty\Schema;
$db = new Database("host","database","user","password","port");
$users = $db->model(function(Schema $table){
$table->int("id");
$table->varchar("name");
$table->varchar("email");
$table->varchar("password");
$table->datetime("created_at");
$table->datetime("updated_at");
});
$user = $users->create([
"name" => "John Doe",
"email" => "[email protected] ",
"password" => "password",
"created_at" => date("Y-m-d H:i:s"),
"updated_at" => date("Y-m-d H:i:s")
]);
$user = $users->update([
"name" => "Jack Doe",
])->save();
$users = $db->model("user",function(Schema $table){
$table->int("id");
$table->varchar("name");
$table->varchar("email");
$table->varchar("password");
$table->datetime("created_at");
$table->datetime("updated_at");
$table->model("posts",function(Schema $table){
$table->int("id");
$table->varchar("title");
$table->varchar("content");
$table->datetime("created_at");
$table->datetime("updated_at");
$table->belongTo("user");
$table->model("comments",function(Schema $table){
$table->int("id");
$table->varchar("content");
$table->datetime("created_at");
$table->datetime("updated_at");
$table->belongTo("post");
});
});
});
$users->create([
"name" => "John Doe",
"email" => "[email protected] ",
"password" => "password",
"created_at" => date("Y-m-d H:i:s"),
"updated_at" => date("Y-m-d H:i:s"),
"posts" => [
[
"title" => "Post 1",
"content" => "Content 1",
"created_at" => date("Y-m-d H:i:s"),
"updated_at" => date("Y-m-d H:i:s"),
"comments" => [
[
"content" => "Comment 1",
"created_at" => date("Y-m-d H:i:s"),
"updated_at" => date("Y-m-d H:i:s"),
],
[
"content" => "Comment 2",
"created_at" => date("Y-m-d H:i:s"),
"updated_at" => date("Y-m-d H:i:s"),
]
]
],
[
"title" => "Post 2",
"content" => "Content 2",
"created_at" => date("Y-m-d H:i:s"),
"updated_at" => date("Y-m-d H:i:s"),
"comments" => [
[
"content" => "Comment 3",
"created_at" => date("Y-m-d H:i:s"),
"updated_at" => date("Y-m-d H:i:s"),
],
[
"content" => "Comment 4",
"created_at" => date("Y-m-d H:i:s"),
"updated_at" => date("Y-m-d H:i:s"),
]
]
]
]
]);
// This will create 2 post with 2 comments each
$users = $db->model(function(Model $table){
$table->int("custom_id")->primaryKey()->autoIncrement();
$table->varchar("name");
$table->varchar("email");
$table->varchar("password");
$table->datetime("created_at");
$table->datetime("updated_at");
$table->primaryKey("email");
});
/**
* Defining model
*/
$users = $db->model("user",function(Schema $table){
$table->int("id");
$table->varchar("name");
$table->varchar("email");
$table->varchar("password");
$table->datetime("created_at");
$table->datetime("updated_at");
$table->model("posts",function(Schema $table){
$table->int("id");
$table->varchar("title");
$table->varchar("content");
$table->datetime("created_at");
$table->datetime("updated_at");
$table->belongTo("user");
$table->model("comments",function(Schema $table){
$table->int("id");
$table->varchar("content");
$table->datetime("created_at");
$table->datetime("updated_at");
$table->belongTo("post");
});
});
});
$found = $db->findAll([
"posts" => [
[
"comments" => [
"content" => "Comment"
]
]
]
])
// The result will be All comment that "Contain" the word Comment.