PHP code example of ayela-emmanuel / ayela-orm

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

$user = Models\User::getById(1);

$user = Models\User::first();

Models\User::update(1, 'email', '[email protected]');

Models\User::delete(1);

$ids = [2, 3, 4];
Models\User::deleteGroup($ids);

$users = Models\User::findWhere(['username' => 'john_doe']);

$user = Models\User::firstWhere(['email' => '[email protected]']);

$users = Models\User::findWhere([
    ['age', '>', 25],
    ['status', '=', 'active']
]);

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;
}

Database::setup('localhost', 'database_name', 'username', 'password', false);

class Article extends DatabaseObject
{
    #[SQLType("TEXT")]
    public string $db_content;

    #[SQLType("VARCHAR(100) UNIQUE")]
    public string $db_slug;
}

class User extends DatabaseObject
{
    #[SQLIgnore]
    public string $password; // Will not be stored in the database
}

class UserProfile
{
    public string $bio;
    public array $interests;
}

class User extends DatabaseObject
{
    public string $db_username;
    public UserProfile $db_profile; // Serializable object
}

// Saving
$profile = new UserProfile();
$profile->bio = 'Developer';
$profile->interests = ['coding', 'music'];

$user = new User();
$user->db_username = 'johndoe';
$user->db_profile = $profile;
$user->save();

// Retrieving
$user = User::getById(1);
echo $user->db_profile->bio; // Outputs: Developer

$user = new Models\User();
$user->db_username = 'john_doe';

// Intentionally cause an error (e.g., missing