0

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();
}
}
Samy
  • 632
  • 4
  • 14

1 Answers1

2

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?

Community
  • 1
  • 1
Shaolin
  • 2,541
  • 4
  • 30
  • 41
  • Thanks for the information.Its useful.They way i have implemented is correct? – Samy Mar 20 '13 at 06:31
  • @MDeSilva Please do not ask people to mark/vote/accept your answers. They are welcome to choose how to act there, without any pressure. – Andrew Barber Mar 20 '13 at 10:45