कोडइग्निटर डेटाबेस: कॉन्फ़िगरेशन, संपादन, अद्यतन, डेटा हटाना

कोडइग्निटर डेटाबेस

पिछले ट्यूटोरियल में, हमने CodeIgniter एक्टिव रिकॉर्ड की मूल बातें और डेटाबेस से रिकॉर्ड डालने, अपडेट करने, हटाने और पढ़ने के तरीके को कवर किया है। इस ट्यूटोरियल में, हम डेटाबेस मॉडल बनाएंगे और डेटाबेस रिकॉर्ड बनाने और अपडेट करने के लिए फ़ॉर्म का उपयोग करेंगे। यदि आप CodeIgniter में डेटाबेस के साथ काम करने के लिए पूरी तरह से नए हैं, तो आपको पिछला ट्यूटोरियल पढ़ने की सलाह दी जाती है

कोडइग्निटर डेटाबेस कॉन्फ़िगरेशन

हम ट्यूटोरियल प्रोजेक्ट डेटाबेस बनाकर शुरू करेंगे। हम संपर्क विवरण प्रबंधित करने के लिए एक सरल डेटाबेस बनाएंगे। हम दो (2) तालिकाओं के साथ एक सरल डेटाबेस बनाएंगे, जिसमें pals और वे शहर शामिल होंगे, जहाँ वे रहते हैं। pals और शहरों के बीच संबंध एक-से-एक है, जिसमें शहरों में 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`)
) ;

कोडइग्निटर डेटाबेस मॉडल

अब हम अपने डेटाबेस के लिए मॉडल बनाएंगे। मॉडल डेटाबेस का M भाग है। MVCयह मॉडल डेटा एक्सेस, डेटा मैनिपुलेशन और बिजनेस लॉजिक से संबंधित है।

कोड इग्निटर में, प्रत्येक मॉडल को उन विधियों को परिभाषित करना होता है जिनका वह समर्थन करेगा। प्रत्येक मॉडल में एक ही कोड को दोहराने के बजाय, हम ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग में इनहेरिटेंस का लाभ उठाएंगे और एक पैरेंट मॉडल क्लास बनाएंगे जो उन बुनियादी विधियों को परिभाषित करेगा जिनका हम चाहते हैं कि हमारे मॉडल समर्थन करें।

नीचे दी गई तालिका उन विधियों को दर्शाती है जिन्हें हम परिभाषित करेंगे तथा डेटा तक कैसे पहुंच बनाई जाएगी।

एस / एन विधि विवरण
1 __construct उस कंस्ट्रक्टर विधि को परिभाषित करता है जो पैरेंट कंस्ट्रक्टर विधि को कॉल करता है
2 सब_पाओ बिना किसी शर्त के डेटाबेस से सभी फ़ील्ड और रिकॉर्ड पुनर्प्राप्त करता है
3 get_by_id id नामक INT प्रकार की प्राथमिक कुंजी का उपयोग करके डेटाबेस से एकल पंक्ति पुनर्प्राप्त करता है
4 कहाँ_पहुँचें डेटाबेस से सभी फ़ील्ड को मानदंड के आधार पर पुनर्प्राप्त करता है
5 डालने के डेटाबेस में एक नया रिकॉर्ड सम्मिलित करता है
6 अद्यतन id नामक INT प्रकार की प्राथमिक कुंजी के आधार पर मौजूदा डेटाबेस रिकॉर्ड को अपडेट करता है
7 हटाना id नामक INT प्रकार की प्राथमिक कुंजी के आधार पर डेटाबेस से मौजूदा रिकॉर्ड को हटाता है

निम्नलिखित छवि क्लास आरेख को दर्शाती है तथा यह भी दर्शाती है कि Pals और Cities चाइल्ड मॉडल, पैरेंट मॉडल BaseModel से किस प्रकार संबंधित हैं।

कोडइग्निटर डेटाबेस मॉडल

हम ऊपर दी गई छवि में बताए अनुसार दो मॉडल बनाएंगे

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);
    }
}

यहाँ,

  • संरक्षित $table = "; टेबल नामक एक संरक्षित चर को परिभाषित करता है। यह संबंधित चाइल्ड क्लास द्वारा पॉपुलेट किया जाएगा ताकि यह निर्दिष्ट किया जा सके कि हमारे बेस मॉडल क्लास विधियों को किस टेबल के साथ इंटरैक्ट करना चाहिए।
  • public function __construct() {…} कंस्ट्रक्टर विधि को परिभाषित करता है और पैरेंट क्लास CI_Model की कंस्ट्रक्टर विधि को निष्पादित करता है।
  • get_all() {…} हमारे डेटाबेस के विरुद्ध SELECT क्वेरी चलाने के लिए डेटाबेस लाइब्रेरी और वेरिएबल $table के मान का उपयोग करता है।
  • get_by_id($id) {…} डेटाबेस से एकल पंक्ति प्राप्त करने के लिए विधि को परिभाषित करता है और एक पैरामीटर $id स्वीकार करता है जो INT डेटा प्रकार का होना चाहिए।
  • get_where($where) {…} get विधि को परिभाषित करता है जो आपको where क्लॉज़ सेट करने की अनुमति देता है।
  • insert($data) {…} insert विधि को परिभाषित करता है और सरणी पैरामीटर $data को स्वीकार करता है जिसमें डेटाबेस में लिखे जाने वाले मान होते हैं।
  • update($id, $data) {…} अद्यतन विधि को परिभाषित करता है और सरणी पैरामीटर $data को स्वीकार करता है जिसमें डेटाबेस में अद्यतन किए जाने वाले मान शामिल होते हैं।
  • delete($id) {…} डिलीट विधि को परिभाषित करता है जो $id के पैरामीटर को स्वीकार करता है जो कि डेटा प्रकार INT का होना चाहिए।

अब जबकि हमने पैरेंट मॉडल क्लास का काम पूरा कर लिया है, तो चलिए अपना पाल्स मॉडल बनाते हैं

application/models/Pals.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();
    }
}

यहाँ,

  • class Pals extends BaseModel {…} पैरेंट मॉडल BaseModel को विस्तारित करता है और स्वचालित रूप से BaseModel में परिभाषित सभी विधियों को चाइल्ड क्लास तक पहुँच योग्य बनाता है।
  • संरक्षित $table = 'pals'; हमारे पैरेंट मॉडल से जुड़े टेबल नाम को परिभाषित करता है
  • __construct() {…} पैरेंट कंस्ट्रक्टर को आरंभ करता है
  • पब्लिक फ़ंक्शन get_by_id($id) {…} Pals मॉडल के लिए विशिष्ट कस्टम कार्यान्वयन प्रदान करने के लिए get_by_id को ओवरराइड करता है। get_by_id के लिए क्वेरी cities टेबल से शहर का नाम प्राप्त करने के लिए जॉइन का उपयोग करती है
  • सार्वजनिक फ़ंक्शन get_all() {…} pals और cities टेबल के बीच जॉइन क्वेरी को लागू करने के लिए get_all विधि को ओवरराइड करता है

application/models/Cities.php में एक नई फ़ाइल बनाएँ

<?php
class Cities extends BaseModel {
    protected $table = 'cities';
    
    public function __construct() {
        parent::__construct();
    }
}

यहाँ,

protected $table = 'cities'; मॉडल डेटाबेस तालिका को परिभाषित करता है।

जैसा कि आप ऊपर दिए गए कोड से देख सकते हैं, कोडइग्निटर में मॉडल के साथ काम करते समय इनहेरिटेंस हमें बहुत समय बचाता है। अगले भाग में, हम सीखेंगे

संपर्क प्रबंधक नियंत्रक

अब जब हमने मॉडल बना लिए हैं तो चलिए अपने एप्लीकेशन के लिए कंट्रोलर बनाते हैं। हमारे पास दो कंट्रोलर होंगे जिनका नाम होगा कॉन्टैक्ट्स और सिटीज

आइये शहरों से शुरुआत करें

एप्लीकेशन/कंट्रोलर्स निर्देशिका में एक नई फ़ाइल 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'));
    }
}

यहाँ,

उपरोक्त कोड डेटाबेस से पंक्तियों को बनाने, अद्यतन करने, हटाने और पढ़ने के लिए आवश्यक सभी विधियों को कार्यान्वित करता है।

एप्लिकेशन/कंट्रोलर्स में एक और फ़ाइल 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'));
    }
}

संपर्क प्रबंधक दृश्य

हमने पिछले ट्यूटोरियल में CodeIgniter में फॉर्म और वैलिडेशन को पहले ही देख लिया है। हम पिछले ट्यूटोरियल में विकसित किए गए कोड का उपयोग करेंगे। पूर्णता के लिए, हम पिछले ट्यूटोरियल में बनाए गए कोड को फिर से प्रस्तुत करेंगे।

हमारे आवेदन के विचार इस प्रकार होंगे

संपर्क प्रबंधक दृश्य

आप नीचे दिए गए लिंक पर क्लिक करके उपरोक्त दृश्यों के लिए कोड डाउनलोड कर सकते हैं

कोड इग्नाइटर संपर्क प्रबंधक दृश्य डाउनलोड करें

सारांश

इस ट्यूटोरियल में, आप सीखेंगे कि मॉडल कैसे बनाएं CodeIgniterहमने ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग में इनहेरिटेंस का लाभ उठाया, ताकि एक आधार मॉडल बनाकर कोड की पुनः प्रयोज्यता को बढ़ावा दिया जा सके, जो चार प्रमुख डेटाबेस ऑपरेशनों - सम्मिलित करना, पढ़ना, अद्यतन करना और हटाना - को कार्यान्वित करता है।

हमने व्यावहारिक अनुप्रयोग का उपयोग करके अवधारणाओं का प्रदर्शन किया है, और हम अगले ट्यूटोरियल में अनुप्रयोग में और अधिक कार्यक्षमता जोड़कर ऐसा करना जारी रखेंगे।