Welcome Guest, Not a member yet? Register   Sign In
Custom ORM - Suggestions
#1

Hello,

I switched from Laravel to CodeIgniter, I really like simplicity" and freedom that CI brings.

Since I only miss Eloquent in CodeIgniter I started working on my own. My plan isn't something advanced, I want to have relationships and some methods like isSomething().

How it works:

In models I have methods like all(), find($id)... In those methods are $this->db queries. Also, I have Entities. You can see that I run foreach in all() method and there I create Entity instances.

I really like this because I can have relationships or some methods that I can access everytime without writing new queries. For example, I have method isPublic() that checks if column "access_level" isn't set to "private".

There should be something like BaseEntity class or interface that each Entity will extend/use but this is just a prototype. The one thing that really annoys me is this "require_once" call at the top. Is there a better way to load those classes?

I would like to know what do you think about this and what should be improved


Thanks. I appreciate any suggestions.

(sorry if I posted in a wrong subforum)

Controller:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends CI_Controller {
    public function 
index()
    {
        
$data = ["content"=>"welcome_message","users"=>$this->user_model->all()]; // Users will be array of Entity instances
        

        // Serve different index if an user is logged in //
        
if($this->session->user) {
            
$this->load->model("link_model");
            
            
$data["user"] = $this->user_model->find($this->session->user->id); // Entity instance
            
$data["content"]="user/index";

        }
        
//
        
$this->load->view('templates/layout',$data);
    } 


Model:

PHP Code:
<?php

require_once(APPPATH.'entities\User_entity.php');

class 
User_model extends CI_Model {

 
   public function all() {
 
       $query $this->db->get("users");
 
       $results = [];
 
       foreach($query->result() as $row) {
 
           $results[] = new User_entity($row);
 
       }
 
       return $results;
 
   }

 
   public function find($id) {
 
       $query $this->db->get_where("users",$id);
 
       foreach($query->result() as $row) {
 
           return new User_entity($row);
 
       }
 
   

Entity:
PHP Code:
<?php 
require_once(APPPATH.'entities\Folder_entity.php');

class 
User_entity {
 
   public $id;
 
   public $user_name;
 
   public $email;
 
   private $db;

 
   public function __construct($row) {
 
       $this->id $row->id;
 
       $this->user_name $row->user_name;
 
       $this->email $row->email;
 
       $ci = & get_instance();
 
       $this->db $ci->db;
 
   }

 
   public function folders() {
 
      $this->db->select('folders.id,folders.title,folders.access_level',FALSE);
 
      $this->db->from('folders,users',FALSE);
 
      $this->db->where('folders.user_id','users.id',FALSE);

 
      $query $this->db->get();

 
      $results = [];
 
      foreach($query->result() as $row) {
 
           $results[] = new \Folder_entity($row);
 
      }
 
      return $results;
 
   }



Reply
#2

This sounds a bit like https://codeigniter4.github.io/CodeIgnit...eling-data, no?
Reply
#3

I haven't checked that link so far. It's definitely what I want to have on my CI 3 project.
Reply
#4

That link is for CI4, definitely not compatible with CI3 ... could be a reason to look at migrating to the new version Undecided
Reply
#5

I didn't express myself correctly in the last post, CI4 Entities are the best example of what I want to build for my CI3 project.

I will consider CI4 for new projects but for now I will continue to work on this.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB