2017-04-26 309 views
1

我想创建一个后端,用于检索某些信息并将其映射回对象。将SQL数据映射到对象

例如: 我有这个在我的模型类:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Core_Product_Model extends CI_model { 

    //get the part id from an partnumber 
    public function getIdFromPartnumber($partnumber){ 
     $this->db->select("id"); 
     $this->db->from('part_list'); 
     $this->db->where('part_number', $partnumber); 
     $query = $this->db->get(); 

     return $query->result_object(); 
    } 
} 

我处理这件事在我的控制器:

class Core_Product_Controller extends MX_Controller { 
    public function getAllCrosslinks(){ 
      $response[] = array(); 
      $response['error'] = false; 

      $partNumber = 100; 

      $output = $this->Core_Product_Model->getIdFromPartnumber($partnumber); 

      if(isset($output[0])){ 
       $response['output'] = $output; 
       $response['valid'] = true; 
      } 

      echo json_encode($response); 
     } 
} 

这工作,但不是我想要的。我想检索一切作为一个对象。这不是一个问题,但是这样做是一个好设计吗?

一些的伪代码:

类产品:

class Product { 
    private $productId; 
    private $stock; 

    function __construct($productId) { 
     $this->productId = $productId; 
    } 

    function getProductId(){ 
     return $this->productId; 
    } 

    function stock(){ 

    } 

    function setStock(Stock $stock){ 
     $this->stock = $stock; 
    } 

    function getStock(){ 
     return $this->stock; 
    } 
} 

型号:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Core_Product_Model extends CI_model { 

    //get the part id from an partnumber 
    public function getIdFromPartnumber($partnumber){ 
     $this->db->select("*"); 
     $this->db->from('part_list'); 
     $this->db->where('part_number', $partnumber); 
     $query = $this->db->get(); 

     $result = $query->result_object(); 

     //loop through result and map back to the Product class, save this to a array 

     //return the array 


     } 
} 

这是一个很好的方法做这样?

+0

在好的方法中,所有的业务逻辑必须内部控制器写! – Saty

+0

因此,映射到对象需要在控制器内完成? – da1lbi3

+0

是的,所有在控制器内完成的映射! – Saty

回答

1

你必须在模型中做到这一点 - 因为CI的结果函数可以提供这一点。

我会告诉你一个方便的方式来做到这一点(但你应该知道 - 这仅仅是个开始)在特定的例子

,你可以做以下

class Core_Product_Model extends CI_model { 

    //get the part id from an partnumber 
    public function getIdFromPartnumber($partnumber){ 
     $this->db->select("id"); 
     $this->db->from('part_list'); 
     $this->db->where('part_number', $partnumber); 
     $query = $this->db->get(); 

     return $query->result("Product"); 
    } 
} 

要由于命名冲突(例如,产品可能也是控制器的名称)并且CI无法使用命名空间,所以应该使用后缀命名这些对象(如Product_Object)

下一个想到的是,使用自动加载器作为钩子,因为您不想在您的模型中使用“require”行作为对象

只需将该类放入应用程序/钩子目录

class AppAutoLoadObjects 
{ 
    private $arrPaths = array(
     'objects/', 
    ); 


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

    public function autoloadObjects($class) 
    { 
     if (strpos($class, 'CI_') !== 0) 
     { 
      foreach ($this->arrPaths as $dir) 
      { 
       $this->counter++; 
       if (file_exists(APPPATH . $dir . '/' . $class . '.php')) 
       { 
        require_once(APPPATH . $dir . '/' . $class . '.php'); 
        return; 
       } 
      } 
     } 
    } 
} 

,并在应用/配置/ hooks.php

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