Simple Example of PHP layered architecture (2. Data Classification class)

Source: Internet
Author: User

The previous section briefly introduces how to implement a business logic class. This section describes how to generate a Data Pipeline class. In theory, the complete data category class should be like an Orm, which can map relational data tables into corresponding object operation methods. Here we will referArticleTo design a simple data pipeline class.

It is best not to include any business logic in the Data Pipeline class. Only the crud methods of data tables and transactions according to rules are supported. We only need to add, modify, and view data. We only need to implement these methods.CodeIt is relatively simple, not to mention.

View code

<? PHP
Require_once ('Connecttodb. php ');

Class Newsdata
{
Private $ Con ;
// Add a piece of data
Public Function Insertone ( $ News )
{
$ This -> Con = connectodb: getconnec ();
$ SQL = "Insert into news (title, content, pubtime, newsclassid)
Value ('{ $ News -> Gettitle ()}','{ $ News -> Getcontent ()}',
'{ $ News -> Getpubtime ()}','{ $ News -> Getnewsclassid ()}')";
$ Result = Mysql_query ( $ SQL , $ This -> Con );
If ( $ Result )
{
$ Sql0 = "Select last_insert_id () from News ";
$ Resultset = Mysql_query ( $ Sql0 , $ This -> Con );
$ ID = Mysql_fetch_row ( $ Resultset );
Return $ ID ;
} Else
{
Return 0;
}
}
// Modify a data record
Public Function Updateone ( $ News )
{
$ This -> Con = connectodb: getconnec ();
$ SQL = "Update News set Title = '{ $ News -> Gettitle ()} ', content = '{ $ News -> Getcontent ()}',
Pubtime = '{ $ News -> Getpubtime ()} ', newsclassid = '{ $ News -> Getnewsclassid ()} 'where id = '{ $ News -> GETID ()}'";
$ Result = Mysql_query ( $ SQL , $ This -> Con );
If ( $ Result )
{
Return $ News -> GETID ();
} Else
{
Return 0;
}
}
// Retrieve a blog data, which is mapped to an array of keys.
Public Function Getone ( $ ID )
{
$ This -> Con = connectodb: getconnec ();
$ SQL = "Select * from news where id = $ ID ";
$ Result = Mysql_query ( $ SQL ,$ This -> Con );
$ Row = Mysql_fetch_assoc ( $ Result );
$ Newsarray = Array ();
$ Newsarray ["ID"] = $ Row ["ID"];
$ Newsarray ["Title"] = $ Row ["Title"];
$ Newsarray ["Content"] =$ Row ["Content"];
$ Newsarray ["Pubtime"] = $ Row ["Pubtime"];
Mysql_free_result ( $ Result );

Return $ Newsarray ;
}

}
?>

Note that connecttodb. PHP is a single-instance database connection class, which can be found here

MySQL connections in PHP Singleton Mode

The connection method is as follows:

$ This-> Con = connectodb: getconnec ();

In theory, the data retrieved by the getone ($ id) method should be mapped to a blog object, but the Array Function in PHP is quite powerful. Here we map the data to the corresponding key-value array.

By now, the simplest Data Handler class has been completed.

For example, if we want to add a new blog to the UI of another three-tier architecture, after obtaining data, we only need to call the addone () method of the blog business logic class. To view the data, we only need to call getone () method. In the MVC framework, if the model is too large, it can also be layered in this way; when the API needs to be opened, you can add a service layer to the business logic layer to call methods of the business logic class to implement web services such as restful.

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.