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?

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

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数