CodeIgniter Forums
Get image url from model good practice? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Get image url from model good practice? (/showthread.php?tid=63496)

Pages: 1 2


RE: Get image url from model good practice? - sintakonte - 11-11-2015

i would never ever attach a class which represents a data object together with a model because there are cases where you just need the object
so this is in my opinion a wrong approach

thats because i wrote the autoloader as a hook because of its clean technique. You dont have to be worried about another hook who eventually needs an object because it gets as a pre controller hook loaded and you will see if your system grows and grows this approach helps you a lot

@dubefx

i know the customer result object function and it gets used if i dont need to initialize my custom objects or attach any other object to
but in most cases i need to iterate over (but of course your approach is right too)


RE: Get image url from model good practice? - sebastianvirlan - 11-11-2015

Ok so I used your idea and merged to create custom objects on another projects, here is the code and please tell me if is ok:


hooks.php

PHP Code:
$hook['pre_system'][] = array(
 
   'class' => 'AppAutoLoadObjects',
 
   'function' => 'initialize',
 
   'filename' => 'AppAutoLoadObjects.php',
 
   'filepath' => 'hooks'
); 

AppAutoLoadObjects.php

PHP Code:
class AppAutoLoadObjects
{

 
   public function initialize()
 
   {
 
       spl_autoload_register(array($this,'autoloadCoreObjects'));
 
   }

 
   public function autoloadCoreObjects($class)
 
   {
 
       $path = array(
 
           'objects/',
 
       );

 
       $class explode('_'$class);
 
       foreach($path as $dir) {
 
           if (file_exists(APPPATH.$dir.$class[0]."_Object".'.php'))
 
               require_once(APPPATH.$dir.$class[0]."_Object".'.php');
 
       }
 
   }




Proprietati_model.php

PHP Code:
   function select_by_limit($start$limit) {
 
       $this->db->from($this->_table.' a');
 
       $this->db->join('zone b'"a.id_zona = b.id_zona"'left');
 
       $this->db->join('tip_proprietate c'"a.id_tip_proprietate = c.id_tip_proprietate");
 
       $this->db->limit($limit$start);
 
       $query $this->db->get_compiled_select();
 
       $result $this->db->query($query);
 
       return $result->custom_result_object('Proprietati_Object');
 
   

and Proprietati_Object.php

PHP Code:
class Proprietati_Object
{

 
   private $resolution;

 
   public function __construct(){

 
       $this->_ci get_instance();
 
   }

 
   public function get_block_caracteristics(){
 
       if($this->nume_tip_proprietate == 'apartament')
 
           $this->_ci->load->view('blocks/apartament', array('proprietate' => $this));
 
       elseif($this->nume_tip_proprietate == 'teren')
 
           $this->_ci->load->view('blocks/teren'$this);
 
   }



Is this ok?


RE: Get image url from model good practice? - sintakonte - 11-12-2015

it was never intended to (ab)use Data Objects to load Views
you should pass your prepared data array from a model to a controller
and in your controller load the view - as described by many many users here

if you want to follow my approach it is a bad practice to load views in a data Object like you do in your Proprietati_Object
The purpose of a data object is to clean up your models and let the objects do all the work which are related to the Objects Data

a simple example would be:
if your database contains 2 columns discount and price and you want the calculated price - than your object can do the work

PHP Code:
class Proprietati_Object
{

    public 
$discount 10;
    public 
$price 20;

    public function 
__construct(){

 
           $this->_ci get_instance();
    }
    
    public function 
getPrice()
    {
        return (
$this->price * ((100 $this->discount) / 100));
    }



and i don't quite get your adaption of the autoloader function

PHP Code:
   public function autoloadCoreObjects($class)
 
   {
 
       $path = array(
 
           'objects/',
 
       );

 
       $class explode('_'$class);
 
       foreach($path as $dir) {
 
           if (file_exists(APPPATH.$dir.$class[0]."_Object".'.php'))
 
               require_once(APPPATH.$dir.$class[0]."_Object".'.php');
 
       }
 
   

this does the same as my posted function except the fact that you use for some weird reason explode to split the name in 2 partials.


RE: Get image url from model good practice? - sebastianvirlan - 11-12-2015

This should fix it cuz I return the view as data:

PHP Code:
   public function get_block_caracteristics(){
 
       if($this->nume_tip_proprietate == 'apartament')
 
           return $this->_ci->load->view('blocks/apartament', array('proprietate' => $this), TRUE);
 
       elseif($this->nume_tip_proprietate == 'teren')
 
           return $this->_ci->load->view('blocks/teren', array('proprietate' => $this), TRUE);
 
   

Yes you told me that to load views in controller but I need different view for each item from the object, and in this case I need to make a foreach to the object like this:

PHP Code:
$object $this->model->get_from_db();

foreach(
$object as $obj):
 
       if($obj->nume_tip_proprietate == 'apartament')
 
           $data['block']  = $this->_ci->load->view('blocks/apartament', array('proprietate' => $this), TRUE);
 
       elseif($obj->nume_tip_proprietate == 'teren')
 
           $data['block'] = $this->_ci->load->view('blocks/teren', array('proprietate' => $this), TRUE);
endforeach;

$this->load->view('page'$data); 

Then my controller will get a mess. This view have only few lines of html from the entire page
I made that explode because $class returned Proprietati_model.

The Proprietati model file was proprietati_model.php with class Proprietati_model.

And also how can I pass a param on the constructor's class?


RE: Get image url from model good practice? - sintakonte - 11-12-2015

the autoloader only loads files from the application/objects directory
if the proprietati_model was inside of this directory than remove it and copy it to the application/models directory, because a model should not be there


RE: Get image url from model good practice? - sebastianvirlan - 11-12-2015

(11-12-2015, 03:41 AM)sintakonte Wrote: the autoloader only loads files from the application/objects directory
if the proprietati_model was inside of this directory than remove it and copy it to the application/models directory, because a model should not be there

You did not understand, public function autoloadCoreObjects($class) where $class is the name of the model yes?

Well my model name was: Proprietati_model because in my models folder this is how I use to name my models:

Properties_model.php with class Properties_model
Products_model.php  with class Products_model

Maybe your models was

Properties.php with class Properties
Products.php with class Products

and you did not need that explode for underscore.


RE: Get image url from model good practice? - sintakonte - 11-12-2015

that doesnt matter the autoloader only loads classes which are in application/objects directory as i mentioned earlier - and the code should indicate it


RE: Get image url from model good practice? - InsiteFX - 11-12-2015

In case you did not know it the spl_array methods have built in foreach
Research them.


RE: Get image url from model good practice? - sintakonte - 11-12-2015

you mean because foreach creates everytime a copy of the array you pass it to, you would consider an arrayiterator or something like that ?