Chapter 1 Introduction to PHP design patterns table data gateway patterns

Source: Internet
Author: User
The dynamic record mode is used to create, retrieve, and update each row of the database table (delete through extension. The dynamic record mode is a simple abstract database connection method, but its simplicity is also its weakness. The dynamic record class only processes a single row, making it inefficient in WEB applications that need to present a large amount of information, such as pre-tourism

The dynamic record mode is used to create, retrieve, and update each row of the database table (delete through extension. The dynamic record mode is a simple abstract database connection method, but its simplicity is also its weakness. The dynamic record class only processes a single row, making it inefficient in WEB applications that need to present a large amount of information, such as pre-tourism

The dynamic record mode is used to create, retrieve, and update each row of the database table (delete through extension. The dynamic record mode is a simple abstract database connection method, but its simplicity is also its weakness. The dynamic record class only processes a single row, making it inefficient in WEB applications that need to present a large amount of information, such as travel booking and online shopping. In this type of applications-almost mainstream WEB applications, the use of datasets is more common and popular.

Problem

How can we perform simple operations on all records in database tables and tables?

Solution

The table data gateway mode integrates the dynamic record mode. In fact, most of the Code in this new mode is based on the Code in chapter 14 dynamic record mode (it reuse the same DB class and bookmark tabel ddl constants, and also uses ADOdb as the code base for manipulating data ). However, the table data gateway mode is concentrated in the whole table-record set rather than a single record.

Sample Code

Let's add a new record to the table from the creation operation. Test Case function TableDataGatewayTestCase: testAdd () to add two URL data records to the bookmarks. It largely references the ActiveRecordTestCase: testAdd () method in chapter 14, but it is also significantly different in that it introduces a new BookmarkGateway table data gateway class.

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 ());
}
}

Similar to dynamic records, table data gateway test cases show a template class and add some records to the database. However, the working object of the table data gateway model is the whole table. You only need to create a schema object and use the object pair to add more new records to the data table.

Here is a feasible implementation of 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 ());
}
}
}

The above code looks familiar. the basic framework of the dynamic record mode is similar to that of the table data gateway mode: insert SQL statement, function parameter table, processing database errors is similar to processing a record at a time using the add () method in the dynamic record mode.

After the code for implementing the CRUD operation is established, we will discuss how to obtain data.

Test Case Structure

Because table data gateway is designed to process database tables with multiple records, you may need a convenient and effective method to initialize the table, make the data table in a known state when running each experiment. The quick solution is to create a base class for each experiment, including two useful methods: setup () and addSeveralBookmark, used to recreate the disrupted table and load some data for each experiment.

The following is the class named 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 | Effect'
, 'The home page of php | effecect,
An outstanding monthly PHP publication'
, 'Php ');
}
}

Now, each test case is sourced from BaseTestCase and inherits its constructor. A setup () method and an addSeveralBookmarks () method are used to pre-install some data.

Http://www.chinaz.com/program/2008/0614/31068.shtml

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.