А vary simple php & mysql custom Active record class for small projects - if you need realy fast connection to database for 2s without configuring 10+ files.
Open Model.php and modify the connection with database.
...
private static $host = 'localhost'; //fill this ..
private static $username = 'root'; //fill this ..
private static $password = ''; //fill this ..
private static $dbname = 'database'; //fill this ..
....
Here are some basic cases that show you how to use this class. You can also see the example_use.php file
- Create new class (If its possible with the name of the of the table)
- Extend the ActiveRecordLite
- Use parent model construct as shown to enable connection
-- Optional --
- If your class name IS NOT the same as your table name, you can specify the table as shown*
- If your primary key filed IS NOT 'id', you can specify that as well.*
class Users extends Model{
public $table = 'users'; // name of your table(optional, if your class name **IS NOT** the same as your table)
public $primary_key = 'id'; // name of your primary key field (optional, if your primary key column **IS NOT** "id")
function __construct()
{
parent::__construct(); // use Parent model constructor to enable connection to DB **MUST do it**
}
}
Thats it! Now you are ready to use it. Here is how you use it.
$user = new Users;
echo $user->find(2)->username; // use a **find(id)** method
// returns only the found row
// or you can use it like that:
$user = new Users;
$user->find(1);
echo $user->email;
$users = new Users; // instantiate the class
echo $users->all(); .. get all recorst with the **all()** method
Okeeyy, there are 2 ways.
The first one is very simple. Just pass an ID to the delete() method and thats it.
$user = new Users;
echo $user->delete(1); // delete #1 record (by id)
The second way is IF you have already selected a row with the find(id) method - you can just call ->delete() on it and thats it.
$user = new Users;
$user->find(2);
echo $user->username;
$users->delete(); // delete the found user!
Okeyy, thats fun. There is a universal method called save()
Creating new record: Just remember that the $user properties must be the same as the columns in your table. So if try to set $user->isAdmin = 1; and in my table isAdmin doesn't exits, it will fail !!!
$user = new Users;
$user->username = "voTkaPoweR";
$user->locale= "bg"; // make sure the atributes exists as columns in your table
$user->email= "test@test.com";
$user->save();
Updating record: You can only update a record if you have found a row by the find(id) method.
$user = new Users;
$user->find(1); // get first row
// make sure the atributes exists as columns in your table
$user->locale= "bg";
$user->email= "test@test.com";
$user->save(); // update
Remember, its only ALPHA, it can be slow with big database sizes. Its ment to be used in realy small projects where you need to conect to database and display a custom page for example.
Thats it. Have fun.