2015-11-13 45 views
0

我想讓我最好的OOP代碼解析對象與數據庫中的數據,所以我創建了一個鉤子,從鉤子文件夾中調用class AppAutoLoadObjectsMVC - 解析來自數據庫的對象

配置/ hooks.php

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

鉤/ AppAutoLoadObjects.php

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.$class."_Object".'.php')) 
       require_once(APPPATH.$dir.$class."_Object".'.php'); 
     } 
    } 

} 

正如你在我有一個objects文件夾,我需要的對象解析器的代碼中看到。

所以,如果我有models/Products_model.phpautoloadCoreObjects自動加載objects/Products_Object.php

然後在我的Products_model.php我使用的各項功能:

public function select_by_limit($start, $limit, $resolution) { 
    .......................................... 
    $query = $this->db->get_compiled_select(); 
    $result = $this->db->query($query); 
    return $result->custom_result_object('Products_Object'); 
    } 

所以我從數據庫項目對象進行解析在Products_Object.php

class Proprietati_Object 
{ 

    private $_resolution; 

    public function __construct($resolution = 270){ 
     $this->_resolution = $resolution; 
     $this->_ci = get_instance(); 
    } 

    //here is where I check if any image in database and if not give a default 
    public function image(){ 

     if($this->image_name): 
      return base_url('assets/uploads/'.$this->id_proprietate.'/'.$this->image_resolution()); 
     else: 
      return base_url('assets/images/no-product-image-available.png'); 
     endif; 
    } 
    //here is where I load a small part of view as string because I must show in view different html code for each product_type 
    public function get_block_caracteristics(){ 
     if($this->product_type == 'apartament') 
      return $this->_ci->load->view('blocks/apartament', array('product' => $this), TRUE); 
     elseif($this->product_type == 'land') 
      return $this->_ci->load->view('blocks/land', array('product' => $this), TRUE); 
    } 

    //here is where I set the image resolution and depends on each page where I show the products. E.g. 100, 200, 500 
    private function image_resolution() { 

     $image = explode('.', $this->image_name); 
     return $image[0].'_'.$this->_resolution.'.'.$image[1]; 
    } 
} 

有了這個方法,我有我的控制器乾淨,只有我使用:

$products = $this->products->select_by_limit(0, 10); 
$data['products'] = $products; 

然後在視圖中:

<?php foreach($products as $product): ?> 

<?= $product->image() ?> 
<?= $product->get_block_caracteristics() ?> 

<?php endforeach; ?> 

我的問題是如何將$resolution變量從模型傳遞給Products_Object構造函數?或者,也許我的方法不是一個好方法?

我現在這是一個非常詳細的問題,但我很久以前就處理了這個問題,我的目的是開始使用乾淨的控制器和模型進行編碼。我使用的框架是CodeIgniter。

+0

我已經解釋了這一主題HTTP一切://forum.codeigniter。 com/thread-63496.html – sintakonte

+0

是的,但在上次討論中,我不明白我如何將$分辨率傳遞給類,爲什麼不能將html的某些部分加載爲字符串上課嗎?如果不是一個好的方法來加載HTML的部分我可以在哪裏? –

+0

我發佈了一個答案,但如果你已經理解我在CI論壇上的做法 - 你自己就會知道答案...... – sintakonte

回答

0

模型

public function select_by_limit($start, $limit, $resolution) 
{ 
    $query = $this->db->get_compiled_select(); 
    $result = $this->db->query($query); 

    $arrData = array(); 

    foreach($result->result("Proprietati_Object") AS $objProduct) 
    { 

     $objProduct->setResolution($resolution); 
     $arrData[] = $objProduct; 
    } 

    return $arrData; 
} 

,並在您Proprietati_Object添加一個名爲setResolution

public function setResolution($resolution) 
{ 
    $this->_resolution = $resolution; 
} 

功能,那它