The similarities and differences between Model, ORM, Dao, and Active Record

Source: Internet
Author: User
Tags ruby on rails
The similarities and differences between Model, ORM, Dao, and Active Record, for expert interpretation, can be based on the current PHP pop-up framework. Can be an example of better!








Reply content:



The similarities and differences between Model, ORM, Dao, and Active Record, for expert interpretation, can be based on the current PHP pop-up framework. Can be an example of better!






Unfortunately, these words describe not the same level of things, nothing comparable, not to mention similarities and differences.
In the bottom of the humble brief can not, simply help you find a bit of information, you reluctant to look at more.


    • Model
      I'm afraid you see this word in MVC, literally--the model, nothing special.
      Refer to: Http://zh.wikipedia.org/wiki/MVC

    • The data in the
    • ORM (Object Relational Mapping)
      relational database is translated into objects and objects so that the database can be accessed in an object-oriented way, saving brainpower and increasing development efficiency.
      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)
      is a layer of code on top of the data source (various databases), abstracting out a unified interface, Jiangzi the upper layer of code does not have to do different coding for different data sources.
      Refer to: Http://en.wikipedia.org/wiki/Data_access_object
      not a JB,:

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

    • Active Record
      an ORM implementation (Thanks @ Zichang). Can not say the secret, after all, is a puppy will be accustomed to the database of a row of records as a read-write entity, perhaps "a look at the clash I mean" is its secret itself it. The
      Martin Master's book explains that his blog address is also on the wiki page below.
      Refer to: Http://zh.wikipedia.org/wiki/Active_Record


For an example, summarize the use of these nouns:
Xiao Qiang to develop an application, choose to use MVC, first blabla ... Then start thinking aboutModellayers and then BlaBla ... Specific to the business object, the first choice of course is a relational database, but directly write SQL too much trouble, then useORM, consider how a mapping method, justActive Recorddo it, a table a class, a row of objects. Then BlaBla ... Test, fix, release. After a period of time, requirements change, change! Need to add a new data source (such as memcached), crouching slot, business object and data source coupled to live, change a ball, first refactoring, write a fewDAOplug in, business object data from theDAOacquisition, andDAOto 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, there are a number of concepts that are often encountered:



Model
Dao,data Access object, data Access objects
Orm,object-relational mapping, Object relational mapping
Active Record
These concepts are data-related, but what is the difference between them?



Let's start with model, models. A model is a concept in MVC that refers to data and the manipulation of data (business logic). A model usually refers to some kind of entity in real life. Take orders as an example, each order contains a lot of data, such as customers, prices, details, and so on, which are called the properties of the model order, in addition, and order related to some of the column operations, such as when the purchase, you may need to check inventory, give a certain discount, and then update the account balance and points, etc. , which is part of the model, is to be placed in the model in terms of code.



When the model executes the business logic, we have to save the data in the model to the database. If we put the database-related code directly in the model, it will make the maintenance of the future quite troublesome. In one of my previous projects, our users grew quite fast, causing a database to fail to support all of the accesses, and had to use a sub-library to solve the problem. However, the predecessor of the SQL statement written directly in the model layer, which leads to a very difficult sub-library, we can only take these SQL statements out, in order to put the sub-Library down. We put the extracted SQL code on a separate layer, which is the Dal,data access layer, the data access tier, which consists of a number of DAO, the purpose is to encapsulate the database-related code, so that when we execute the Sub-library, it is only used to adjust the DAO code, The model doesn't care whether the data it uses is placed in a or B libraries.



DAO is actually originated from the Java EE design pattern, the original purpose is to make the enterprise change the database, do not affect the model layer of code.



Like DAO, ORM is also a concept of encapsulating data access. However, ORM is not like DAO just a kind of guiding principle of software design, emphasizing that the system should be structured. ORM is more of a tool, with mature products such as hibernate in the Java World, and an ORM library that comes with many PHP frameworks. Their advantage is that the data objects in your program can be automatically converted into the corresponding tables and columns in the relational database, the reference between the data objects can also be converted into joins between tables, and hibernate even provides a set of their own data query language HQL to solve complex query problems.



The advantage of using ORM is that your development has virtually no access to SQL statements. Create a table, declare a corresponding class, and then you just use an instance of this class to interact with, as for the data in this object how to store and how to get, all do not care.



The Active record is an ORM pattern that fires with the popularity of Ruby on Rails, which integrates the code that is responsible for persistence into the data object, that is, the data object knows how to store itself in the database. This is different from the previous ORM, where the traditional ORM separates the data object from the code that is responsible for persistence, and the data object is simply a struct that contains data, passed in the model layer and the ORM layer. In the active record, the model layer integrates the ORM functionality, which represents both the entity, the business logic, and the data object, and is responsible for storing itself in the database, of course, the part of the stored code is already implemented in the model's parent class, which is part of the framework. The model simply calls the parent class's method to complete the persistence.
Source: Http://blog.pengqi.me/2011/03/30/difference-between-model-dao-orm-and-...






Every time I see this topic want to say one of my views, in web development has a common misconception is to equate model and ORM



The reason for this myth is that a lot of web development is actually a CRUD operation on the database, and business logic is almost equivalent to an ORM operation



In fact, model is a class that contains business logic, which is not necessarily associated with ORM, as long as you encapsulate a business logic into a single class, this class is actually model



So when I read the code, once I see this code,


phpclass Foobar extends Model {}


Just one thing, this person or framework author does not really understand the relationship between model and ORM, because business logic is changeable, so there should be no model base class at all.






Recommend a short and short ActiveRecord library, Lloydzhou/activerecord GitHub can achieve the effect of relation like 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 user
var_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);
Related Article

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.