SQL 数据映射到对象
SQL Data mapping to objects
我想创建一个后端来检索一些信息并将其映射回对象。
例如:
我的模型中有这个 class:
<?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 产品:
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
}
}
这样做是不是一个好方法?
您必须在模型中执行此操作 - 因为 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");
}
}
Be aware, due to naming conflicts (e.g. Product could be the name of a controller too) and the inability of CI to use namespaces you should name these Objects with a suffix (like Product_Object)
接下来想到的是,使用自动加载器作为挂钩,因为您不想为模型中的对象使用 "require" 行
只需将此 class 放入您的 application/hooks 目录
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;
}
}
}
}
}
并在 application/config/hooks.php
$hook['pre_system'] = [
[
'class' => 'AppAutoLoadObjects',
'function' => 'initialize',
'filename' => 'AppAutoLoadObjects.php',
'filepath' => 'hooks'
]
];
我想创建一个后端来检索一些信息并将其映射回对象。
例如: 我的模型中有这个 class:
<?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 产品:
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
}
}
这样做是不是一个好方法?
您必须在模型中执行此操作 - 因为 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");
}
}
Be aware, due to naming conflicts (e.g. Product could be the name of a controller too) and the inability of CI to use namespaces you should name these Objects with a suffix (like Product_Object)
接下来想到的是,使用自动加载器作为挂钩,因为您不想为模型中的对象使用 "require" 行
只需将此 class 放入您的 application/hooks 目录
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;
}
}
}
}
}
并在 application/config/hooks.php
$hook['pre_system'] = [
[
'class' => 'AppAutoLoadObjects',
'function' => 'initialize',
'filename' => 'AppAutoLoadObjects.php',
'filepath' => 'hooks'
]
];