Welcome Guest, Not a member yet? Register   Sign In
Get image url from model good practice?
#5

i think your way of thinking is on the right way but needs to get organized

Your Problem is that you need sometimes methods or variables where it seems they don't belong to a model or a controller.
i try to explain it on your current example

First of all we need to adapt the MVC principle into a MVOC term.
make a folder in your application directory called objects

the next thing is you have to make sure that your objects get autoloaded
open your hooks.php in application/config/ and write the following down

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


after that create a class named AppAutoLoadObjects.php in your applications/hooks/ directory
this class should look like

PHP Code:
class AppAutoLoadObjects {

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

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

        foreach(
$path as $dir) {
            if (
file_exists(APPPATH.$dir.'/'.strtolower($class).'.php'))    require_once(APPPATH.$dir.'/'.strtolower($class).'.php');
        }
    }


now to your object

create a Product_Object in the application/objects/ directory
(be aware in order to avoid name collision add always the _Object Suffix to your Objects)

PHP Code:
class Product_Object
{
    private 
$rez;

    public function 
getImage()
    {
        if (
$this->nume_imagine)
        {
            
$strImage base_url("assets/uploads/".$this->id_produs."/".$this->getParsedImage());
        }
        else
        {
            
$strImage base_url('assets/images/no-product-image-available.png');
        }
        
        return 
$strImage;
    }
    
    public function 
setRez($rez)
    {
        
$this->rez $rez;
    }
    
    private function 
getParsedImage(){
 
       $image explode('.'$this->nume_imagine);
 
       return $image[0].'_'.$this->rez.'.'.$image[1];

 
   


your model should look like

PHP Code:
public function entries_by_limit($start$limit$order_by "order by id_produs asc"$rez
{
    
$this->db->select('a.*');
    
$this->db->select('b.nume_imagine'false);
    
$this->db->from("$this->_table a");
    
$this->db->join('imagini_produse b'"b.$this->_primary_key = a.id_produs""left");
    
$this->db->limit($limit$start);
    
$this->db->order_by($order_by);
    
$result $this->db->get();

    
$arrData = array();
    
    foreach(
$result->result("Product_Object") as $objProduct) {
        
$objProduct->setRez($rez);
        
$arrData[] = $objProduct;
    }
    
    return 
$arrData;


your controller function could be

PHP Code:
public function listProducts()
{
    
$arrProducts $this->produse->entries_by_limit((($page-1) * $config_pagina['per_page']), $config_pagina['per_page'], $order_by'270')
    
$arrViewData = array("produse" => $arrProducts);
    
    if (
count($arrProducts) > 0)
    {
        
$this->load->view("your_view_name"$arrViewData);
    }
    else
    {
        
$this->load->view("no_products");
    }


and finally your view
PHP Code:
<?php
foreach($produse AS $objProduct)
{
?>
    <img class="primary-image" src="<?=$objProduct->getImage(); ?>" alt="" />
<?php
}
?>


with this approach everything is on its own place
and if you want to manipulate product data you can do this in your own object and you could be sure - every time a product gets instantiated you have the right data and methods.
Reply


Messages In This Thread
RE: Get image url from model good practice? - by sintakonte - 11-10-2015, 02:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB