Codeइग्नाइटर डेटाबेस: कॉन्फ़िगरेशन, डेटा अपडेट और डिलीट करना
⚡ स्मार्ट सारांश
CodeIgniter डेटाबेस, संपर्क प्रबंधक के लिए मॉडल और नियंत्रक बनाता है, जो इसके द्वारा समर्थित है। MySQLएक साझा बेसमॉडल इंसर्ट, रीड, अपडेट और डिलीट को इम्प्लीमेंट करता है, और चाइल्ड मॉडल पाल्स और सिटीज इसे इनहेरिट करते हैं, इसलिए कंट्रोलर पूरे एप्लिकेशन में समान डेटाबेस ऑपरेशंस का पुन: उपयोग करते हैं।
Codeइग्नाइटर डेटाबेस
पिछले ट्यूटोरियल में, हमने बुनियादी बातों को कवर किया था। CodeIgniter Active Record और डेटाबेस से रिकॉर्ड डालने, अपडेट करने, हटाने और पढ़ने का तरीका। इस ट्यूटोरियल में, हम डेटाबेस मॉडल बनाएंगे और डेटाबेस रिकॉर्ड बनाने और अपडेट करने के लिए फ़ॉर्म का उपयोग करेंगे। यदि आप डेटाबेस के साथ काम करने में बिल्कुल नए हैं, तो यह ट्यूटोरियल आपके लिए उपयोगी हो सकता है। Codeइग्नाइटर, आपको इसे पढ़ने की सलाह दी जाती है। पिछले ट्यूटोरियल पहले।
Codeइग्नाइटर डेटाबेस कॉन्फ़िगरेशन
हम ट्यूटोरियल प्रोजेक्ट डेटाबेस बनाकर शुरुआत करेंगे। हम संपर्क विवरणों को प्रबंधित करने के लिए एक सरल डेटाबेस बनाएंगे, जिसमें दो टेबल होंगी: 'pals' और 'citys', जिनके नाम पर वे रहते हैं। 'pals' और 'citys' के बीच एक-से-एक संबंध होगा, जिसमें 'citys' टेबल में 'id' प्राथमिक कुंजी होगी और 'pals' टेबल में 'city_id' विदेशी कुंजी होगी।
डेटाबेस बनाने के लिए निम्नलिखित स्क्रिप्ट चलाएँ:
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इग्नाइटर डेटाबेस मॉडल
अब हम अपने डेटाबेस के लिए मॉडल बनाएंगे। मॉडल, M भाग है। MVCयह मॉडल डेटा एक्सेस, डेटा मैनिपुलेशन और बिजनेस लॉजिक से संबंधित है।
In CodeIgniter में, प्रत्येक मॉडल को उन विधियों को परिभाषित करना होता है जिनका वह समर्थन करेगा। प्रत्येक मॉडल में एक ही कोड को दोहराने के बजाय, हम ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग में इनहेरिटेंस का लाभ उठाएंगे और एक पैरेंट मॉडल क्लास बनाएंगे जो उन बुनियादी विधियों को परिभाषित करेगी जिनका हम अपने मॉडलों द्वारा समर्थन चाहते हैं।
नीचे दी गई तालिका उन विधियों को दर्शाती है जिन्हें हम परिभाषित करेंगे तथा डेटा तक कैसे पहुंच बनाई जाएगी।
| एस / एन | विधि | विवरण |
|---|---|---|
| 1 | __construct | यह उस कंस्ट्रक्टर मेथड को परिभाषित करता है जो पैरेंट कंस्ट्रक्टर मेथड को कॉल करता है। |
| 2 | सब_पाओ | यह डेटाबेस से बिना किसी शर्त के सभी फ़ील्ड और रिकॉर्ड प्राप्त करता है। |
| 3 | get_by_id | आईडी नामक INT प्रकार की प्राथमिक कुंजी का उपयोग करके डेटाबेस से एक पंक्ति पुनर्प्राप्त करता है। |
| 4 | कहाँ_पहुँचें | दिए गए मानदंडों के आधार पर डेटाबेस से सभी फ़ील्ड्स को पुनः प्राप्त करता है। |
| 5 | डालने के | डेटाबेस में एक नया रिकॉर्ड सम्मिलित करता है। |
| 6 | अद्यतन | यह फ़ंक्शन ID नामक INT प्रकार की प्राथमिक कुंजी के आधार पर मौजूदा डेटाबेस रिकॉर्ड को अपडेट करता है। |
| 7 | हटाना | INT प्रकार की प्राथमिक कुंजी जिसका नाम id है, के आधार पर डेटाबेस से मौजूदा रिकॉर्ड को हटाता है। |
निम्नलिखित छवि क्लास डायग्राम को दर्शाती है और यह भी कि पाल्स और सिटीज चाइल्ड मॉडल पैरेंट मॉडल बेसमॉडल से कैसे संबंधित हैं।
हम ऊपर दी गई छवि में वर्णित अनुसार दो मॉडल बनाएंगे। application/models/BaseModel.php में एक नई क्लास BaseModel बनाएं और उसमें निम्नलिखित कोड जोड़ें:
<?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 = ”; एक संरक्षित चर को परिभाषित करता है जिसका नाम table है। यह संबंधित चाइल्ड क्लास द्वारा भरा जाता है ताकि यह निर्दिष्ट किया जा सके कि बेस मॉडल विधियों को किस टेबल के साथ इंटरैक्ट करना चाहिए।
- public function __construct() {…} कंस्ट्रक्टर को परिभाषित करता है और पैरेंट क्लास CI_Model के कंस्ट्रक्टर को निष्पादित करता है।
- get_all() {…} डेटाबेस लाइब्रेरी और $table के मान का उपयोग करके SELECT क्वेरी चलाता है।
- get_by_id($id) {…} एक पंक्ति को पुनः प्राप्त करता है और INT डेटा प्रकार के $id पैरामीटर को स्वीकार करता है।
- get_where($where) {…} एक गेट विधि को परिभाषित करता है जो आपको एक वेयर क्लॉज़ सेट करने की अनुमति देता है।
- insert($data) {…} डेटाबेस में लिखे जाने वाले मानों को समाहित करने वाले सरणी पैरामीटर $data को स्वीकार करता है।
- update($id, $data) {…} $data नामक सरणी पैरामीटर को स्वीकार करता है जिसमें अपडेट किए जाने वाले मान होते हैं।
- delete($id) {…} INT डेटा प्रकार का एक पैरामीटर $id स्वीकार करता है।
अब जबकि हमने पैरेंट मॉडल क्लास का काम पूरा कर लिया है, चलिए अपना पाल्स मॉडल बनाते हैं। 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 extends BaseModel {…} पैरेंट मॉडल BaseModel को एक्सटेंड करती है और स्वचालित रूप से इसके सभी तरीकों को चाइल्ड क्लास के लिए सुलभ बनाती है।
- protected $table = 'pals'; इस मॉडल से जुड़े टेबल नाम को परिभाषित करता है।
- __construct() {…} पैरेंट कंस्ट्रक्टर को इनिशियलाइज़ करता है।
- public function get_by_id($id) {…} get_by_id फ़ंक्शन को ओवरराइड करके एक कस्टम कार्यान्वयन प्रदान करता है। यह क्वेरी शहरों की तालिका से शहर का नाम प्राप्त करने के लिए एक जॉइन का उपयोग करती है।
- public function get_all() {…} pals और cities टेबल के बीच एक जॉइन क्वेरी को लागू करने के लिए get_all को ओवरराइड करता है।
application/models/Cities_model.php में एक नई फ़ाइल बनाएं:
<?php class Cities_model extends BaseModel { protected $table = 'cities'; public function __construct() { parent::__construct(); } }
यहाँ:
- protected $table = 'cities'; मॉडल डेटाबेस तालिका को परिभाषित करता है।
जैसा कि आप देख सकते हैं, इनहेरिटेंस मॉडल के साथ काम करते समय हमारा बहुत समय बचाता है। Codeइग्नाइटर।
संपर्क प्रबंधक नियंत्रक
अब जब हमने मॉडल बना लिए हैं, तो चलिए अपने एप्लिकेशन के लिए कंट्रोलर बनाते हैं। हमारे पास दो कंट्रोलर होंगे, Cities और Contacts। चलिए Cities से शुरू करते हैं। एप्लिकेशन/controllers डायरेक्टरी में Cities.php नाम की एक नई फ़ाइल बनाएं और उसमें निम्नलिखित कोड डालें:
<?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')); } }
यहाँ:
- उपरोक्त कोड डेटाबेस से पंक्तियों को बनाने, अपडेट करने, हटाने और पढ़ने के लिए आवश्यक सभी विधियों को लागू करता है।
application/controllers में Contacts.php नाम की एक और फाइल बनाएं और उसमें निम्नलिखित कोड जोड़ें:
<?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इग्निटर कॉन्टैक्ट्स मैनेजर व्यू डाउनलोड करें



