MVC - 从数据库中解析对象

时间:2015-11-13 09:53:54

标签: php codeigniter oop model-view-controller

我正在尝试使用最好的OOP代码来解析数据库中的数据,因此我创建了一个从hooks文件夹调用class AppAutoLoadObjects的钩子。

配置/ 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。

1 个答案:

答案 0 :(得分: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;
}

就是这样