いくつかの情報を取得してオブジェクトにマップするバックエンドを作成します。オブジェクトへの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
}
}
が、これはこの方法でそれを行うには良い方法ですか?
良いアプローチでは、すべてのビジネスロジックは、コントローラ内に記述する必要があります! – Saty
オブジェクトへのマッピングはコントローラ内部で行う必要がありますか? – da1lbi3
はい、すべてのマッピングがコントローラ内で実行されました! – Saty