Home >Backend Development >PHP Tutorial >Activity Recording of Data Source Architecture Pattern_PHP Tutorial

Activity Recording of Data Source Architecture Pattern_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:42:031056browse

【Intent of activity recording】

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

【Applicable scenarios for activity recording】

Suitable for less complex domain logic, such as CRUD operations, etc.

【Operation mechanism of activity records】

Objects have both data and behavior. It uses the most direct method, placing data access logic in domain objects.

The essence of active record is a domain model. The classes in this domain model and the record structure in the base database should completely match. Each domain of the class corresponds to each column of the table.

Generally speaking, activity recording includes the following methods:

1. Construct an active record instance from data rows;

2. Construct a new instance for future insertion into the table;

3. Use static search methods to wrap commonly used SQL queries and return activity records;

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

5. Get or set the domain;

6. Implement some business logic.

【Advantages and Disadvantages of Activity Recording】

Advantages:

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

 2. Reduce code duplication when using transaction scripts.

3. You can change the database structure without changing the domain logic.

4. Derivation and test verification based on a single activity record will be very effective.

Disadvantages:

1. There is no hidden relational database.

2. The activity record will be effective only when the activity record object directly corresponds to the table in the database.

3. The design of the object and the design of the database are required to be tightly coupled, which makes further reconstruction in the project difficult

【Activity Recording and Other Modes】

Row data entry of data source architecture mode: Activity record is very similar to row data entry. The main difference between the two is that the row data entry only has database access while the activity record has both data source logic and domain logic.

【PHP example of activity recording】

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span>   </span></li><li class="alt"><span class="comment">/**</span> </li><li><span><span class="comment"> * Enterprise Application Architecture Data Source Architecture Pattern Activity Record 2010-10-17 sz </span> </span></li><li class="alt"><span><span class="comment"> * @author phppan.p#gmail.com http://www.phppan.com </span> </span></li><li><span><span class="comment"> * Member of Brother Society (http://www.blog -brother.com/) </span> </span></li><li class="alt"><span><span class="comment"> * @package architecture </span> </span></li><li><span><span class="comment">*/</span><span> </span></span></li><li class="alt"><span>   </span></li><li><span class="comment">/**</span> </li><li class="alt"><span><span class="comment"> * Order type </span> </span></li><li><span><span class="comment">*/</span><span> </span></span></li><li class="alt"><span class="keyword">class</span><span> Order {  </span></li><li><span>   </span></li><li class="alt"><span>    </span><span class="comment">/**</span> </li><li><span><span class="comment"> * Order ID </span> </span></li><li class="alt"><span><span class="comment"> * @var <type> </span> </span></li>
<li><span><span class="comment">*/</span><span> </span></span></li>
<li class="alt">
<span>    </span><span class="keyword">private</span><span> </span><span class="vars">$_order_id</span><span>;  </span>
</li>
<li><span>   </span></li>
<li class="alt">
<span>    </span><span class="comment">/**</span> </li>
<li><span><span class="comment"> * Customer ID </span> </span></li>
<li class="alt"><span><span class="comment"> * @var <type> </span> </span></li>
<li><span><span class="comment">*/</span><span> </span></span></li>
<li class="alt">
<span>    </span><span class="keyword">private</span><span> </span><span class="vars">$_customer_id</span><span>;  </span>
</li>
<li><span>   </span></li>
<li class="alt">
<span>    </span><span class="comment">/**</span> </li>
<li><span><span class="comment"> * Order amount </span> </span></li>
<li class="alt"><span><span class="comment"> * @var <type> </span> </span></li>
<li><span><span class="comment">*/</span><span> </span></span></li>
<li class="alt">
<span>    </span><span class="keyword">private</span><span> </span><span class="vars">$_amount</span><span>;  </span>
</li>
<li><span>   </span></li>
<li class="alt">
<span>    </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __construct(</span><span class="vars">$order_id</span><span>, </span><span class="vars">$customer_id</span><span>, </span><span class="vars">$amount</span><span>) {  </span>
</li>
<li>
<span>        </span><span class="vars">$this</span><span>->_order_id = </span><span class="vars">$order_id</span><span>;  </span>
</li>
<li class="alt">
<span>        </span><span class="vars">$this</span><span>->_customer_id = </span><span class="vars">$customer_id</span><span>;  </span>
</li>
<li>
<span>        </span><span class="vars">$this</span><span>->_amount = </span><span class="vars">$amount</span><span>;  </span>
</li>
<li class="alt"><span>    } </span></li>
<li><span>   </span></li>
<li class="alt">
<span>    </span><span class="comment">/**</span> </li>
<li><span><span class="comment"> * Instance deletion operation </span> </span></li>
<li class="alt"><span><span class="comment">*/</span><span> </span></span></li>
<li>
<span>    </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> </span><span class="func">delete</span><span>() {  </span>
</li>
<li class="alt">
<span>        </span><span class="vars">$sql</span><span> = </span><span class="string">"DELETE FROM Order SET WHERE order_id = "</span><span> . </span><span class="vars">$this</span><span>->_order_id . </span><span class="string">" AND customer_id = "</span><span>  . </span><span class="vars">$this</span><span>->_customer_id;  </span>
</li>
<li>
<span>        </span><span class="keyword">return</span><span> DB::query(</span><span class="vars">$sql</span><span>);  </span>
</li>
<li class="alt"><span>    }  </span></li>
<li><span>   </span></li>
<li class="alt">
<span>    </span><span class="comment">/**</span> </li>
<li><span><span class="comment"> * Instance update operation </span> </span></li>
<li class="alt"><span><span class="comment">*/</span><span> </span></span></li>
<li>
<span>    </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> update() {  </span>
</li>
<li class="alt"><span>    }  </span></li>
<li><span>   </span></li>
<li class="alt">
<span>    </span><span class="comment">/**</span> </li>
<li><span><span class="comment"> * Insert operation </span> </span></li>
<li class="alt"><span><span class="comment">*/</span><span> </span></span></li>
<li>
<span>    </span><span class="keyword">public</span><span> </span><span class="keywo						</p>
<p align=" left><div style="display:none;">
<span id="url" itemprop="url">http://www.bkjia.com/PHPjc/486087.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/486087.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">【活动记录的意图】 一个对象,它包装数据表或视图中某一行,封装数据库访问,并在这些数据上增加了领域逻辑。 【活动记录的适用场...</span>
</div>
<div class="art_confoot"></div></span>
</li>
</ol>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn