<?php
class article_model extends CI_Model
{
public function add()
{
// Note: $_POST should be filtered,
// and not accessed directly by the model
$result = $this->db->insert('article', $_POST);
if (! $result) {
log_message('error', $this->getDbErrorMessage());
}
return $result;
}
public function update($article_id, $data_fields = NULL)
{
// You may want to perform additional validation on $article_id
if (empty($article_id)) {
log_message('error', 'Invalid article ID supplied to update');
return false;
}
if ($data_fields == NULL) {
// Note: $_POST should be filtered,
// and not accessed directly by the model
$data_fields = $_POST;
}
$this->db->where("article_id", $article_id);
$result = $this->db->update('article', $data_fields);
if (! $result) {
log_message('error', $this->getDbErrorMessage());
}
return $result;
}
public function delete($id)
{
// You may want to perform additional validation on $id
if (empty($id)) {
log_message('error', 'Invalid article ID supplied to delete');
return false;
}
$this->db->where("article_id", $id);
return $this->db->delete('article');
}
//return the article with this id
public function get_article($id)
{
// You may want to perform additional validation on $id
if (empty($id)) {
log_message('error', 'Invalid article ID supplied to get_article');
return false;
}
$this->db->where("article_id", $id);
$query = $this->db->get('article');
if ($query->num_rows() > 0){
return $query->row_array();
}
log_message('error', $this->getDbErrorMessage());
return FALSE;
}
//return the available article in the table
public function get_article_all()
{
$query = $this->db->get('article');
if ($query->num_rows() > 0) {
return $query->result();
}
return null;
}
// This should probably be in MY_Model (extends CI_Model),
// which article_model would extend in place of CI_Model.
protected function getDbErrorMessage()
{
if (substr(CI_VERSION, 0, 1) != '2') {
$error = $this->db->error();
return isset($error['message']) ? $error['message'] : '';
}
return $this->db->_error_message();
}
}