MySimpleORM is a simple PHP/MySQL Object-relational mapping library. It is lightweight and tries to take care of as many things as possible. It allows you to focus on coding the business logic of your application without having to think too much about the SQL queries, simply write your conditions and the ORM will take care of the rest 😄 !
To be able to use the ORM, you need to have a PHP application and a MySQL/MariaDB database. Follow these simple guidelines to setup MsORM within your PHP web app.
The recommended method of installation is via composer
composer require mysimpleorm/mysimpleorm
- The name of your tables are going to be the names of your object classes in PHP. Therefore, a table named "Users" will refer to the class "Users".
Add the following to your PHP class:
require 'vendor/autoload.php;
use MySimpleORM\BaseClass;
extends BaseClass
- Change your
private
class attributes toprotected
to give access to the mapper. - Setup your MySQL/MariaDB information (check the example below)
No need for getters/setters, the BaseClass
provides you a generic methods.
require 'vendor/autoload.php';
use MySimpleORM\BaseClass;
class MyClass extends BaseClass {
protected $IDMyClass;
protected $Name;
public function MyClass() {
parent::__construct($this);
/**
* There are two ways to connect the ORM to your Database.
*
* 1. Set the following environment variables:
* - DB_HOSTNAME
* - DB_USERNAME
* - DB_PASSWORD
* - DB_DATABASE
* - DB_PORT
*
* 2. Call the following method
* - $this->Mapper->Database->setup($host, $user, $password, $db, $port);
*/
$this->IDMyClass = 0;
$this->Name = "";
}
public function __destruct() {}
}
MyClass |
---|
IDMyClass |
Name |
*The examples below are all used as if they were part of a function within a controller (MVC).
The ORM provides generic get & set methods to help alleviate the content of your class.
$Users = new Users();
$name = $Users->get('Name');
// $class->get(attribute_name) => returns value of the attribute
$Users = new Users();
$name = $Users->set('Name', 'foo');
// $class->set(attribute_name, value) => sets the attribute to the value
$Users = new Users();
$Users = $Users->findById(1);
You've retrieved the user "1" from the table "Users" and can now use it as an object.
$Users = new Users();
//replace by whatever condition you desire
$wheres = array(
"column" => "IDCompanies",
"condition" => "=",
"value" => 1 //keep in mind you could use a sub-query here
);
// OR
$wheres = array(
"IDCompanies,=,1"
)
$Users = $Users->getArray($wheres);
You've just retrieved all the users that were part of the company "1". You're object $Users
is now an array of Users
Assuming the database contains aUsers
with the name "foo".
$Users = new Users();
$Users->set("Name", "foo");
$Users = $Users->getCurrent();
//The mapper returned the object User named "foo"
Inserting, updating or deleting an object is very simple. All you need to do is call a few functions!
//Insert & Update
$Users = new $Users();
$Users->set("Name", "foo");
$Users->save(); // inserted
$Users->set("Name", "bar");
$Users->save(); // updated
//Delete
$Users->delete();
// $Users is now undefined