AutoCRUD - PHP 下的透明持久層
AutoCRUD是一個php下的持久層,雖然它目前只支援MySQL資料庫,但是它無需配置,也很方便實用。
已建立下面幾個表為例
CREATE TABLE `article` (
`articleid` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`content` text,
`datetimestamp` int(10) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`articleid`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
CREATE TABLE `article2category` (
`article` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
`category` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`article`,`category`)
) TYPE=MyISAM;
CREATE TABLE `category` (
`categoryid` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`parent` smallint(5) UNSIGNED DEFAULT '0',
PRIMARY KEY (`categoryid`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
CREATE TABLE `comment` (
`commentid` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` varchar(25) DEFAULT '',
`author` varchar(255) DEFAULT NULL,
`article` smallint(6) NOT NULL DEFAULT '0',
PRIMARY KEY (`commentid`),
KEY `article` (`article`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
CREATE TABLE `user` (
`userid` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL DEFAULT '',
`password` varchar(255) DEFAULT '',
PRIMARY KEY (`userid`),
UNIQUE KEY `username` (`username`)
) TYPE=MyISAM AUTO_INCREMENT=1;
基本操作
1 初始化
<?php
include ('autocrud/autocrud.php');
$crud = new AutoCRUD;
$result = $crud->connect ('sa', '', 'demo');
if (autocrud_is_error($result) == true) {
// error occured, find out what error
echo $result->getCode() . ': ' . $result->getMessage();
die();
}
$crud->generate(); // 該函式會自動生成一些存取資料所用的程式碼到資料庫裡面
?>
2 插入紀錄
<?php
// ...
// setup AutoCRUD
// ...
$new_row = array('title' => 'My article', 'content' => 'Article content goes here', 'datetimestamp' => time());
$result = $crud->article->insert ($new_row);
if (autocrud_is_error($result) == true) {
// error occured, find out what error
echo $result->getCode() . ': ' . $result->getMessage();
die();
}
echo 'Row inserted';
?>
3 更新紀錄
<?php
$row = array('title' => 'New title 2', 'content' => 'New Article content goes here', 'datetimestamp' => time());
$id = '1';
$crud->article->where = "articleid = " . $crud->quote($id);
$result = $crud->article->update ($row);
// we left out the title, so error should be returned
if (autocrud_is_error($result) == true) {
// error occured, find out what error
echo $result->getCode() . ': ' . $result->getMessage();
die();
}
echo 'Record updated';
?>
4 刪除紀錄
<?php
// ...
// setup AutoCRUD
// ...
$crud->article->delete(1);
echo 'Record delete';
?>
5 選擇紀錄
<?php
// ...
// setup AutoCRUD
// ...
// Get a list of articles
$list = $crud->article->select();
echo 'Number of records: ' . count($list) . '';
foreach ($list as $article) {
echo $article['title'] . '';
}
?>
<?php
// Specify WHERE and ORDER BY
$crud->article->where = "articleid != 6";
$crud->article->orderby = "title DESC";
// Get a list of articles
$list = $crud->article->select();
echo 'Number of records: ' . count($list) . '';
foreach ($list as $article) {
echo $article['title'] . '';
}
?>
翻頁處理
<?php
// Do paging
$crud->article->paging = true; # Enable paging
$crud->article->perpage = 2; # Set number of records to show per page
$crud->article->currentpage = (empty($_GET['page'])) ? 1 : $_GET['page']; # Set current page
// Get a list of articles
$list = $crud->article->select();
echo 'Number of records: ' . count($list) . '';
foreach ($list as $article) {
echo $article['title'] . '';
}
if ($crud->article->currentpage > 1) {
echo '($crud->article->currentpage-1) . '">Previous Page ';
}
if ($crud->article->currentpage < $crud->article->totalpages) {
echo ' ($crud->article->currentpage+1) . '">Next Page';
}
?>
<?php
// Get article #5
$article = $crud->article->get(5);
if ($article == false) {
echo 'Article doesn/'t exist';
}
echo '
';
print_r ($article);
echo '';
?>
6 處理多表連線
<?php
// Setup relationship
$crud->comment->addRelationship ($crud->article, 'article', 'articleid', 'many-to-one');
// Get articles and their comments
$crud->article->join ('comment');
$articles = $crud->article->select();
echo '
';
print_r ($articles);
echo '';
?>
<?php
// Setup relationship
$crud->comment->addRelationship ($crud->article, 'article', 'articleid', 'many-to-one');
// Get comments and their article
$crud->comment->join ('article');
$comments = $crud->comment->select();
echo '
';
print_r ($comments);
echo '';
?>
// Setup relationship
$crud->article2category->addRelationship ($crud->category, 'category', 'categoryid', 'many-to-many');
$crud->article2category->addRelationship ($crud->article, 'article', 'articleid', 'many-to-many');
// Get articles and the categories they belong to
$crud->article->join ('category');
$articles = $crud->article->select();
echo '
';
print_r ($articles);
echo '';
?>
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=779777
相關文章
- Spring 持久層整合Spring
- 河青的持久層框架hqbatis框架BAT
- Java資料持久層Java
- 在Java SE下測試CDI Bean和持久層 - relationJavaBean
- MyBatis--優秀的持久層框架MyBatis框架
- PHP 底層原始碼下載地址PHP原始碼
- PHP持久配置容器YaconfPHP
- Java 持久層框架之 MyBatisJava框架MyBatis
- RxCache 整合 Android 的持久層框架 greenDAO、RoomAndroid框架OOM
- Java持久層框架Mybatis入門Java框架MyBatis
- MicroStream + Helidon高效能Java持久層ROSJava
- 持久層Mybatis3底層原始碼分析,原理解析MyBatisS3原始碼
- 自己動手寫一個持久層框架框架
- 透明層滑動跟隨導航選單
- 滑鼠懸浮從中心放大和縮小的半透明遮罩層遮罩
- 帶有半透明遮罩層可關閉視窗遮罩
- 帶有半透明背景居中彈出層詳解
- 【深入 PHP】PHP7 陣列的底層實現PHP陣列
- Docker下redis的主從、持久化配置DockerRedis持久化
- Docker下Nacos持久化配置Docker持久化
- 使用Spring Data JPA實現持久化層的簡化開發Spring持久化
- 理解PHP底層原理(一)PHP
- 用一個專案把控制層、業務層、持久層說明白了,每一句話都講的很清楚
- 【深入 PHP】PHP7 底層執行機制PHP
- php底層原理之函式PHP函式
- PHP 陣列底層實現PHP陣列
- 詳解 PHP 陣列的底層實現:HashTablePHP陣列
- PHP下的非同步嘗試四:PHP版的PromisePHP非同步Promise
- PHP 底層原理之類和物件PHP物件
- php底層原理之變數(一)PHP變數
- php底層原理之變數(二)PHP變數
- WordPress模板層次09:sidebar.phpIDEPHP
- WordPress模板層次13:comments.phpPHP
- WordPress模板層次08:footer.phpPHP
- WordPress模板層次17:404.phpPHP
- WordPress模板層次07:header.phpHeaderPHP
- WordPress模板層次06:index.phpIndexPHP
- WordPress模板層次12:single.phpPHP
- PHP 基金會:2023 年影響力和透明度報告PHP