1. Go to this page and download the library: Download ayela-emmanuel/ayela-orm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ayela-emmanuel / ayela-orm example snippets
use AyelaORM\Database;
Database::setup('localhost', 'database_name', 'username', 'password', false);
namespace Models;
use AyelaORM\DatabaseObject;
use AyelaORM\SQLType;
use AyelaORM\SQLIgnore;
class User extends DatabaseObject
{
public string $db_username;
public string $db_email;
public int $db_age;
public \DateTime $db_created_at;
#[SQLIgnore]
public string $password; // Will not be stored in the database
public function __construct()
{
parent::__construct();
$this->db_created_at = new \DateTime();
}
}
Models\User::register();
$user = new Models\User();
$user->db_username = 'john_doe';
$user->db_email = '[email protected]';
$user->db_age = 30;
$user->password = 'securepassword'; // Will not be saved to the database
$user->save();
$users = Models\User::list(1, 10); // Retrieve the first 10 users (page 1).
class Author extends DatabaseObject
{
public string $db_name;
public string $db_email;
}
class Book extends DatabaseObject
{
public string $db_title;
public Author $db_author; // Relationship to Author
}
$author = new Models\Author();
$author->db_name = 'Jane Doe';
$author->db_email = '[email protected]';
$author->save();
$book = new Models\Book();
$book->db_title = 'Learning AyelaORM';
$book->db_author = $author; // Set the Author object
$book->save();
$book = Models\Book::getById(1);
echo $book->db_title; // Outputs: Learning AyelaORM
echo $book->db_author->db_name; // Outputs: Jane Doe
class Author extends DatabaseObject
{
// ...
public function getBooks(): array
{
return Models\Book::findWhere([['author', '=', $this->db_id]]);
}
}
// Usage
$author = Models\Author::getById(1);
$books = $author->getBooks();
class Student extends DatabaseObject
{
public string $db_name;
public function enrollInCourse(Course $course)
{
$enrollment = new StudentCourse();
$enrollment->db_student = $this;
$enrollment->db_course = $course;
$enrollment->save();
}
public function getCourses(): array
{
$enrollments = StudentCourse::findWhere([['student', '=', $this->db_id]]);
return array_map(fn($enrollment) => $enrollment->db_course, $enrollments);
}
}
class Course extends DatabaseObject
{
public string $db_title;
public function getStudents(): array
{
$enrollments = StudentCourse::findWhere([['course', '=', $this->db_id]]);
return array_map(fn($enrollment) => $enrollment->db_student, $enrollments);
}
}
class StudentCourse extends DatabaseObject
{
public Student $db_student;
public Course $db_course;
}