Giter Site home page Giter Site logo

mysimpleorm's Introduction

MySimpleORM

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 ๐Ÿ˜„ !

Setting up MySimpleORM (MsORM)

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.

Installation

The recommended method of installation is via composer

composer require mysimpleorm/mysimpleorm

Database-side guidelines

  1. 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".

Setup

Add the following to your PHP class:

  1. require 'vendor/autoload.php;
  2. use MySimpleORM\BaseClass;
  3. extends BaseClass
  4. Change your private class attributes to protected to give access to the mapper.
  5. Setup your MySQL/MariaDB information (check the example below)

No need for getters/setters, the BaseClass provides you a generic methods.

Example

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() {}
}

Example of a table

MyClass
IDMyClass
Name

Documentation

*The examples below are all used as if they were part of a function within a controller (MVC).

Generic get/set

The ORM provides generic get & set methods to help alleviate the content of your class.

Get

$Users = new Users();
$name = $Users->get('Name');
// $class->get(attribute_name) => returns value of the attribute

Set

$Users = new Users();
$name = $Users->set('Name', 'foo');
// $class->set(attribute_name, value) => sets the attribute to the value

Select

To select an object by its ID

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

To retrieve an array of objects

$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
);
$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

To retrieve current object, depending on what you've already set

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"

Insert / Update / Delete

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

mysimpleorm's People

Contributors

anthonygauthier avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.