0
私が持っているものは、製品クラスです。そのIDまたはその製品名で製品を入手できます。だから私は2つのコンストラクタを作成しました。クラスは、データベースを介して製品を検索し、その結果をクラス変数にマッピングしています。クラス内のAcces属性
class Partnumber extends CI_Model
{
private $partNr;
private $description;
private $type;
public function __construct() {
}
public static function withId($id) {
$instance = new self();
$instance->loadByID($id);
return $instance;
}
public static function withNr($partnumber) {
$instance = new self();
$instance->getIdFromPartnumber($partnumber);
return $instance;
}
protected function loadByID($id) {
$instance = new self();
$instance->getPartnumberFromId($id);
return $instance;
}
private function getIdFromPartnumber($partnumber){
$this->db->select("*");
$this->db->from('part_list');
$this->db->where('part_number', $partnumber);
$query = $this->db->get();
return $query->result_object();
}
//get the partnumber from an part id
private function getPartnumberFromId($partId){
$this->db->select("*");
$this->db->from('part_list');
$this->db->where('id', $partId);
$query = $this->db->get();
$this->mapToObject($query->result());
}
private function mapToObject($result){
$this->partNr = $result[0]->Part_number;
$this->description = $result[0]->Description;
$this->type = $result[0]->Type;
}
public function toJson(){
return json_encode($this->partNr);
}
}
マッピングが機能します(私はエラーをキャッチする必要があります)。しかし、toJsonメソッドを呼び出すと、すべての値がnullになります。
私はこのようにそれを呼び出す:
class TestController extends MX_Controller{
public function __construct(){
parent::__construct();
$this->load->model('Partnumber');
}
public function loadPage() {
$p = Partnumber::withId(1);
echo $p->toJson();
}
}
そして、私はマッピング方法のすべての項目を印刷することができますので、はい、私は、データが戻って来ていることを確実に知ります。しかし、なぜ私がtoJson経由でアクセスするとデータがなくなったのですか?
うわー、私はそれを見ます。ありがとう! – da1lbi3