Codeฐานข้อมูล Igniter: การกำหนดค่า การอัปเดต และการลบข้อมูล
⚡ สรุปอย่างชาญฉลาด
CodeIgniter Database สร้างแบบจำลองและตัวควบคุมสำหรับระบบจัดการรายชื่อผู้ติดต่อที่ได้รับการสนับสนุนจาก MySQL. โมเดลพื้นฐานที่ใช้ร่วมกัน (BaseModel) จะทำหน้าที่แทรก อ่าน อัปเดต และลบข้อมูล และโมเดลย่อยอย่าง Pals และ Cities จะสืบทอดคุณสมบัติจากโมเดลพื้นฐานนี้ ดังนั้นคอนโทรลเลอร์ต่างๆ จึงสามารถใช้การดำเนินการฐานข้อมูลเดียวกันได้ทั่วทั้งแอปพลิเคชัน

Codeฐานข้อมูล Igniter
ในบทเรียนก่อนหน้านี้ เราได้กล่าวถึงพื้นฐานของ... CodeIgniter Active Record และวิธีการแทรก อัปเดต ลบ และอ่านข้อมูลจากฐานข้อมูล ในบทช่วยสอนนี้ เราจะสร้างแบบจำลองฐานข้อมูลและใช้ฟอร์มเพื่อสร้างและอัปเดตข้อมูลในฐานข้อมูล หากคุณยังใหม่กับงานด้านฐานข้อมูลโดยสิ้นเชิง Codeขอแนะนำให้คุณอ่านข้อความต่อไปนี้: Igniter กวดวิชาก่อนหน้านี้ ก่อน
Codeการกำหนดค่าฐานข้อมูล Igniter
เราจะเริ่มต้นด้วยการสร้างฐานข้อมูลสำหรับโปรเจ็กต์ตัวอย่าง เราจะสร้างฐานข้อมูลอย่างง่ายสำหรับการจัดการรายละเอียดการติดต่อ โดยมีสองตารางชื่อ pals และ cities ที่พวกเขาอาศัยอยู่ ความสัมพันธ์ระหว่าง pals และ cities เป็นแบบหนึ่งต่อหนึ่ง โดยใช้ id ในตาราง cities เป็นคีย์หลัก และ city_id เป็นคีย์นอกในตาราง pals
เรียกใช้สคริปต์ต่อไปนี้เพื่อสร้างฐานข้อมูล:
CREATE TABLE `pals` ( `id` int(11) NOT NULL AUTO_INCREMENT, `city_id` int(11) DEFAULT NULL, `contact_name` varchar(245) DEFAULT NULL, `contact_number` varchar(245) DEFAULT NULL, `email_address` varchar(245) DEFAULT NULL, PRIMARY KEY (`id`) );
ต่อไปนี้เรามาสร้างตารางเมืองกัน:
CREATE TABLE `cities` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(245) DEFAULT NULL, PRIMARY KEY (`id`) );
Codeโมเดลฐานข้อมูล Igniter
ต่อไปเราจะสร้างแบบจำลองสำหรับฐานข้อมูลของเรา แบบจำลองนี้คือส่วน M ของฐานข้อมูล VMC- โมเดลนี้เกี่ยวข้องกับการเข้าถึงข้อมูล การจัดการข้อมูล และตรรกะทางธุรกิจ
In Codeใน Igniter แต่ละโมเดลจะต้องกำหนดเมธอดที่มันจะรองรับ แทนที่จะเขียนโค้ดซ้ำกันในแต่ละโมเดล เราจะใช้ประโยชน์จากการสืบทอดในโปรแกรมเชิงวัตถุและสร้างคลาสโมเดลหลักที่กำหนดเมธอดพื้นฐานที่เราต้องการให้โมเดลของเราสนับสนุน
ตารางด้านล่างแสดงวิธีการที่เราจะกำหนดและวิธีการเข้าถึงข้อมูล
| S / N | วิธี | Descriptไอออน |
|---|---|---|
| 1 | __สร้าง | กำหนดเมธอดคอนสตรัคเตอร์ที่เรียกเมธอดคอนสตรัคเตอร์ของคลาสแม่ |
| 2 | รับ_ทั้งหมด | ดึงข้อมูลทุกฟิลด์และทุกระเบียนจากฐานข้อมูลโดยไม่มีเงื่อนไขใดๆ |
| 3 | get_by_id | ดึงข้อมูลแถวเดียวจากฐานข้อมูลโดยใช้คีย์หลักชนิด INT ที่ชื่อว่า id |
| 4 | รับ_ที่ไหน | ดึงข้อมูลทุกฟิลด์จากฐานข้อมูลตามเกณฑ์ที่กำหนด |
| 5 | แทรก | เพิ่มระเบียนใหม่ลงในฐานข้อมูล |
| 6 | ปรับปรุง | อัปเดตข้อมูลในฐานข้อมูลที่มีอยู่แล้ว โดยใช้คีย์หลักชนิด INT ที่ชื่อว่า id เป็นเกณฑ์ |
| 7 | ลบ | ลบข้อมูลที่มีอยู่แล้วออกจากฐานข้อมูลโดยใช้คีย์หลักชนิด INT ที่ชื่อว่า id |
ภาพต่อไปนี้แสดงไดอะแกรมคลาสและวิธีที่โมเดลลูก Pals และ Cities เกี่ยวข้องกับโมเดลแม่ BaseModel
เราจะสร้างโมเดลสองโมเดลตามที่อธิบายไว้ในภาพด้านบน สร้างคลาสใหม่ชื่อ BaseModel ในไฟล์ application/models/BaseModel.php และเพิ่มโค้ดต่อไปนี้:
<?php class BaseModel extends CI_Model { protected $table = ''; public function __construct() { parent::__construct(); } public function get_all() { return $this->db->get($this->table) ->result(); } public function get_by_id($id) { return $this->db->get_where($this->table, array('id' => $id)) ->row(); } public function get_where($where) { return $this->db->where($where) ->get($this->table) ->result(); } public function insert($data) { return $this->db->insert($this->table, $data); } public function update($id, $data) { $this->db->where('id', $id); $this->db->update($this->table, $data); } public function delete($id) { $this->db->where('id', $id); $this->db->delete($this->table); } }
ที่นี่:
- protected $table = ”; กำหนดตัวแปร protected ชื่อ table ซึ่งคลาสย่อยที่เกี่ยวข้องจะเติมค่าให้กับตัวแปรนี้ เพื่อระบุว่าเมธอดของโมเดลพื้นฐานควรโต้ตอบกับตารางใด
- public function __construct() {…} กำหนดคอนสตรัคเตอร์และเรียกใช้คอนสตรัคเตอร์ของคลาสแม่ CI_Model
- get_all() {…} ใช้ไลบรารีฐานข้อมูลและค่าของ $table เพื่อเรียกใช้คำสั่ง SELECT
- get_by_id($id) {…} ดึงข้อมูลแถวเดียวและรับพารามิเตอร์ $id ซึ่งเป็นชนิดข้อมูล INT
- get_where($where) {…} กำหนดเมธอด get ที่อนุญาตให้คุณกำหนดเงื่อนไข where ได้
- insert($data) {…} รับพารามิเตอร์อาร์เรย์ $data ซึ่งประกอบด้วยค่าที่จะเขียนลงในฐานข้อมูล
- update($id, $data) {…} รับพารามิเตอร์อาร์เรย์ $data ที่มีค่าที่จะอัปเดต
- delete($id) {…} รับพารามิเตอร์ $id ที่มีชนิดข้อมูล INT
เมื่อเราสร้างคลาสโมเดลหลักเสร็จแล้ว มาสร้างโมเดล Pals กัน สร้างไฟล์ใหม่ใน application/models/Pals_model.php และเพิ่มโค้ดต่อไปนี้:
<?php class Pals_model extends BaseModel { protected $table = 'pals'; public function __construct() { parent::__construct(); } public function get_by_id($id) { $this->db->where('pals.id', $id); $this->db->select('pals.*,cities.name'); $this->db->from('pals'); $this->db->join('cities', 'pals.city_id = cities.id'); $query = $this->db->get(); return $query->row(); } public function get_all() { $this->db->select('pals.*,cities.name'); $this->db->from('pals'); $this->db->join('cities', 'pals.city_id = cities.id'); $query = $this->db->get(); return $query->result(); } }
ที่นี่:
- คลาส Pals_model สืบทอดมาจาก BaseModel {…} สืบทอดมาจากโมเดลแม่ BaseModel และทำให้เมธอดทั้งหมดของโมเดลแม่สามารถเข้าถึงได้จากคลาสลูกโดยอัตโนมัติ
- protected $table = 'pals'; กำหนดชื่อตารางที่เชื่อมโยงกับโมเดลนี้
- __construct() {…} จะเริ่มต้นคอนสตรัคเตอร์ของคลาสแม่
- ฟังก์ชันสาธารณะ get_by_id($id) {…} จะเขียนทับเมธอด get_by_id เพื่อให้มีการใช้งานแบบกำหนดเอง การค้นหาใช้การเชื่อมต่อ (join) เพื่อดึงชื่อเมืองจากตาราง cities
- ฟังก์ชันสาธารณะ get_all() {…} จะเขียนทับเมธอด get_all เพื่อทำการเชื่อมต่อข้อมูลระหว่างตาราง pals และ cities
สร้างไฟล์ใหม่ใน application/models/Cities_model.php:
<?php class Cities_model extends BaseModel { protected $table = 'cities'; public function __construct() { parent::__construct(); } }
ที่นี่:
- ป้องกัน $table = 'เมือง'; กำหนดตารางฐานข้อมูลโมเดล
อย่างที่คุณเห็น การสืบทอดช่วยประหยัดเวลาได้มากเมื่อทำงานกับโมเดล Codeตัวจุดไฟ
ตัวควบคุมตัวจัดการรายชื่อติดต่อ
เมื่อเราสร้างโมเดลเสร็จแล้ว ต่อไปเรามาสร้างคอนโทรลเลอร์สำหรับแอปพลิเคชันของเรากัน เราจะมีคอนโทรลเลอร์สองตัว คือ Cities และ Contacts เริ่มจาก Cities ก่อน สร้างไฟล์ใหม่ชื่อ Cities.php ในไดเร็กทอรี application/controllers และเพิ่มโค้ดต่อไปนี้:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Cities extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('url', 'form'); $this->load->library('form_validation'); $this->load->model('cities_model'); } public function index() { $header['title'] = 'Cities Listing'; $data['pals'] = $this->cities_model->get_all(); $this->load->view('header',$header); $this->load->view('cities/index', $data); $this->load->view('footer'); } public function create() { $header['title'] = 'Create City'; $this->load->view('header',$header); $this->load->view('cities/create'); $this->load->view('footer'); } public function store() { $rules = array( array( 'field' => 'name', 'label' => 'City Name', 'rules' => 'required' ) ); $this->form_validation->set_rules($rules); if ($this->form_validation->run() == TRUE) { $data = array('name' => $this->input->post('name')); $this->cities_model->insert($data); redirect(base_url('cities')); } else { $header['title'] = 'Create City'; $this->load->view('header',$header); $this->load->view('cities/create'); $this->load->view('footer'); } } public function edit($id) { $header['title'] = 'Edit City'; $data['city'] = $this->cities_model->get_by_id($id); $this->load->view('header', $header); $this->load->view('cities/edit', $data); $this->load->view('footer'); } public function update($id) { $rules = array( array( 'field' => 'name', 'label' => 'City Name', 'rules' => 'required' ) ); $this->form_validation->set_rules($rules); if ($this->form_validation->run() == TRUE) { $data = array('name' => $this->input->post('name')); $this->cities_model->update($id,$data); redirect(base_url('cities')); } else { $header['title'] = 'Edit City'; $data['city'] = $this->cities_model->get_by_id($id); $this->load->view('header',$header); $this->load->view('cities/create',$data); $this->load->view('footer'); } } public function delete($id) { $header['title'] = 'Delete City'; $data['city'] = $this->cities_model->get_by_id($id); $this->load->view('header', $header); $this->load->view('cities/delete', $data); $this->load->view('footer'); } public function destroy($id) { $this->cities_model->delete($id); redirect(base_url('cities')); } }
ที่นี่:
- โค้ดด้านบนได้นำวิธีการทั้งหมดที่จำเป็นในการสร้าง อัปเดต ลบ และอ่านแถวจากฐานข้อมูลมาใช้แล้ว
สร้างไฟล์ Contacts.php อีกไฟล์หนึ่งในโฟลเดอร์ application/controllers และเพิ่มโค้ดต่อไปนี้ลงไป:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Contacts extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('url', 'form'); $this->load->library('form_validation'); $this->load->model('pals_model'); } public function index() { $header['title'] = 'Contacts List'; $data['pals'] = $this->pals_model->get_all(); $this->load->view('header', $header); $this->load->view('contacts/index', $data); $this->load->view('footer'); } public function create() { $this->load->model('cities_model'); $header['title'] = 'Create Contact'; $data['cities'] = $this->cities_model->get_all(); $this->load->view('header', $header); $this->load->view('contacts/create', $data); $this->load->view('footer'); } public function store() { $rules = array( array( 'field' => 'contact_name', 'label' => 'Contact Name', 'rules' => 'required' ), array( 'field' => 'contact_number', 'label' => 'Contact Number', 'rules' => 'required', 'errors' => array( 'required' => 'You must provide a %s.', ), ), array( 'field' => 'email_address', 'label' => 'Email Address', 'rules' => 'required' ), array( 'field' => 'city_id', 'label' => 'City', 'rules' => 'required' ) ); $this->form_validation->set_rules($rules); if ($this->form_validation->run() == FALSE) { $this->load->model('cities_model'); $header['title'] = 'Create Contact'; $data['cities'] = $this->cities_model->get_all(); $this->load->view('header', $header); $this->load->view('contacts/create', $data); $this->load->view('footer'); } else { $data = array( 'city_id' => $this->input->post('city_id'), 'contact_name' => $this->input->post('contact_name'), 'contact_number' => $this->input->post('contact_number'), 'email_address' => $this->input->post('email_address'), ); $this->pals_model->insert($data); redirect(base_url('contacts')); } } public function edit($id) { $this->load->model('cities_model'); $header['title'] = 'Edit Contact'; $data['cities'] = $this->cities_model->get_all(); $data['pal'] = $this->pals_model->get_by_id($id); $this->load->view('header', $header); $this->load->view('contacts/edit', $data); $this->load->view('footer'); } public function update($id) { $rules = array( array( 'field' => 'contact_name', 'label' => 'Contact Name', 'rules' => 'required' ), array( 'field' => 'contact_number', 'label' => 'Contact Number', 'rules' => 'required', 'errors' => array( 'required' => 'You must provide a %s.', ), ), array( 'field' => 'email_address', 'label' => 'Email Address', 'rules' => 'required' ), array( 'field' => 'city_id', 'label' => 'City', 'rules' => 'required' ) ); $this->form_validation->set_rules($rules); if ($this->form_validation->run() == FALSE) { $this->load->model('cities_model'); $header['title'] = 'Create Contact'; $data['cities'] = $this->cities_model->get_all(); $data['pal'] = $this->pals_model->get_by_id($id); $this->load->view('header', $header); $this->load->view('contacts/edit', $data); $this->load->view('footer'); } else { $data = array( 'city_id' => $this->input->post('city_id'), 'contact_name' => $this->input->post('contact_name'), 'contact_number' => $this->input->post('contact_number'), 'email_address' => $this->input->post('email_address'), ); $this->pals_model->update($id, $data); redirect(base_url('contacts')); } } public function delete($id) { $this->load->model('cities_model'); $header['title'] = 'Delete Contact'; $data['cities'] = $this->cities_model->get_all(); $data['pal'] = $this->pals_model->get_by_id($id); $this->load->view('header',$header); $this->load->view('contacts/delete',$data); $this->load->view('footer'); } public function destroy($id){ $this->pals_model->delete($id); redirect(base_url('contacts')); } }
มุมมองตัวจัดการผู้ติดต่อ
เราได้พิจารณาแบบฟอร์มและการตรวจสอบความถูกต้องไปแล้วใน Codeเราได้พูดถึง Igniter ในบทเรียนก่อนหน้านี้ และเราจะใช้โค้ดที่เราพัฒนาขึ้นในนั้น เพื่อความสมบูรณ์ มุมมองของแอปพลิเคชันของเราจะมีลักษณะดังต่อไปนี้:
คุณสามารถดาวน์โหลดโค้ดสำหรับส่วนแสดงผลได้โดยคลิกที่ลิงก์ด้านล่าง:
Codeดาวน์โหลดมุมมองผู้จัดการรายชื่อติดต่อ Igniter


