Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement native ORM #289

Closed
ghost opened this issue Oct 7, 2016 · 8 comments
Closed

Implement native ORM #289

ghost opened this issue Oct 7, 2016 · 8 comments

Comments

@ghost
Copy link

ghost commented Oct 7, 2016

It would be an excellent decision native implementation of an ORM own, or take an existing one and integrate, to give more tools to developers.

@lonnieezell
Copy link
Member

Thanks for your interest! At this time, we don't plan on including an ORM, but are looking into ways to make it simpler to integrate with third-party ORMs.

However, most third-party ORMs can be used within any CodeIgniter application already. Use Composer to load it. Ensure the use_composer flag in the main config file is setup. Then make a new instance of the ORM and go to town! No special integration needed.

@ghost
Copy link
Author

ghost commented Oct 7, 2016

excellent thanks for your reply

@komputronika
Copy link

I found simple ORM for CodeIgniter 3.

https://github.com/yidas/codeigniter-model/blob/master/README.md

I think this can be used as CI4 default Model or added as optional model library.

Thanks.

@uno-de-piera
Copy link

You can implement the Eloquent ORM easily, I think it is currently the simplest and best option.

@lonnieezell
Copy link
Member

You can implement the Eloquent ORM easily, I think it is currently the simplest and best option.

I first read that as it would be a simple thing for us to build in the framework and starting laughing. I know that Taylor has said the most difficult thing he did with the framework was the ORM.

I found simple ORM for CodeIgniter 3.
https://github.com/yidas/codeigniter-model/blob/master/README.md
I think this can be used as CI4 default Model or added as optional model library.

Well... our model already handles 80%+ of that. Some of that belongs at the framework level and not the model level, and it doesn't support any form of relationship management, which is what most people want from an ORM.

But if you like it - help them modify it to work in CI4 and use it your projects.

@uno-de-piera
Copy link

A quick question @lonnieezell about how load a new service like ORM on CI4, for example Eloquent:

composer.json

"illuminate/database": "^5.7"

application/Services/DatabaseService.php

<?php
namespace App\Services;

use Illuminate\Database\Capsule\Manager;

class DatabaseService {
    public function __construct() {
        $manager = new Manager();
        $manager->addConnection([
            "driver"    => 'mysql',
            "host"      => getenv('database.default.hostname'),
            "database"  => getenv('database.default.database'),
            "username"  => getenv('database.default.username'),
            "password"  => getenv('database.default.password'),
        ]);
        $manager->bootEloquent();
    }
}

application/Models/User.php

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

    public function scopeRandom () {
        return $this->orderByRaw("RAND()")->first();
    }

    public function getNameAndEmailAttribute () {
        return sprintf('Nombre: %s, Email: %s', $this->name, $this->email);
    }
}

This is a full implementation, but, which is the best way for load a service like it?

Thanks for your help!

@lonnieezell
Copy link
Member

@uno-de-piera I would just create a new method in application/Config/Services.php if you're wanting to make it simple to reuse that connection.

@uno-de-piera
Copy link

Thanks @lonnieezell, great job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants