《PHP设计模式介绍》第十五章 表数据网关模式
使用动态记录模式对数据库表进行建立,获取,更新(通过扩展实现删除)每一行的操作。动态记录模式是一种简单的抽象数据库连接的方式,但是这种简洁性也正是它的弱点。动态记录类只处理单一的行,使得它在需要呈现大量信息的WEB应用中显得效率很低,如旅游预
使用动态记录模式对数据库表进行建立,获取,更新(通过扩展实现删除)每一行的操作。动态记录模式是一种简单的抽象数据库连接的方式,但是这种简洁性也正是它的弱点。动态记录类只处理单一的行,使得它在需要呈现大量信息的WEB应用中显得效率很低,如旅游预约,在线购物等。在这一类应用――几乎是主流的WEB应用中,数据集的使用就是更普遍更流行的。
问题
怎样才能简单的操作数据库表与表中的所有记录?
解决方案
表数据网关模式集成了动态记录模式。实际上,这个新模式的大多数代码都借签于14章动态记录模式的代码(它重用了一样的DB类与BOOKMARK TABEL DDL常量,并且也用ADOdb作为操纵数据的代码库)。然而,表数据网关模式集中于整表――记录集而不是单个的记录。
样本代码
让我们从建立操作开始,该操作完成向表中增加新记录。测试用例函数TableDataGatewayTestCase::testAdd() 完成向书签数据表中增加两条URL数据记录的步骤要求。它很大程度上参照了14章ActiveRecordTestCase::testAdd()方法,但它也其显著不同的地方,在于引入了一个新的BookmarkGateway这个表数据网关类。
class TableDataGatewayTestCase extends UnitTestCase {
function testAdd() {
$gateway = new BookmarkGateway($conn = DB::conn());
$gateway->add(
‘http://simpletest.org/’,
‘SimpleTest’,
‘The SimpleTest homepage’,
‘testing’);
$gateway->add(
‘http://blog.casey-sweat.us/’,
‘My Blog’,
‘Where I write about stuff’,
‘php’);
$rs = $this->conn->execute(‘select * from bookmark’);
$this->assertEqual(2,$rs->recordCount());
$this->assertEqual(2,$conn->Insert_ID());
}
}类似于动态记录,表数据网关测试用例示例了一个模板类,并增加一些记录到数据库。然而表数据网关模的工作对象是整张表,你只需建立一个该模式对象,并重用该对象对就能向数据表中增加更多的新记录。
这儿是BookmarkGateway一个可行的实现。
class BookmarkGateway {
protected $conn;
public function __construct($conn) {
$this->conn = $conn;
}
const INSERT_SQL = “
insert into bookmark (url, name, description, tag, created, updated)
values (?, ?, ?, ?, now(), now())
“;
public function add($url, $name, $description, $group) {
$rs = $this->conn->execute(
self::INSERT_SQL
,array($url, $name, $description, $group));
if (!$rs) {
trigger_error(‘DB Error: ‘.$this->conn->errorMsg());
}
}
}以上代码看上去很熟悉,动态记录模式与表数据网关模式的基本框架是相仿的:INSERT SQL 语句,函数参数表,对数据库错误的处理等都与动态记录模式的add()方法一次处理一条记录相类似。
建立了实现CRUD操作的代码后,现在来讨论如何获取数据。
测试用例结构
因为表数据网关的目的是处理具有多条记录的数据库表,你很有可能需要一个方便有效的方法来初始化表,使得在运行每一个实验时数据表都处于一个已知的状态。快速的解决方案是为每个实验建立一个基类,包括两个有用的方法:setup()与addSeveralBookmark,用来为每个实验重建已打乱的表和载入一些数据。
如下就是名为BaseTestCase的类
class BaseTestCase extends UnitTestCase {
protected $conn;
function __construct($name=’’) {
$this->UnitTestCase($name);
$this->conn = DB::conn();
}
function setup() {
$this->conn->execute(‘drop table bookmark’);
$this->conn->execute(BOOKMARK_TABLE_DDL);
}
function addSeveralBookmarks($gateway) {
// add(url, name, desc, tag)
$gateway->add(‘http://blog.casey-sweat.us/’
,’Jason\’s Blog’
,’PHP related thoughts’
,’php’);
$gateway->add(‘http://www.php.net/’
,’PHP homepage’
,’The main page for PHP’
,’php’);
$gateway->add(‘http://slashdot.org/’
,’/.’
,’News for Nerds’
,’new’);
$gateway->add(‘http://google.com/’
,’Google’
,’Google Search Engine’
,’web’);
$gateway->add(‘http://www.phparch.com/’
,’php|architect’
,’The home page of php|architect,
an outstanding monthly PHP publication’
,’php’);
}
}现在,每一个测试用例都源自BaseTestCase并继承它的构造器,一个setup()方法与一个addSeveralBookmarks()方法来预装一些数据。
http://www.chinaz.com/program/2008/0614/31068.shtml

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

DDREASE is a tool for recovering data from file or block devices such as hard drives, SSDs, RAM disks, CDs, DVDs and USB storage devices. It copies data from one block device to another, leaving corrupted data blocks behind and moving only good data blocks. ddreasue is a powerful recovery tool that is fully automated as it does not require any interference during recovery operations. Additionally, thanks to the ddasue map file, it can be stopped and resumed at any time. Other key features of DDREASE are as follows: It does not overwrite recovered data but fills the gaps in case of iterative recovery. However, it can be truncated if the tool is instructed to do so explicitly. Recover data from multiple files or blocks to a single

0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

When creating a virtual machine, you will be asked to select a disk type, you can select fixed disk or dynamic disk. What if you choose fixed disks and later realize you need dynamic disks, or vice versa? Good! You can convert one to the other. In this post, we will see how to convert VirtualBox fixed disk to dynamic disk and vice versa. A dynamic disk is a virtual hard disk that initially has a small size and grows in size as you store data in the virtual machine. Dynamic disks are very efficient at saving storage space because they only take up as much host storage space as needed. However, as disk capacity expands, your computer's performance may be slightly affected. Fixed disks and dynamic disks are commonly used in virtual machines

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker
