LiPMO is exactly what its name suggests. It is the abbreviation of Lightweight PHP - MySQL ORM. Originally I created it as a part of a university project but I kept working on it and finally I release it to the public.
- Automatic mapping of MySQL tables to PHP classes
- Insert
- Update
- Delete
- Select by id
- Select all
- Select with condition(s)
- Paging support for selects (start and limit values)
LiPMO uses PDO parametrized queries which improves the security and performance of the MySQL queries.
The part "lightweight" is meant very seriously. LiPMO features only the minimum of what an object relational mapper needs, nothing fancy. Therefore it requires no special installation or configuration - and it's extremely easy to use.
The biggest feature of many huge ORM Frameworks that LiPMO does not support is the mapping of table relations.
LiPMO can be installed on most free webhosts. The requirements are the following:
- PHP 5.1 with PDO enabled
- MySQL
- Create your database and your tables.
- Make sure all of your tables contain a UNIQUE field id. Most of the time this will be the PK anyways.
- Get the LiPMO directory.
- Edit the Config.php file. Believe me, you don't need more explanation.
- Create your entity classes (see below) and save them to the LiPMO directory.
- Make sure there is nothing but the LiPMO engine and entity files in the LiPMO directory.
Entity classes are not created automatically, you have to create them by yourself. Don't worry though, it only takes a few minutes and it is actually very simple. You can check the provided example "Sample.php" (you can actually delete it from your project).
Let's assume you have a table called user with 4 fields: id, username, password, email. Let's create an entity class for it:
<?php
// The superclass has to be included
require_once 'DBEntity.php';
class User extends DBEntity {
// Set the name of the table in the constructor
public function __construct() {
$this->tableName = "user";
}
// Implement the properties.
// Note that you don't need the id as it has been already implemented by the superclass DBEntity.
// Also, MySQL is case-insensitive.
public $username;
public $password;
public $email;
}
?>
Yup, that's it. Your User entity is ready to be used.
Whenever you use LiPMO you have to include it in your file:
require_once('LiPMO/IncludeList.php');
Note that you always need to instantiate the entity classes, there are no static methods.
// Id must not be set.
$user = new User();
$user->username = 'admin';
$user->password = 'Admin1234';
$user->email = 'email@address.com';
$userId = $user->insert();
Return value: The id of the created entity, false in case of an error.
// Id is used to identify the entity in the database
$user = new User();
$user->id = 1;
$user->username = newUsername;
$success = $user->update();
Return value: True on success, otherwise false.
// Id is used to identify the entity in the database
$user = new User();
$user->id = 1;
$success = $user->delete();
Return value: True on success, otherwise false.
// Again, you need an instance
$user = new User();
$user = $user->selectById(4);
Return value: Selected entity or null.
// Again, you need an instance
$user = new User();
$users = $user->selectAll();
// Alternatively, you can select the elements 21-40 as follows:
$users = $user->selectAll(1, 20);
Return value: Array of entities, single entity if there is only one found, or null.
// Again, you need an instance
$user = new User();
// Example: select users who have a GMail address
// and have the word 'admin' in their username
// This example uses positional parameters (?) but you can name them.
// In that case, you need to provide key-value pairs in the array.
// Paging (starting index / selection limit) still works.
$users = $user->select('email LIKE ? AND username LIKE ?', array('gmail.com', 'admin'));
Return value: Array of entities, single entity if there is only one found, or null.
Feel free to contact me on GitHub or in email (nXu@nXu.hu). Also, any feedback, suggestion and reports of bugs or vulnerabilities are welcome (I recommend using the Issue page in the last two cases).
LiPMO is licensed under the MIT License. See LICENSE for details.