Similarities and differences between Model, Orm, Dao, and ActiveRecord

Source: Internet
Author: User
The similarities and differences between Model, Orm, Dao, and ActiveRecord can be explained based on the popular PHP framework. A better example! The similarities and differences between Model, Orm, Dao, and Active Record can be explained based on the popular PHP framework. A better example!

Reply content:

The similarities and differences between Model, Orm, Dao, and Active Record can be explained based on the popular PHP framework. A better example!

Unfortunately, these words are not at the same level, and there is no comparability, let alone similarities and differences.
I can't explain it in the lower part. I just helped you find some information, so you can easily look at it.

  • Model
    I'm afraid you saw this word in MVC, such as the literal meaning-model, which has no special meaning.
    Specific reference: http://zh.wikipedia.org/wiki/MVC

  • ORM (Object Relational Mapping)
    The data in a relational database is converted into objects and object references, so that you can access the database in an object-oriented way, saving your effort and increasing development efficiency.
    Specific can refer to: http://zh.wikipedia.org/wiki/%E5%AF%B9%E8%B1%A1%E5%85%B3%E7%B3%BB%E6%9...

  • DAO (Data Access Object)
    It is to lay a layer of code on the data source (various databases) and abstract a unified interface. The code on the upper layer of the sauce purple layer does not have to be encoded differently for different data sources.
    Specific reference: http://en.wikipedia.org/wiki/Data_access_object
    No picture says a JB ,:

    Technical documentation http://www.oracle.com/technetwork/java/dataaccessobject-138824.html from Java

  • Active Record
    An ORM implementation method (thanks @ Yang Yi ). I cannot tell the story below. After all, a cat and a dog will get used to treating a row of records in the database as a readable and writable entity. Maybe it's exactly what it means when you look at it.
    He wrote in his book that Martin described his blog address on the wiki page below.
    Specific reference: http://zh.wikipedia.org/wiki/Active_Record

The following example summarizes the usage of these terms:
Xiaoqiang wants to develop an application, and selects MVC, first blabla ...... Then begin to considerModelLayer, and then blabla ...... Specific to how business objects are persistent, the first choice is of course relational databases, but it is too time-consuming to directly write SQL statements.ORM, Consider how to map, just pressActive RecordLet's do that. One table, one class, one row, and one object. Then blabla ...... Tests, fixes, and releases. After a period of time, the demand will be changed! You need to add a new data source (such as Memcached). The slots, Business Objects, and data sources are coupled. Change the ball, refactor it, and write a fewDAOThe data of the Business Object isDAOAndDAOTo determine the data source.

......

Refer:
Http://stackoverflow.com/questions/6640784/difference-between-active-r...
And
Http://stackoverflow.com/questions/3198419/orm-dao-datamapper-activere...

In web development, we often encounter the following concepts:

Model
DAO, data access object, and data access object
ORM, object-relational mapping, object relationship ing
Active Record
These concepts are related to data, but what are the differences between them?

First, let's look at the Model. A model is a concept in MVC that refers to data and data change operations (business logic ). A model usually refers to an entity in real life. Taking orders as an example, each order contains a lot of data, such as customers, prices, details, etc. These data are called the attributes of the order model. In addition, some column operations related to orders, for example, when purchasing an instance, you may need to first check the inventory, give a certain discount, and then update the account balance and points. These are called business logic and are part of the model, in terms of code, it should be placed in the model.

After the model executes the business logic, we need to save the data in the model to the database. If we put the database-related code directly in the model, it will make future maintenance quite troublesome. In one of my previous projects, our user growth was quite fast, so a database could not support all the accesses and had to use sub-databases to solve the problem. However, the predecessors wrote SQL statements directly in the model layer, which made database sharding quite troublesome. We can only extract these SQL statements first to continue database sharding. We put the extracted SQL code on a separate Layer, which is DAL, Data Access Layer, and Data Access Layer, which consists of many DAO, the purpose is to encapsulate the database-related code so that when we execute database shards, we only need to adjust the DAO code, the model does not have to worry about whether the data it uses is stored in database A or database B.

DAO is actually a design model based on J2EE. It was originally designed to allow enterprises to replace databases without affecting the code at the model layer.

Similar to DAO, ORM is also a concept that encapsulates data access. However, unlike DAO, ORM is only a guiding principle for software design. It emphasizes that the system should be hierarchical. ORM is more like a tool with mature products, such as Hibernate, a well-known JAVA field, and the orm libraries that come with many PHP frameworks. Their advantage is that they can automatically convert the data objects in your program into corresponding tables and columns in a relational database. The reference between data objects can also be converted to join between tables through this tool, hibernate even provides its own data query language HQL to solve complex query problems.

The advantage of using ORM is that your development has almost no access to SQL statements. Create a table, declare a corresponding class, And then you only need to interact with the instance of this class. As for how to store and obtain the data in this object, you don't need to worry about communication.

Active Record is an ORM mode popular with ruby on rails. It integrates code for persistence into data objects, that is, this data object knows how to store itself in the database. This is different from the old ORM. The traditional ORM separates the data object from the code responsible for persistence. The data object is just a structure containing data, passed in the model layer and the orm layer. In Active Record, the model layer integrates the functions of ORM. They represent entities, including business logic and data objects, and are responsible for storing themselves in the database. Of course, this part of the stored Code has already been implemented in the model's parent class and is part of the framework. The model simply needs to call the method of the parent class to complete persistence.
Source: http://blog.pengqi.me/2011/03/30/difference-between-model-dao-orm-and -...

Every time I see this topic, I want to share my point of view. A common misunderstanding in web development is to equate model With orm.

The reason for this misunderstanding is that a large number of web development operations are actually CRUD operations on databases, and the business logic is almost equivalent to orm operations.

In fact, model is a class that contains the business logic. It is not necessarily related to orm. As Long As You encapsulate a piece of business logic into a separate class, this class is actually a model.

So when I read the code

phpclass Foobar extends Model {}

This person or framework author does not really understand the relationship between model and orm. Because the business logic is changeable, there should be no model base class at all.

We recommend that you use a short and concise ActiveRecord library, such as loydzhou/activerecord-GitHub, to implement relation similar to Yii. Document address: http://lloydzhou.github.io/activerecord/

class User extends ActiveRecord{  public $table = 'user';  public $primaryKey = 'id';  public $relations = array(    'contacts' => array(self::HAS_MANY, 'Contact', 'user_id')  );}class Contact extends ActiveRecord{}$user = new User();// find one uservar_dump($user->notnull('id')->orderby('id desc')->find());echo "\nContact of User # {$user->id}\n";// get contacts by using relation://   'contacts' => array(self::HAS_MANY, 'Contact', 'user_id'),var_dump($user->contacts);

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.