2013-03-20 42 views
0

我想使用getters/setters在我的Model.Below中存儲和檢索值是我的Controller和Model代碼。我想知道它是一個標準的正確方法來做到這一點?Codeigniter - 使用getters/setters將值從控制器傳遞到model

控制器代碼:

public function saveevent() { 

    $this->event_model->setEvent_title($this->input->post('event_title')); 
    $this->event_model->setSms_description($this->input->post('sms_description')); 
    $event_id = $this->event_model->saveEvent(); 
} 

MODEL CODE:

class Event_Model extends CI_Model{ 

private $id; 
private $event_title; 
private $sms_description; 

function __construct() { 
    parent::__construct(); 
} 

public function getId() { 
    return $this->id; 
} 

public function setId($id) { 
    $this->id = $id; 
} 

public function getEvent_title() { 
    return $this->event_title; 
} 

public function setEvent_title($event_title) { 
    $this->event_title = $event_title; 
} 

public function getSms_description() { 
    return $this->sms_description; 
} 

public function setSms_description($sms_description) { 
    $this->sms_description = $sms_description; 
} 

public function saveEvent() { 
    $data = array(
     'title' => $this->getEvent_title() , 
     'sms_description' => $this->getSms_description() 
    ); 

    $this->db->insert('events', $data); 
    return $this->db->insert_id(); 
} 
} 

回答

2

使用getter和setter方法是一個很好的做法,但這樣做是沒有必要的。但是我會建議你是否繼續使用setter和getters。

請參考以下鏈接

Why use getters and setters? Getter and Setter?

+0

感謝information.Its useful.They方式,我已經實現了是正確的? – Samy 2013-03-20 06:31:54

+0

是的,這是正確的 – Shaolin 2013-03-20 06:32:47

+0

@MDeSilva請不要讓人們標記/投票/接受你的答案。歡迎他們選擇如何在那裏行動,沒有任何壓力。 – 2013-03-20 10:45:17

相關問題