1. Go to this page and download the library: Download ongom/dite-orm 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/ */
use Dite\Model\Model;
xtends Model{}
//posts
class Posts extends Model{}
// Status
class Status extends Model{}
// Status
class DB extends Model{}
use Dite\Schema\Schema;
use Dite\Model\Model;
use Dite\Table\Table;
Schema::create(Users::class, function(Table $table){
$table->id();
$table->string('name');
$table->enum('gender', ['male','female']);
$table->string('email')->unique()->notnull();
$table->int('age')->notnull();
$table->timestamp();
});
}
}
//users
class Users extends Model{
public function __construct() {
Schema::create(Users::class, function(Table $table){
$table->id();
$table->string('name');
$table->string('email')->unique()->notnull();
$table->int('age')->notnull();
$table->timestamp();
});
}
}
//posts
class Posts extends Model{
public function __construct() {
Schema::create('Posts', function(Table $table){
$table->id();
$table->string('title')->notnull();
$table->string('body')->notnull();
$table->foreignKey('users_id')->notnull();
$table->foreignKey('status_id')->cascade();
$table->timestamp();
});
}
}
// Status
class Status extends Model{
public function __construct() {
Schema::create(Status::class, function(Table $table){
$table->id();
$table->int('status')->notnull();
});
}
}
//Instantiating the three classes
$status = new Status()
$users = new Users()
$posts = new Posts()
//Instantiating the three classes
$users = new Users()
$posts = new Posts()
$status = new Status()
//Teachers table
class Teachers extends Model{
public function __construct() {
Schema::create(Teachers::class, function(Table $table){
$table->id();
$table->string('teacher_name');
$table->string('email');
});
}
}
//Courses table
class Courses extends Model{
public function __construct() {
Schema::create(Courses::class, function(Table $table){
$table->id();
$table->string('course_name');
});
}
}
//Intermediate table
class Teachers_Courses extends Model{
public function __construct() {
Schema::create(Teachers_Courses::class, function(Table $table){
$table->id();
$table->foriegnKey('courses_id');
$table->foriegnKey('teachers_id');
});
}
}
$table->sql("CREATE TABLE IF NOT EXISTS Users ( status_id INT AUTO_INCREMENT PRIMARY KEY NOT NULL , status INT NOT NULL , created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP )");
$table->foreignKey('users_id');
$table->foreignKey('user_id')->notnull();
$table->string('user_name')->notnull();
$table->id('user_name')->notnull(); // dont do this!!
// creating single user
$user = User::create(["user_name"=>"tom", "age"=>32]);
//creates multiple users at once
$user = User::create([
["user_name"=>"tom", "age"=>32],
["user_name"=>"mike", "age"=>25],
["user_name"=>"loy", "age"=>27],
]);
//updating the user with id 1
$user = User::update(1, ["user_name"=>"tom", "age"=>32]);
//updating the user where user_name = tom
$user = User::updateMany(["user_name"=>"tom"], ["user_name"=>"john", "age"=>32]);
//updating the user where user_name = tom and age = 32
$user = User::updateMany(["user_name"=>"tom", "age"=>32], ["user_name"=>"andrew", "age"=>15]);
//deleting the user where user_id = 1
$user = User::delete(1);
//deleting the user where user_name = tom
$user = User::deleteMany(["user_name"=>"tom"]);
//deleting the user where user_name = tom and age = 30
$user = User::deleteMany(["user_name"=>"tom", "age" => 30]);
//counting number the users where user_name = tom
$user = User::countRecords();
//updating the user where user_name = tom and age = 30
$user = User::countRecords(["user_name"=>"tom", "age" => 30]);
//get all the records from the table user
$user = User::all([], 'name, email');
////get all the records from the user table where age > 30
$user = User::all(["age"=>[":gt"=>30]], ['name', 'email']);
//get all the records from the table user
$user = User::all();
////get all the records from the user table where age > 30
$user = User::all(["age"=>[":gt"=>30]]);
//get all the records from the table user
$user = User::first();
//get the first records from the user table where age > 30
$user = User::first(["age"=>[":gt"=>30]]);
//get the last records from the table user
$user = User::last();
//get the last records from the user table where age > 30
$user = User::last(["age"=>[":gt"=>30]]);
//get the records from the table user where user_id = 2
$user = User::findById(2);
//get the one and first record from the table user where user_id > 10
$user = User::findOne(["user_id"=>[":gt"=>10]]);
//checks if any user record exist
$user = User::exist();
//checks if user with user_id 2 exists
$user = User::exist(2);
//checks if the users with user_id greater than 10 exist
$user = User::exist(["user_id"=>[":gt"=>10]]);
//checks if email [email protected] exist in user table
$user = User::exist(["email"=>"[email protected]"]);
$user = User::sql("SELECT * FROM users");
$user = User::sql("SELECT * FROM users", []);
$user = User::sql("SELECT * FROM users WHERE user_name = ? And age > ?", ['tom', 30]);
$user = User::joins(Post::class,);
//OR
$user = User::joins("Post");
//select * from user join post on user.user_id = post.user_id
//select * from user where user_id = 10.
$user = User::findByPk(10)->get()
//select user_id, username from user where user_id = 10.
$user = User::findByPk(10)->select('user_id, username')->get()
$users = User::find()->get();
//select * from user
$users = User::find()->get();
//select * from user where name = tom and age = 10.
$users = User::find()
->where(["name"=>"tom", "age"=>10])
->get();
//select * from user where age = tom and age = 10.
$users = User::find()->orderBy("user_id, name ASC, created_at DESC")->get();
//select * from user order by user_id asc name asc created_at DESC.
$user = User::find()->orderBy(['user_id'=> 'asc'])->get();
//select * from user order by user_id desc.
$user = User::find()->orderBy(['user_id'=> 'asc', 'name'=>'desc'])->get();
//select * from user order by user_id desc age asc.
$user = User::find()->orderBy('age asc')->get();
//select * from user where name = john orderby age asc.
$user = User::find()->groupBy('name')->get()
//select * from user group by name.
$user = User::find()->groupBy('group_id')->get()
//select * from user group by group_id.
$user = User::find()->groupBy('username')->get()
//select * from user group by username where name = john doe.
$users = User::find()->select(['name', 'age'])->get()
//select name, age from user.
//OR
$users = User::find()->select('name, age')->get()
//select name, age from user.
$users = User::find()
->select(['name as names_of_staffs', 'age', "COUNT(name) * 2 as total"])
->get()
//
$users = User::find()
->groupBy('age')
->get()
//SELECT name as names_of_staffs', 'age', "COUNT(name) as total from user group by age where name = john.
$users = User::find()
->where(['name'=>'john'])
->select('name, age')
->groupBy('name')
->orderBy(['user_id'=> 'desc', 'age'=>'asc'])
->get()
$users = User::find()->limit(5)->get()
//select * from user limit 5.
[
'num_records' => 15; //total number of records
'num_pages' => 2; //total number of pages
'has_next' => false; //if it has next page or not
'current_page' => 2; //the current page showing
'has_prev' => true; //if it has previous page or not
'next_page' => null; //what next page is, null for no next page
'prev_page' => 1; //what next page is, null for no next page
'per_page' => 10; //number of records per page
'position' => 1; //position of the first record of a page in the entire result
'data' = [...]; // records for each page
]
$result = User::all(["user_id"=>5])
//sql = SELECT * FROM user WHERE user_id = 5
//OR
$result = User::all(["user_id"=>5, "name"=>"tom"])
//sql = SELECT * FROM user WHERE user_id = 5 AND name = tom
//OR
$result = Posts::find()
->where(["user_id"=>5, "name"=>"tom"])
->get()
//sql = SELECT * FROM user WHERE user_id = 5 AND name = tom
$result = User::all(["user_id"=>['$lt'=>5]])
//sql = SELECT * FROM user WHERE user_id < 5
//OR
$result = User::all([
"user_id"=>[':lt'=>5],
"age"=>[':gt'=>30],
])
//OR
$result = User::all([
"email"=>"[email protected]"
"user_id"=>[':lt'=>5],
"age"=>[':gt'=>30],
])
//sql = SELECT * FROM users WHERE email = [email protected] AND user_id < 5 AND age > 30
$result = User::all(["user_id"=>['$lt'=>5]])
$result = User::all(["user_id"=>[':lt'=>5]])
//Both will output
//sql = SELECT * FROM user WHERE user_id < 5
$result = User::all([
':and'=>[
"user_id"=>['$lt'=>5],
'age'=>[':gt'=>30],
'phone'=>3 333 333 333
]
])
//sql = SELECT * FROM user WHERE user_id < 5 AND age >30 AND phone = 3 333 333 333
$result = User::all([
':or'=>[
"user_id"=>['$lt'=>5],
'age'=>[':gt'=>30],
'phone'=>3 333 333 333
]
])
//sql = SELECT * FROM user WHERE user_id < 5 OR age > 30 OR phone = 3 333 333 333
$result = User::all([
':or'=>[
['name'=>'tom', 'email'=>'[email protected]'],
"user_id"=>['$lt'=>5],
'age'=>[':gt'=>30],
'phone'=>3 333 333 333
]
])
/*
sql = SELECT * FROM user
WHERE (name = tom AND email = [email protected])
OR user_id < 5
OR age > 30
OR phone = 3 333 333 333
*/
$result = User::all([
':nand'=>[
"user_id"=>['$lt'=>5],
'age'=>[':gt'=>30],
'phone'=>3 333 333 333
]
])
/*
sql = SELECT * FROM user
WHERE NOT (
user_id < 5
AND age > 30
AND phone = 3 333 333 333)
*/
$result = User::all([
':nand'=>[
"name" => "tom",
"user_id"=>['$lt'=>5],
]
])
/*
sql = SELECT * FROM user
WHERE NOT (name = tom AND user_id < 5 )
*/
$result = User::all([
':nor'=>[
"user_id"=>['$lt'=>5],
'age'=>[':gt'=>30],
'phone'=>3 333 333 333
]
])
/*
sql = SELECT * FROM user
WHERE NOT (
OR user_id < 5
OR age > 30
OR phone = 3 333 333 333)
*/
$result = User::all([
':nor'=>[
"name" => "tom",
"user_id"=>['$lt'=>5],
]
])
/*
sql = SELECT * FROM user
WHERE NOT (name = tom OR user_id < 5 )
*/
$result = Users::all(["name" => "tom","user_id"=>['$nlt'=>5]])
/*
sql = SELECT * FROM users
WHERE name = tom AND NOT (user_id < 5 ))
*/
$result = Users::all(["name" => "joyce","_name"=>"tom"])
// sql = SELECT * FROM users WHERE name = tom AND name = tom ))
$result = Users::all([':or'=>[
"name" => "tom",
"_name"=>"daniel",
"__name"=>"loy"]])
// sql = SELECT * FROM users WHERE name = tom OR name = deniel OR name = loy))
//Alternatively
$result = Users::find()
->where([':or'=>[
"name" => "tom",
"_name"=>"daniel",
"__name"=>"loy"]])
->get()
// sql = SELECT * FROM users WHERE name = tom OR name = deniel OR name = loy))
$card = User::findByPk(4)
->hasOne(CreditCard::class)
->get()
// returns one creditcard or false
//and also
$card = CreditCards::findByPk(4)
->belongsToOne(User::class)
->get()
// returns one user or false
$card = User::findByPk(4)
->hasMany(Post::class)
->get()
// returns array of post records
// and also
$Post = Post::findByPk(4)
->belongsToOne(User::class)
->get()
// returns a user or false
//Teachers table
class Teachers extends Model{
public function __construct() {
Schema::create(Teachers::class, function(Table $table){
$table->id();
$table->string('teacher_name');
$table->string('email');
});
}
}
//Courses table
class Courses extends Model{
public function __construct() {
Schema::create(Courses::class, function(Table $table){
$table->id();
$table->string('course_name');
});
}
}
//Intermediate table
class Teachers_Courses extends Model{
public function __construct() {
Schema::create(Teachers_Courses::class, function(Table $table){
$table->id();
$table->foriegnKey('courses_id');
$table->foriegnKey('teachers_id');
});
}
}
$card = Teacher::findByPk(4)
->hasManyMany(Courses::class)
->get()
// returns one user or false