Activity records for data source schema mode

Source: Internet
Author: User
Keywords Activity records for data source schema mode
Tags php example vars

"Intent of the activity record"

An object that wraps a row in a data table or view, encapsulates the database access, and adds domain logic to the data.

"Applicable Scenarios for Activity records"

Applies to less complex domain logic, such as CRUD operations.

"Operating mechanism of activity records"

objects have both data and behavior. It uses the most straightforward method of placing data access logic in a domain object.

The essence of an activity record is a domain model in which the records structure in the class and base database in the domain model should match exactly, and each field of the class corresponds to each column of the table.

In general, the activity record includes the following methods:

1, the data row constructs an activity record instance;

2, for the future of the insertion of the table to construct a new instance;

3, using static search method to wrap the common SQL query and return activity records;

4. Update the database and insert the data from the activity record into the database;

5, get or set the domain;

6. Realize part of business logic.

"Advantages and disadvantages of activity logging"

Advantages:

1, simple, easy to create and easy to understand.

2. Reduce code replication when using transactional scripting.

3, you can change the database structure without changing the domain logic.

4. Derivation and test validation based on a single activity record can be effective.

Disadvantages:

1. There is no hidden relational database.

2. The activity record is valid only if the active Record object corresponds directly to the table in the database.

3, the requirements of the design of the object and the design of the database tightly coupled, which makes it difficult to further reconstruct the project

"Activity records and other modes"

Data source schema mode row data entry: The activity record is very similar to the row data entry. The main difference between the two is that the row data portal has only database access and the active record has both the data source logic and the domain logic.

"PHP Example of activity log"

 
 
  1. /**
  2. * Enterprise Application Architecture Data source schema mode of activity record 2010-10-17 sz
  3. * @author phppan.p#gmail.com http://www.phppan.com
  4. * Members of the Brother Society (http://www.blog-brother.com/)
  5. * @package Architecture
  6. */
  7. /**
  8. * Order Class
  9. */
  10. class Order {
  11.   /**
  12. * Order ID
  13. * @var
  14. * /
  15.   Private $_order_id;
  16.   /**
  17. * Customer ID
  18. * @var
  19. * /
  20.   Private $_customer_id;
  21.   /**
  22. * Order Amount
  23. * @var
  24. * /
  25.   Private $_amount;
  26.    Public function __construct (er _id, $customer _id, $amount) {
  27.     $this ->_order_id = er _id ;
  28.     $this ->_customer_id = $customer _id ;
  29.     $this ->_amount = $amount ;
  30. }
  31.   /**
  32. * Delete operations for instances
  33. * /
  34.    Public function delete() {
  35.     $sql = "DELETE from Order SET WHERE order_id =" . $this ->_order_id. "and customer_id =" . $this ->_customer_id;
  36.     return db::query ($sql);
  37. }
  38.   /**
  39. * Update operation of the instance
  40. * /
  41.    Public function Update () {
  42. }
  43.   /**
  44. * Insert Operation
  45. * /
  46.    Public function Insert () {
  47. }
  48.    Public static function load ($rs) {
  49.     return New Order ($rs[' order_id ']? $rs [' order_id ']: NULL, $rs [' customer_id '], $rs [' amount ']? $rs [' amount ']: 0);
  50. }
  51. }
  52. class Customer {
  53.   Private $_name;
  54.   Private $_customer_id;
  55.    Public function __construct ($customer _id, $name) {
  56.     $this ->_customer_id = $customer _id ;
  57.     $this ->_name = $name ;
  58. }
  59.   /**
  60. * User Delete order operation This instance method contains the business logic
  61. * Implemented by invoking an order instance
  62. * Suppose this is the corresponding delete operation (it may actually be a false delete operation marked with a field)
  63. * /
  64.    Public function deleteorder (er _id) {
  65.     er = Order::load (array(' order_id ' = = er _id, ' customer_id ' = $this ->_customer_id));
  66.     return er,delete();
  67. }
  68.   /**
  69. * Update operation of the instance
  70. * /
  71.    Public function Update () {
  72. }
  73.   /**
  74. * The Ingress class itself has an insert operation
  75. * /
  76.    Public function Insert () {
  77. }
  78.    Public static function load ($rs) {
  79.     / * Add cache here * /
  80.     return New Customer ($rs[' customer_id ']? $rs [' customer_id ']: NULL, $rs [' name ']);
  81. }
  82.   /**
  83. * Search by Customer ID
  84. * @param integer $id customer ID
  85. * @return Customer Object
  86. * /
  87.    Public static function find ($id) {
  88.     return customerfinder::find ($id);
  89. }
  90. }
  91. /**
  92. * Personnel Lookup class
  93. */
  94. class Customerfinder {
  95.    Public static function find ($id) {
  96.     $sql = "SELECT * from person WHERE customer_id =" . $id ;
  97.     $rs = Db::query ($sql);
  98.     return customer::load ($rs);
  99. }
  100. }
  101. class DB {
  102.   /**
  103. * This is just a demonstration method of executing SQL
  104. * @param string $sql SQL that needs to be executed
  105. * /
  106.    Public static function query ($sql) {
  107.     Echo "Execute sql:", $sql, "
    ";
  108.      if (strpos($sql, ' SELECT ')!== FALSE) { ///example, Returning query Results for a select query
  109.       return Array(' customer_id ' = 1, ' name ' = = ' Martin ');
  110. }
  111. }
  112. }
  113. /**
  114. * Client Calls
  115. */
  116. class Client {
  117.   /**
  118. * Main program.
  119. * /
  120.    Public static function main () {
  121. Header ("content-type:text/html; Charset=utf-8 ");
  122.     / * Load customer information with Customer ID 1 * /
  123.     $customer = Customer::find (1);
  124.     / * Assume that the user has an order ID of 9527*/
  125.     $customer ->deleteorder (9527);
  126. }
  127. }
  128. Client::main ();
  129. ?>

As in the previous article, this is just an example of an activity record, the application of the active record mode, you can view the DB class in the YII framework, there is a Cactiverecord abstract class in its source code, from which we can see the application of the active recording mode.

In addition, if you create an activity record from a transactional script, you typically first wrap the table as a portal, and then start the behavior migration, which deepens the table into the active record.

Access and settings for the fields in the active record can be like the YII framework, using the Magic method __set method and the __get method.

  • 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.