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/ */
ongom / dite-orm example snippets
User::create(['user'=>'john doe']); //creating a user
User::update(1,['user'=>'mike doe']); //updating a user with id 1
User::update(['age'=>20],['user'=>'mike doe']); //updating a user with id 20
User::delete(2); //deleting a user with age 20
User::delete(['age'=>20]); //deleting a user with age 20
User::all(['age'=>3]); //getting users with age 3
User::find()->get(); //getting all users
DB::table('blog_post')
->limit(3)
->select('title, post_body')
->get();
use Dite\Model\Model;
el{
}
//users
class Users extends DB{}
//posts
class Posts extends DB{}
// Status
class Status extends DB{}
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 BlogPosts extends Model{
public function __construct() {
Schema::create('BlogPosts', 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 BlogPosts()
//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 TeachersCourses extends Model{
public function __construct() {
Schema::create(TeachersCourses::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 age > 10
$user = User::update(["age"=>[":gt"=>10]], ["user_name"=>"john", "age"=>32]);
//deleting the user where user_id = 1
$user = User::delete(1);
//deleting the user where user_name = tom
$user = User::delete(["user_name"=>"tom"]);
//deleting the user where IN (1,2)
$user = User::delete([1,2]);
//counting number the users all users
$user = User::countRows();
//updating the user where user_name = tom and age = 30
$user = User::countRows(["user_name"=>"tom", "age" => 30]);
//get all the records from the table user
$user = User::all(null, '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 records from the table user where user_id IN (1,2,3)
$user = User::findById([1,2,3]);
//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
$users = User::find()->get();
//select * from user
$users = User::find(5)->get();
//select * from user where user_id = 5
$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()->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 AS staff_names, 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()->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
]
$user = User::find()
->withAll(Post::class, ['status'=>'active'],'username, age')
->get();
//
$user = User::find(5)
->withAll(Post::class, ['status'=>'active'],'username, age')
->get();
//You can chain it many time like
$user = Product::find(5)
->withAll(Order::class)
->withAll(Status::class)
->get();
$user = User::find()
->withOne(Post::class, ['status'=>'active'],'title')
->get();
//Returns users post where you can paginate, each having there post appended to
$user = User::find(3)
->withOne(Post::class, ['status'=>'active'],'title')
->get();
//Returns on post by its id together with one of his post
//You can chain it many time like
$user = Product::find(5)
->withOne(Order::class)
->withOne(Status::class)
->withOne(Owner::class)
->page(1)
->get();
$user = Post::find()
->attach(User::class)
->get();
//Returns posts together with the user wo posted it.
$user = Post::find()
->attach(User::class)
->attach(Department::class)
->get();
//Returns a post together with the user who posted it.
//You can chain it many time like
$user = User::find(5)
->attach(Department::class)
->withOne(City::class)
->get();
$user = User::withMost(Post::class)
->limit(5)
->get();
//returns 5 users that has posted the most.
//
$user = User::withMost(Post::class)
->withAll(Post::class)
->attach(City::class)
->limit(5)
->get();
$user = User::withLeast(Post::class)
->withAll(Post::class)
->attach(City::class)
->limit(5)
->get();
//Returns 5 users that has least number of post.
$user = User::withOut(Post::class)
->get();
//Returns users that has no post.
$result = User::all(["user_id"=>5]);
//SELECT * FROM user WHERE user_id = 5
//OR
$result = User::all(["user_id"=>5, "name"=>"tom"]);
//SELECT * FROM user WHERE user_id = 5 AND name = tom
//OR
$result = Posts::find()
->where(["user_id"=>5, "name"=>"tom"])
->get();
//SELECT * FROM user WHERE user_id = 5 AND name = tom
$result = User::all(["user_id"=>['$lt'=>5]]);
//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],
]);
//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
//SELECT * FROM user WHERE user_id < 5
$result = User::all([
':and'=>[
"user_id"=>['$lt'=>5],
'age'=>[':gt'=>30],
'phone'=>3 333 333 333
]
]);
//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
]
]);
//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
]
]);
/*
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
]
]);
/*
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],
]
]);
/*
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
]
]);
/*
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],
]
]);
/*
SELECT * FROM user
WHERE NOT (name = tom OR user_id < 5 )
*/
$result = Users::all(["name" => "tom","user_id"=>['$nlt'=>5]]);
/*
SELECT * FROM users
WHERE name = tom AND NOT (user_id < 5 ))
*/
$result = Users::all(["name" => "joyce","_name"=>"tom"])
// SELECT * FROM users WHERE name = tom AND name = tom ))
$result = Users::all([':or'=>[
"name" => "tom",
"_name"=>"daniel",
"__name"=>"loy"]]);
// 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();
// SELECT * FROM users WHERE name = tom OR name = deniel OR name = loy))
$card = User::find(4)
->hasOne(CreditCard::class)
->get();
// returns one creditcard or false
//and also
$card = CreditCards::find(4)
->belongsToOne(User::class)
->get();
// returns one user or false
$card = User::find(4)
->hasMany(Post::class)
->get();
// returns array of post records
// and also
$Post = Post::find(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 TeachersCourses extends Model{
public function __construct() {
Schema::create(TeachersCourses::class, function(Table $table){
$table->id();
$table->foriegnKey('courses_id');
$table->foriegnKey('teachers_id');
});
}
}
$card = Teacher::find(4)
->hasManyMany(Courses::class)
->get();
// returns one user or false