Home php教程 php手册 数据源架构模式之活动记录

数据源架构模式之活动记录

Jun 13, 2016 am 10:40 AM
Package it object data source data sheet Architecture model Activity of Record

  【活动记录的意图】

  一个对象,它包装数据表或视图中某一行,封装数据库访问,并在这些数据上增加了领域逻辑。

  【活动记录的适用场景】

  适用于不太复杂的领域逻辑,如CRUD操作等。

  【活动记录的运行机制】

  对象既有数据又有行为。其使用最直接的方法,将数据访问逻辑置于领域对象中。

  活动记录的本质是一个领域模型,这个领域模型中的类和基数据库中的记录结构应该完全匹配,类的每个域对应表的每一列。

  一般来说,活动记录包括如下一些方法:

  1、由数据行构造一个活动记录实例;

  2、为将来对表的插入构造一个新的实例;

  3、用静态查找方法来包装常用的SQL查询和返回活动记录;

  4、更新数据库并将活动记录中的数据插入数据库;

  5、获取或设置域;

  6、实现部分业务逻辑。

  【活动记录的优点和缺点】

  优点:

  1、简单,容易创建并且容易理解。

  2、在使用事务脚本时,减少代码复制。

  3、可以在改变数据库结构时不改变领域逻辑。

  4、基于单个活动记录的派生和测试验证会很有效。

  缺点:

  1、没有隐藏关系数据库的存在。

  2、仅当活动记录对象和数据库中表直接对应时,活动记录才会有效。

  3、要求对象的设计和数据库的设计紧耦合,这使得项目中的进一步重构很困难

  【活动记录与其它模式】

  数据源架构模式之行数据入口:活动记录与行数据入口十分类似。二者的主要差别是行数据入口 仅有数据库访问而活动记录既有数据源逻辑又有领域逻辑。

  【活动记录的PHP示例】

 

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li><span>   </span></li>
<li class="alt">
<span class="comment">/** </span> </li>
<li><span><span class="comment"> * 企业应用架构 数据源架构模式之活动记录 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"> * 哥学社成员(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"> * 定单类 </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">     *  定单ID </span> </span></li>
<li class="alt"><span><span class="comment">     * @var <type> </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">     * 客户ID </span> </span></li>
<li class="alt"><span><span class="comment">     * @var <type> </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">     * 定单金额 </span> </span></li>
<li class="alt"><span><span class="comment">     * @var <type> </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">     * 实例的删除操作 </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">     * 实例的更新操作 </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">     * 插入操作 </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>
"></span>
</li>
</ol>
Copy after login
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Mar 12, 2024 pm 07:20 PM

Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

A complete guide to the activities of "Glory of Kings" A complete guide to the activities of "Glory of Kings" Mar 24, 2024 pm 12:36 PM

King of Glory has launched the Let’s Go Together to Flower Season event. Players who participate in the event can receive free avatar frames and many gifts. The event has a time limit and provides players with a total of four levels. Today, the editor has brought you a guide to the Let’s Go to Flower Season event. Encyclopedia, I hope it can help everyone complete the level challenge. A guide to the King of Glory's &quot;Going to the Flowering Season&quot; event. King of Glory, &quot;Going to the Flowering Season&quot; activity introduction. How to play: 1. &quot;Going to the Flowering Season&quot; is a card-turning activity, and players need to turn over the cards to pass the level. 2. Players can turn over cards by completing tasks and obtaining flower dew during the event. 3. Every four clearance cards in the activity panel are connected into a line (including horizontal lines, vertical lines and diagonal lines) to pass a small level. 4. Every time you clear a level, you can get corresponding rewards, and you can also get additional rewards by helping your friends turn over cards. live

1.3ms takes 1.3ms! Tsinghua's latest open source mobile neural network architecture RepViT 1.3ms takes 1.3ms! Tsinghua's latest open source mobile neural network architecture RepViT Mar 11, 2024 pm 12:07 PM

Paper address: https://arxiv.org/abs/2307.09283 Code address: https://github.com/THU-MIG/RepViTRepViT performs well in the mobile ViT architecture and shows significant advantages. Next, we explore the contributions of this study. It is mentioned in the article that lightweight ViTs generally perform better than lightweight CNNs on visual tasks, mainly due to their multi-head self-attention module (MSHA) that allows the model to learn global representations. However, the architectural differences between lightweight ViTs and lightweight CNNs have not been fully studied. In this study, the authors integrated lightweight ViTs into the effective

Do Not Disturb Mode Not Working in iPhone: Fix Do Not Disturb Mode Not Working in iPhone: Fix Apr 24, 2024 pm 04:50 PM

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings

What is the architecture and working principle of Spring Data JPA? What is the architecture and working principle of Spring Data JPA? Apr 17, 2024 pm 02:48 PM

SpringDataJPA is based on the JPA architecture and interacts with the database through mapping, ORM and transaction management. Its repository provides CRUD operations, and derived queries simplify database access. Additionally, it uses lazy loading to only retrieve data when necessary, thus improving performance.

How steep is the learning curve of golang framework architecture? How steep is the learning curve of golang framework architecture? Jun 05, 2024 pm 06:59 PM

The learning curve of the Go framework architecture depends on familiarity with the Go language and back-end development and the complexity of the chosen framework: a good understanding of the basics of the Go language. It helps to have backend development experience. Frameworks that differ in complexity lead to differences in learning curves.

Return to Omaha Beach! World of Tanks launches Normandy commemoration event Return to Omaha Beach! World of Tanks launches Normandy commemoration event May 31, 2024 pm 10:25 PM

As the D-Day invasion approaches its 80th anniversary, a whole month of World of Tanks events and specials will be centered around Operation Overlord - a new PvE mode, a themed battle pass, the release of a new Frontline mode, and a month-long The Operation Normandy token store is about to open. OPERATION MAP From June 3 to June 30, explore the beaches of Normandy and collect up to 90 Operation Normandy Tokens: 36 from this map and another 54 by completing daily tasks. Check out the interactive map and see the start dates for each event, then start earning tokens now, or unlock special token missions. Use the map to learn more about Operation Normandy related activities. Once you have obtained enough Operation Normandy tokens, you can go to the Operation Normandy token dealer

Hand-tearing Llama3 layer 1: Implementing llama3 from scratch Hand-tearing Llama3 layer 1: Implementing llama3 from scratch Jun 01, 2024 pm 05:45 PM

1. Architecture of Llama3 In this series of articles, we implement llama3 from scratch. The overall architecture of Llama3: Picture the model parameters of Llama3: Let's take a look at the actual values ​​of these parameters in the Llama3 model. Picture [1] Context window (context-window) When instantiating the LlaMa class, the variable max_seq_len defines context-window. There are other parameters in the class, but this parameter is most directly related to the transformer model. The max_seq_len here is 8K. Picture [2] Vocabulary-size and AttentionL

See all articles