"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"
-
-
- /**
- * Enterprise Application Architecture Data source schema mode of activity record 2010-10-17 sz
- * @author phppan.p#gmail.com http://www.phppan.com
- * Members of the Brother Society (http://www.blog-brother.com/)
- * @package Architecture
- */
-
- /**
- * Order Class
- */
- class Order {
-
- /**
- * Order ID
- * @var
- * /
- Private $_order_id;
-
- /**
- * Customer ID
- * @var
- * /
- Private $_customer_id;
-
- /**
- * Order Amount
- * @var
- * /
- Private $_amount;
-
- Public function __construct (er _id, $customer _id, $amount) {
- $this ->_order_id = er _id ;
- $this ->_customer_id = $customer _id ;
- $this ->_amount = $amount ;
- }
-
- /**
- * Delete operations for instances
- * /
- Public function delete() {
- $sql = "DELETE from Order SET WHERE order_id =" . $this ->_order_id. "and customer_id =" . $this ->_customer_id;
- return db::query ($sql);
- }
-
- /**
- * Update operation of the instance
- * /
- Public function Update () {
- }
-
- /**
- * Insert Operation
- * /
- Public function Insert () {
- }
-
- Public static function load ($rs) {
- return New Order ($rs[' order_id ']? $rs [' order_id ']: NULL, $rs [' customer_id '], $rs [' amount ']? $rs [' amount ']: 0);
- }
-
- }
-
- class Customer {
-
- Private $_name;
- Private $_customer_id;
-
- Public function __construct ($customer _id, $name) {
- $this ->_customer_id = $customer _id ;
- $this ->_name = $name ;
- }
-
- /**
- * User Delete order operation This instance method contains the business logic
- * Implemented by invoking an order instance
- * Suppose this is the corresponding delete operation (it may actually be a false delete operation marked with a field)
- * /
- Public function deleteorder (er _id) {
- er = Order::load (array(' order_id ' = = er _id, ' customer_id ' = $this ->_customer_id));
- return er,delete();
- }
-
- /**
- * Update operation of the instance
- * /
- Public function Update () {
- }
-
- /**
- * The Ingress class itself has an insert operation
- * /
- Public function Insert () {
- }
-
- Public static function load ($rs) {
- / * Add cache here * /
- return New Customer ($rs[' customer_id ']? $rs [' customer_id ']: NULL, $rs [' name ']);
- }
-
- /**
- * Search by Customer ID
- * @param integer $id customer ID
- * @return Customer Object
- * /
- Public static function find ($id) {
- return customerfinder::find ($id);
- }
-
- }
-
- /**
- * Personnel Lookup class
- */
- class Customerfinder {
-
- Public static function find ($id) {
- $sql = "SELECT * from person WHERE customer_id =" . $id ;
- $rs = Db::query ($sql);
-
- return customer::load ($rs);
- }
- }
-
- class DB {
-
- /**
- * This is just a demonstration method of executing SQL
- * @param string $sql SQL that needs to be executed
- * /
- Public static function query ($sql) {
- Echo "Execute sql:", $sql, "
";
-
- if (strpos($sql, ' SELECT ')!== FALSE) { ///example, Returning query Results for a select query
- return Array(' customer_id ' = 1, ' name ' = = ' Martin ');
- }
- }
-
- }
-
- /**
- * Client Calls
- */
- class Client {
-
- /**
- * Main program.
- * /
- Public static function main () {
-
-
- Header ("content-type:text/html; Charset=utf-8 ");
-
- / * Load customer information with Customer ID 1 * /
- $customer = Customer::find (1);
-
- / * Assume that the user has an order ID of 9527*/
- $customer ->deleteorder (9527);
- }
-
- }
-
- Client::main ();
- ?>
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.