doubao12345 2013-03-20 06:17
浏览 41
已采纳

Codeigniter - 使用getter / setter将值从控制器传递给模型

I would like to use getters/setters to store and retrieve values in my Model.Below is my Controller and Model code. I would like to know its a standard right way to do that?

CONTROLLER CODE:

  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();
}
}
  • 写回答

1条回答 默认 最新

  • dpoh61610 2013-03-20 06:19
    关注

    Using getters and setters is a good practice, but it's not necessary to do so. But I would recommend if you continue using setter and getters.

    Refer these links

    Why use getters and setters? Getter and Setter?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料