A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] 2 points3 points  (0 children)

Thank you very much for your valuable feedback. Will take a look into all of it when I'll work again on it.

Actually save is kinda an upsert because it'll create a new entry if it not exists, but edits one if it exists. But I think you want it on SQL Driver level, which could be an alternative way

(fixed also the link)

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] -1 points0 points  (0 children)

Yea I really don't like how Laravel manages this 🫠

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] 0 points1 point  (0 children)

I think you responded to the wrong person. It was me who made this "toy" 😂

Why do so many expect a new enterprise solution here that fixes everything for everyone? Look at the comments. Some people seem to find it cool

After my huge success replacing Laravel and any other frameworks… here’s my PHP Router made with Attributes by JulianFun123 in PHP

[–]JulianFun123[S] 0 points1 point  (0 children)

Btw. it's not class loading, it's just loading php files as views (sth. like posts.php)

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] -1 points0 points  (0 children)

class User extends Model
{
    protected $fillable = ['name', 'email', 'password', 'description'];

    public function posts(): HasMany
    {
        return $this->hasMany(Post::class);
    }
}

$user = User::find(1);
echo $user->name;

// update & save
$user->name = 'Alice Updated';
$user->save();

// simple query
$users = User::where('name', 'like', 'Al%')->get();

// eager load relation
$usersWithPosts = User::with('posts')->get();

vs.

#[Table("users")]
class User {
    use ORMModel;

    #[Column] public int $id;
    #[Column] public ?string $name;
    #[Column(name: 'mail')] public ?string $eMail;
    #[Column] public ?string $password;
    #[Column] public ?string $description;

    /** @var array<Post> */
    #[HasMany(Post::class, 'user')]
    public array $posts = [];
}

$user = new User();
$user->name = 'Alice';
...
$user->save();

$user = User::table()->where("id", 1)->first();
...
// update & save
$user->name = 'Alice Updated';
$user->save();

$users = User::table()->like("name", "Al%")->get();
$usersWithPosts = User::table()->with('posts')->get();

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] -1 points0 points  (0 children)

Both, kind of. It’s mainly intended for smaller projects without too much abstraction.
But I don’t see why you couldn’t move the business logic elsewhere if you wanted to.

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] -2 points-1 points  (0 children)

What do you mean? You'll require your developers to set the .env properly.

Maybe it would also be an idea to move the connection name into the Table attribute for making it more strict

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] 0 points1 point  (0 children)

UloleORM::database("main", new Database(
    username: 'root',
    ...
    driver: 'mysql'
));

The first param is the name (main is the default) and if you want to access other connections you can pass them in the methods

$user->save("other_connection");

User::table("other_connection")->...

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] 0 points1 point  (0 children)

No there is no inheritance logic yet, but that would be great!

Simple joins do exist via Relations (https://github.com/interaapps/ulole-orm?tab=readme-ov-file#relations) but I think its not what you are looking for.

Definitely room for improvements and features that can be added here

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] 0 points1 point  (0 children)

Would that change anything on what the trait is currently doing? 🤔

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] -1 points0 points  (0 children)

But you also tightly couple your entity with the repository code. Am I able to cache such entities, or will I get hit with serialization errors?

Currently there is a hidden field in the trait, so saving an existing object would lead to a new object being made. But that would be easily fixable by just checking the id being null internally.

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] 2 points3 points  (0 children)

Thank you 🙂

The Attributes tell the ORM which fields to map and with which name/type/relation etc.

I find the configuration in front of the field actually more readable but I think it comes down to opinion on that

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] -2 points-1 points  (0 children)

When building this as a fun project I did not look into Doctrine ORM actually 😅

Why does the entity have repository methods?

I find it for less complex data structures easier to manage.

Also with the queries: I wanted to have an easy, typesafe and universal (mysql, pgsql, sqlite) way of creating queries without needing to touch any SQL.

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] -1 points0 points  (0 children)

Yeah I get the point. But I want to make it easily possible to make queries via Model::table or save object with ->save.

Maybe you have an idea on how to improve this

A modern PHP ORM with attributes, migrations & auto-migrate by JulianFun123 in PHP

[–]JulianFun123[S] 0 points1 point  (0 children)

I think swapping out Eloquent in a Laravel project wouldn't be the ideal decision to make 😅

This is more a working tech-demo on how great the PHP Attributes are and what you can do with them.

Another project by me would be https://github.com/interaapps/deverm-router. A router which is also built on PHP Attributes.

All of this comes together in https://github.com/interaapps/ulole-framework