miladm/orm

basic and lite orm for php

4.0.9 2022-07-02 14:33 UTC

README

This hook takes care of everything you need with database and currently it works with MySQL database. With the use of PDO communication, it will lower the threat of SQL-injection to almost zero.

Configuration and Installation

create

For each table you have to create a Class extends from Table class.

use miladm\table\Table;

class User extends Table {

}

there are abstract methods you have to config as below

class User extends Table {
	public function connection() {
		return new MainConnection;
	}

	public function tableName() {
		return 'user';
	}
}

setup actions before using the table

there's a method called init to setup actions before using this table;

	public function init()
	{
		$this->leftJoin( .... );
	}

Connection Class

use miladm\table\Connection;

class MainConnection extends Connection
{
    public $host = "127.0.0.1";
    public $databaseName = "sample";
    public $user = 'root';
    public $password = 'root';
}

here's the structure of creating connection and to assign a table to a connection

class User extends Table {
	...

	public function connection() {
		return new MainConnection;
	}

	....
}

NOTE: by default the key name is id so if it's the same with your table key name you don't have to set it.

set default Key

    public function key()
    {
        return 'id';
	}

you can change 'id' as you wish and your database structure is.

Query

To create the query you need to create a function of your table then create the query.

$userTable = new user;
$userTable->select();// this will select all records from user table

// equal Query : SELECT * FROM `user` WHERE 1

query methods

Results data object

If you fetch data as object there will be some features assigned to result data object.

update data

you can update data of current result and call ->save() on results data object and update will happen on database.

$UserModel = new User();
$userData = $userModel->where(['id' => 1])->select();
echo $userData[0]->name; // its alex for example
$userData[0]->name = 'jack'; // variable updated but not saved on database
$userData[0]->save(); // now the change has updated the database

note

documentation is in progress but code talks itself. checkout the code for more.