日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Discuz!X3.1数据库的操作(三)

發(fā)布時(shí)間:2025/6/17 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Discuz!X3.1数据库的操作(三) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

MVC開(kāi)發(fā)思想簡(jiǎn)介

MVC全名是Model-View-Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫(xiě),它是一種軟件設(shè)計(jì)思想。使用一種業(yè)務(wù)邏輯,數(shù)據(jù)和顯示分離的方法組織代碼,實(shí)現(xiàn)代碼復(fù)用的最大化。

MVC的執(zhí)行流程

mvc執(zhí)行流程

模型目錄介紹

內(nèi)置模型目錄

產(chǎn)品根目錄/source/class/table/table_xxx.php

插件模型目錄

產(chǎn)品根目錄/source/plugin/插件目錄/table/table_xxx.php

模型調(diào)用方法

內(nèi)置模型調(diào)用

C::t('模型類(lèi)名')->模型方法()

1
2
3
4
5
6
7
8
<?php
? ??$data?=?C::t('common_credit_rule')->fetch_all_rule();
? ??print_r($data);
? ??
? ??//通過(guò)$action動(dòng)作取出數(shù)據(jù)
? ??$data1?=?C::t('common_credit_rule')->fetch_all_by_action(reply);
? ??print_r($data1);
?>

插件模型調(diào)用

C::t('#插件標(biāo)識(shí)符#模型類(lèi)名')->模型方法()

1
2
3
4
<?php
? ??//調(diào)用插件模型
? ??$data2?=?C::t('#licai#test_db')->test()
?>

模型基類(lèi)屬性介紹

屬性名屬性值
$_table數(shù)據(jù)表名稱(chēng)
$_pk數(shù)據(jù)表主鍵名稱(chēng)
%_pre_cache_key數(shù)據(jù)緩存Key前綴

用法:

1
2
3
$this->_table?=?'test_db';//數(shù)據(jù)庫(kù)表名稱(chēng)
$this->_pk ? ?=?'dId';//數(shù)據(jù)表主鍵名稱(chēng)
$this->_pre_cache_key?=?'test_db_';//數(shù)據(jù)緩存

模型基類(lèi)CURD方法介紹

方法名方法作用
insert()插入數(shù)據(jù)
delete()刪除數(shù)據(jù)
update更新數(shù)據(jù)
fetch()根據(jù)主鍵值查詢數(shù)據(jù)
fetch_all()根據(jù)主鍵值查詢數(shù)據(jù)組
range()查詢指定范圍的數(shù)據(jù)
count()計(jì)算數(shù)據(jù)表數(shù)據(jù)總數(shù)

用法(模型):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
? ??//防止程序跳過(guò)主程序執(zhí)行判斷
? ??if(!defined('IN_DISCUZ'))?{
? ? ? ??exit('Access Denied');
? ??}
? ??
? ??class?table_test_db?extends?discuz_table{
? ? ? ? ? ??public?function?__construct()?{
? ? ? ??
? ? ? ? ? ? ? ??$this->_table?=?'test_db';//數(shù)據(jù)庫(kù)表名稱(chēng)
? ? ? ? ? ? ? ??$this->_pk ? ?=?'dId';//數(shù)據(jù)表主鍵名稱(chēng)
? ? ? ? ? ? ? ??//$this->_pre_cache_key = 'test_db_';//數(shù)據(jù)緩存
? ? ? ??
? ? ? ? ? ? ? ? parent::__construct();
? ? ? ? ? ??}
? ? ? ? ? ??//插入數(shù)據(jù)
? ? ? ? ? ??public?function?add_name($name){
? ? ? ? ? ? ? ? ??$this->insert(array(
? ? ? ? ? ? ? ? ? ? ? ? ??'dname'=>$name
? ? ? ? ? ? ? ? ??));
? ? ? ? ? ? ? ??}
? ? ? ? ? ??//更新數(shù)據(jù)
? ? ? ? ? ??public?function?change_name_by_id($dId,$name){
? ? ? ? ? ? ? ? ??$this->update($dId,array(
? ? ? ? ? ? ? ? ? ? ? ?'dName'?=>?$name,?
? ? ? ? ? ? ? ? ??));
? ? ? ? ? ? ? ??}
? ? ? ? ? ??//刪除數(shù)據(jù)
? ? ? ? ? ??public?function?delete_by_id($dId){
? ? ? ? ? ? ? ? ? ?$this->delete($dId);
? ? ? ? ? ? ? ??}
? ? ? ? ? ??//取數(shù)據(jù)區(qū)間
? ? ? ? ? ??public?function?get_last_name(){
? ? ? ? ? ? ? ? ? ?return?$this?->?range(0,5,'DESC');
? ? ? ? ? ? ? ??}

? ? ?}
?>

用法(控制器):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
? ??header("content-type:text/html;charset=utf-8");
? ??//調(diào)用插件模型
? ??$dId?=?100;
? ??$name?=?"2121312";
? ??//C::t('#licai#test_db')->add_name($name);
? ??//C::t('#licai#test_db')->change_name_by_id('2',$name);
? ??//C::t('#licai#test_db')->delete_by_id($dId);
? ??//$data = C::t('#licai#test_db')->fetch($dId);取單條數(shù)據(jù)
? ??//print_r($data);
? ??//$data = C::t('#licai#test_db')->fetch_all(array(1,2,5));取多條數(shù)據(jù)
? ??//print_r($data);
? ??//$data = C::t('#licai#test_db')->count();計(jì)算數(shù)據(jù)
? ??//print_r($data);
? ??$data?=?C::t('#licai#test_db')->get_last_name();
? ??print_r($data);
?>

模型基類(lèi)其他方法介紹

方法名方法作用
truncate()清空數(shù)據(jù)表
optimize()優(yōu)化數(shù)據(jù)表
checkpk檢查主鍵是否設(shè)置
fetch_all_field()取出所有字段
getTable()獲取表名稱(chēng)

用法

1
2
3
4
5
6
7
8
9
10
11
12
<?php
? ??header("content-type:text/html;charset=utf-8");
? ??//調(diào)用插件模型
? ??$dId?=?100;
? ??$name?=?"2121312";
? ??//C::t('#licai#test_db')->truncate();//清空數(shù)據(jù)表
? ??//C::t('#licai#test_db')->optimize();
? ??//C::t('#licai#test_db')->checkpk();//檢查是否設(shè)置pk值主鍵
? ??//$data = ?C::t('#licai#test_db')->fetch_all_field();//取出所有字段
? ??//print_r($data);
? ??//$data = ?C::t('#licai#test_db')->getTable();//獲取表名稱(chēng)
? ??//print_r($data);

轉(zhuǎn)載于:https://www.cnblogs.com/alleyonline/p/7498586.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Discuz!X3.1数据库的操作(三)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。