CI Framework Custom database query name (method)

Source: Internet
Author: User

For people who have just contacted PHP and the CI framework, I am also a small white

Reason: The official database Query method may not be conducive to memory use, some of the official methods can only be basic query, each compound query requires stitching conditions, the definition of their own method directly call method can be

Operation Steps:

1.ci Frame Download Good unpacked folder will have application this directory, in the application directory found models subdirectory, in the models subdirectory to build a PHP file (name casually, such as base_model.php)

This file is for you to write your own definition of the database query method, but the beginning to write (as follows), the method of its own definition is written under the __construct method

  

<?php
Class Base_model extends Ci_model
{
Public $table = "";
Public $error = "";//error message generated by Operation database, self-defined
Public $primary _key = "";//PRIMARY key

Public Function __construct ()
{
if (!isset ($this->db)) {
$this->load->database ();
}
}
}

2. Then you have the table in the database, you build the corresponding model in the models directory (for example, a user table, you can build a user.php model), and so on, and then write in the model, do not write more, (you can also write the method in this, I ignore)

<?php
Require_once "base_model.php"; Both the model and the base_model.php are in the models directory.
Class User extends Base_model //This is the uppercase user inheritance Base_model instead of inheriting Ci_model
{
Public Function __construct ()
{
Parent::__construct ();
$this->table = "user"; This is the table name, the user table
$this->primary_key = "id"; This is the primary key self-increment ID, can not write
}
}

3. I have the method to query all and modify in base_model.php (here are two simple small examples, the composite method I will update later)

  

<?php

Class Base_model extends Ci_model
{
Public $table = "";
Public $error = "";//error message generated by Operation database
Public $primary _key = "";//PRIMARY key

Public Function __construct ()
{
if (!isset ($this->db)) {
$this->load->database ();
}
}

/**
* @param bool $onlyOne
* Query all data for this table
* No parameters to pass
*/
Public function lists ($onlyOne =true, $select = "*")
{
$this->db->from ($this->table); Here is the table name
$res = $this->db->get ();
$row = $res->result_array ();
return $row;
}

/**
* Modify Data
* Pass two arrays to $data=[' name ' and $name];
* $data for the Modified field, $where as a where condition, two are arrays
*/
Public Function Update ($DATA, $where)
{
return $this->db->update ($this->table, $data, $where);
}
}

  

4. The methods of querying and modifying above are used in the controller (the controller is available)

 

Querying all data

  $this, table name, method name (parameters to pass);
  $this->user->lists ();
//

modifying data

First, you need to have the modified data (array) $data, where condition (array) $where

$data =[' id ' =>1, ' name ' = ' Lisi '];
$where =[' id ' + $id];
$this, table name, method name (parameters to pass);
$this->user->update ($data, $where);




CI Framework Custom database query name (method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.