PHP code example of ongom / dite-orm

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


DRIVER = sqlite
DATABASE_NAME = schooldb
RUN_SCHEMA = 1

DRIVER = mysql
DATABASE_NAME = schooldb
SERVER_NAME = localhost
USER_NAME = root
DATABASE_PASSWORD = 1234
RUN_SCHEMA = 1

$user = ["name"=>"tom", "age"=>36];
$user["name"]; //tom
$user["age"]; //36

$user = stdClass Object ([name] => tom [email] => [email protected])
$user->name; //tom
$user->age; //36



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->id();

  $table->string('name');
  //OR
  $table->string('name', 50);
  

  $table->text('name');
  //OR
  $table->text('name', 200);
  

  $table->longText('name');
  

  $table->int('size');
  

  $table->bigInt('size');
  //OR
  $table->bigInt('size', 6);
  

  $table->enum('size', ['small','medium', 'large']);
  

  $table->unsign('year');
  

  $table->boolean('is_active');
  

  $table->float('distance');
  

  $table->double('distances');
  

  $table->decimal('distance');
  

  $table->year('Year_of_birth');
  

  $table->timestamp();
  

  $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!!
  

  $table->foreignKey('posts-id')->unique();
  $table->email('user_email')->unique()->notnull();
  

  $table->foreignKey('post_id')->cascades();
  $table->foreignKey('user_id')->unique()->notnull()->cascade();
  

  $table->foreignKey('post_id')->cascades();
  $table->foreignKey('user_id')->unique()->notnull()->cascade();
  

  $table->foreignKey('post_id')->restrict();
  

  $table->foreignKey('post_id')->setnull();
  

    $table->foreignKey('post_id')->noaction();
  

  $table->foreignKey('post_id')->cascadeDelete();
  

  $table->foreignKey('post_id')->cascadeUpdate();
  

  $table->foreignKey('post_id')->restrictDelete();
  

  $table->foreignKey('post_id')->restrictUpdate();
  

  $table->foreignKey('post_id')->setnullDelete();
  

  $table->foreignKey('post_id')->setnullUpdate();
  

  $table->foreignKey('post_id')->noactionDelete();
  

    $table->foreignKey('post_id')->noactionUpdate();
  

   $table->foreignKey('post_id')->noactionUpdate()->setnullDelete();
  

// 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.

$users = User::find()->skip(10)->get()
//OR
$users = User::find()->offset(10)->get()

$users = Post::find()
            ->skip(5)
            ->limit(10)
            ->select('title, post')
            ->get()
//select title, post from user limit 5,10

$users = User::find()
        ->page(3)
        ->perpage(5)
        ->where(['first_name'=>'tom','age'=>[':gt'=>18]])
        ->get()

[
  '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
]

  class DB extends Model{}
  $Post = DB::table('Post')->get()
  // OR
  $Posts = Model::table('Post')->get()
  
  // 
  $users = DB::table('user')->select('name')->get()
  // find
  $users = Model::table('user')
          ->limit(10)
          ->offset(5)
          ->select('name')
          ->get()

  // paginating
  $users = Model::table('post')
          ->page(10)
          ->perpage(5)
          ->select('title')
          ->get()
// OR
$users = DB::table('post')
          ->page(10)
          ->perpage(5)
          ->select('title')
          ->get()

$users = User::find()
        ->join('post')
        ->join('comments')
        ->get()

$users = User::find()
        ->join('post')
        ->leftJoin('comments')
        ->page(1)
        ->perpage(10)
        ->orderBy('user.name'=>'desc')
        ->select(['user.name', 'post.title', 'count(*) As total'])
        ->where(["first_name" = "mike"])
        ->get()

   $result = User::findById(2)
   //OR
   $post = Post::findByPk(2)->get()
   //OR
   $post = Post::findByPk(2)->select('post, title, created_at AS time')->get()

   $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

$post = Users::findByPk(2)
        ->hasMany(Post::class)
        ->select('post_id,title, post')
        ->orderBy()
        ->where(['title'=>['$like'=>'%computer']])
        ->limit(10)
        ->get();

Users::drop();
bush
LOGGER = 1
FETCH_MODE = std_array
RUN_SCHEMA = 1
SQL_COLOR = green