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


Messages In This Thread
Custom ORM - Suggestions - by milosh-96 - 03-10-2019, 03:47 PM
RE: Custom ORM - Suggestions - by ciadmin - 03-10-2019, 04:11 PM
RE: Custom ORM - Suggestions - by milosh-96 - 03-10-2019, 04:41 PM
RE: Custom ORM - Suggestions - by ciadmin - 03-10-2019, 05:17 PM
RE: Custom ORM - Suggestions - by milosh-96 - 03-10-2019, 07:08 PM



Theme © iAndrew 2016 - Forum software by © MyBB