CodeIgniter Model
3581 단어 CodeIgniterModel
2 Analysis model: 1) Storage location: application/models/folder, you can also create subfolders in this folder
2) Basic model class example:
The filename of the class is application/models/user_model.php
<?php
<!-- lang: php -->
class User_model extends CI_Model{
<!-- lang: php -->
function __construct(){
<!-- lang: php -->
parent::__construct();
<!-- lang: php -->
}
<!-- lang: php -->
}
<!-- lang: php -->
?>
3 Load the model in the controllerSyntax: his->load->model('Model_name);
Example: application/models/blog/queries.php
$this->load->model('blog/queries');
Once the model is loaded, it can be used in the following ways
$this->load->model('Model_name);
<!-- lang: php -->
$this->Model_name->function();
By default the model name is imported directly as the object name, as shown above, but it is also possible to have a more memorable object name such as:
$this->load->model('Model_name','fubar');
<!-- lang: php -->
$this->fubar->function();
4 Autoload Models: If you need a specific model that works across the entire project, you can have CodeIgniter autoload it on initializationMethod: Add this model to the autoload array in the applicaion/config/autoload.php file.
Note: The more memory is consumed when the model is loaded, the more memory is consumed. Automatic loading of models is at the expense of memory consumption. Try not to automatically load models that do not need to run through the entire site.
5 An example of a model:
class Blogmodel extends CI_Model{
<!-- lang: php -->
var $title = '';
<!-- lang: php -->
var $content = '';
<!-- lang: php -->
var $date = '';
<!-- lang: php -->
<!-- lang: php -->
function __construct(){
<!-- lang: php -->
parent::__construct();
<!-- lang: php -->
}
<!-- lang: php -->
<!-- lang: php -->
function get_last_ten_entries(){
<!-- lang: php -->
$query = $this->ldb->get('entires',10);
<!-- lang: php -->
return $query->result();
<!-- lang: php -->
}
<!-- lang: php -->
<!-- lang: php -->
function insert_entry(){
<!-- lang: php -->
$this->title = $_POST['title'];
<!-- lang: php -->
$this->content = $_POST['content'];
<!-- lang: php -->
$this->date = time();
<!-- lang: php -->
<!-- lang: php -->
$this->db->insert('entries',$this);
<!-- lang: php -->
}
<!-- lang: php -->
<!-- lang: php -->
function update_entry(){
<!-- lang: php -->
$this->title = $_POST['title'];
<!-- lang: php -->
$this->content = $_POST['content'];
<!-- lang: php -->
$this->date = time();
<!-- lang: php -->
<!-- lang: php -->
$this->db->update('entries',$this,array('id' =>$_POST['id']));
<!-- lang: php -->
}
<!-- lang: php -->
}
Note: Use $_POST directly, which is not very good, usually you should use the input class: $this->input->post('title');
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
jquery의 $.post를 사용하여 비동기 통신 수행 (프레임 워크 : codeigniter)messages_test.php ・ 텍스트를 2개 준비시켜, 각각에 id를 붙입니다. ・button의 type은 이번 비동기 통신이기 때문에, submit는 아니고 button를 사용. (submit라고 화면 천이해...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.