PHP Codeigniter模型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Codeigniter模型相关的知识,希望对你有一定的参考价值。

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed.');

	class News_model extends Model {
		function News_model(){
			parent::Model();

			$this->fields = explode(',', 'post_id,main_id,title,body,post_date');
			$this->table = 'news';
			$this->key = 'post_id';
			$this->order = 'post_date desc';

			$this->reset();
		}

		function reset(){
			foreach($this->fields as $field){
				$this->$field = null;
			}
		}

		function select(){
			$result = false;

			foreach($this->fields as $field){
				if($this->$field){
					$this->db->where($field, $this->$field);
				}
			}

			$this->db->order_by($this->order);
			$query = $this->db->get($this->table);

			if($this->post_id){
				$result = $query->row();
			} else{
				$result = $query->result();
			}

			return $result;
		}

		function update(){
			$result = false;

			foreach($this->fields as $field){
				$entry[$field] = $this->$field;
			}

			if($this->post_id){
				$this->db->where($this->key, $this->post_id);
				$this->db->update($this->table, $entry);
			} else{
				$this->db->insert($this->table, $entry);
			}

			return $result;
		}

		function insert(){
			return $this->update();
		}

		function delete(){
			if($this->post_id){
				$this->db->where($this->key, $this->post_id);
				$this->db->delete($this->table);
			}
		}
	}
?>

以上是关于PHP Codeigniter模型的主要内容,如果未能解决你的问题,请参考以下文章

PHP codeigniter标准crud模型

PHP Codeigniter模型

PHP 如何在CodeIgniter中编写更好的模型

UnitTest一个调用模型的PHP CodeIgniter控制器

将数组作为参数传递给PHP CodeIgniter中的模型

Codeigniter:调用未定义的函数(模型)