Home  >  Article  >  Backend Development  >  Extracting CodeIgniter’s database access class_PHP tutorial

Extracting CodeIgniter’s database access class_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:46:02691browse

Well, due to organizational needs, I have recently started to switch to PHP again. The business logic is okay, mainly because the boss requires login verification to be added to the data access layer.
In fact, this requirement is reasonable. Internet services require the upper layer to protect the lower layer, but the lower layer cannot fully trust the upper layer. But the problem arises. There are two options:
1. Write a mysql proxy server to assemble the request from the caller and then return it to the calling side. The main difficulty in doing this is:
a) Assembly and serialization of SQL statements
b) Data set serialization. Although there are many products in this area, it is still too complicated and there is no time to fiddle with it
Give up decisively.
2. Encapsulate a layer of mysql API, and the caller can directly call it locally. In this case, you only need to consider the assembly of SQL statements. There are many choices now,
a) Use a model class similar to Model in django
b) Use Active Record in ci
Although the Model method is better at shielding the data layer, the team members generally believe that this method is too heavy. If it is lighter, they finally chose AR in CodeIgniter.
OK, now, it’s time to test whether the ci module splitting is good or not!
I won’t go into the details of the hard work involved, but let’s just talk about my final implementation: copy system database to a separate directory, x:/php/.
Create a file myconfig.php:
define('BASEPATH', dirname(__FILE__).'/');
define('EXT', '.php');
require_once(BASEPATH . 'database/DB' . EXT);

function &instantiate_class(&$class_object)
{
Return $class_object;
}

function log_message($level = 'error', $message, $php_error = FALSE)
{
echo($message);
}
function MYDB()
{
$params = array(
'dbdriver' => 'mysql',
        'hostname' => 'localhost',
         'username' => 'root',
          'password' => '',
'database' => 'dante',
         'pconnect' => TRUE,
          'db_debug' => FALSE,
         'cache_on' => FALSE,
         'char_set' => 'utf-8',
'dbcollat' => 'utf8_general_ci',
);
$db = DB($params,TRUE);
Return $db;
}
?>
Create a test file test.php:

require_once('myconfig.php');
$db = MYDB();
$db->select('ID,user_login,user_email');
$query = $db->get('wp_users');
echo "n";
foreach ($query->result() as $row)
{
Print $row->ID . "n";
Print $row->user_login . "n";
Print $row->user_email . "n";
}
?>
The input results are as follows:

Database Driver Class Initialized
1
admin
zny2008@gmail.com
OK~~~ In this way, if we need to perform permission verification before data access, we only need to make a judgment in the MYDB function.
In addition, I have to say that the ci module split is really good. instantiate_class comes from its systemcodeigniterCommon.php. I rewrote log_message because each caller wants to write logs in a different way. (For example, I printed it directly on the screen this time...), I happened to be looking at the design pattern recently, and this method is also in line with the template method pattern.

Author "wolf's personal space"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478617.htmlTechArticleWell, because of organizational needs, I have recently started to switch to PHP again. The business logic is okay, mainly because of the boss’s requirements Add login verification to the data access layer. In fact, this request is reasonable...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn