PHP ORM Framework and Easy Code implementation

Source: Internet
Author: User
Tags php framework zend framework
PHP ORM Framework and simple code implementation

Object Relational Mapping (relational Mapping, or ORM) is a technique to solve the mismatch between object-oriented and relational databases. In a nutshell, ORM automatically persists objects in a program to a relational database by using metadata that describes the mapping between the object and the database. Essentially, it's converting data from one form to another.

ORM provides the generation of all SQL statements, and the code staff is far away from the database concept. Mapping from a conceptual requirement (such as a hql) to an SQL statement does not require any cost, even 1% of the performance penalty. Real performance loss is in the process of mapping, more specifically, during object instantiation.

At present, PHP open source more famous ORM has the following several:

1, Propel

Propel is an ORM mapping (Object Relational Mapping) framework for PHP5, which provides object persistence layer support based on Apache torque. It generates SQL and classes by using schema definition files in XML format and corresponding configuration files, which allows you to use objects instead of SQL to read and write records in database tables. Propel provides a generator to create SQL definition files and PHP classes for your data model. Developers can also easily customize the generated classes, and we can also integrate propel into existing application development frameworks through XML, PHP classes, and phing build tools. For example, PHP framework Symfony version 1.2 is the default use of the lite version of the Propel as the default ORM framework.

Official website: http://www.propelorm.org/

2, Doctrine

Doctrine is a PHP ORM framework that must run on the >=php5.2.3 version, which is a powerful data abstraction layer.

One of its main features is to use object-oriented approach to implement the database query, the bottom layer through a similar Hibernate hql DQL query Statement database query, which makes the development of more flexibility, greatly reducing the duplication of code. Compared to Propel,doctrine's advantage is that it supports full-text retrieval, doctrine documents have been more comprehensive than propel, the community is more active, and more natural, easier to read, and closer to native SQL. The performance aspect is also slightly better than Propel. You can also easily integrate doctrine into your existing application framework, such as the PHP Framework Symfony 1.3 version will doctrine as the default ORM framework, but also can integrate doctrine and codeigniter.

Official website: http://www.doctrine-project.org/

3, Ezpdo

Ezpdo is a very lightweight PHP ORM framework. Ezpdo's author's intention is to reduce the complexity of the ORM learning curve, as much as possible to make a balance between the efficiency and function of the ORM, it is the simplest ORM framework I have used so far, and I want to integrate it into my coolphp SDK, and it is very efficient to run, The function also basically can satisfy the demand, but the ESPDO update is relatively slow.

Official website: http://www.ezpdo.net/blog/?p=2

4, Redbean

Redbean is an easy-to-use, lightweight PHP ORM framework that provides support for MySQL, Sqlite&postgresql. Redbean architecture is very flexible, the core is very simple, developers can easily through the plug-in to extend functionality.

Official website: http://www.redbeanphp.com/

5. Other

The domestic fleaphp development framework implements ORM based on Tabledatagateway, and the Zend framework, in addition to providing encapsulation of SQL statements, also implements Tablegateway, Tablerowset, TableRow implementation, and there are some activerecord implementations of rails-like solutions.

Generally speaking, the general ORM framework to deal with simple application system can meet the basic needs, can greatly reduce the development difficulty, improve development efficiency, but it is in the SQL optimization, it is certainly less than the pure SQL language, the complex association, SQL embedded Expression processing may not be ideal. Perhaps this is mainly due to the problem of the persistence of PHP itself, resulting in ORM inefficient, generally slower than pure SQL. But these are solutions, the most basic solution to the performance of the scheme, we can improve efficiency through the cache, hibernate, although the configuration is more complex, but it through the flexible use of level two cache and query cache greatly alleviate the database query pressure, greatly improve the performance of the system.

If you want to implement a PHP orm yourself, the following can be referred to below:

 _db = mysql_connect (' 127.0.0.1 ', ' root ', ');       $this->_tablename = $this->gettablename ();       $this->_arrelationmap = $this->getrelationmap ();   if (Isset ($id)) $this->_id = $id;   } Abstract protected function gettablename ();   Abstract protected function getrelationmap ();           Public Function Load () {if (Isset ($this->_id)) {$sql = "select";           foreach ($this->_arrelationmap as $k = + $v) {$sql. = '. $k. ', ';           } $sql. = substr ($sql, 0,strlen ($sql)-1); $sql. = "from". $this->_tablename. "           WHERE ". $this->pk." = ". $this->_id;           $result = $this->_db->mysql_query ($sql);              foreach ($result [0] as $k 1 + $v 1) {$member = $this->_arrelationmap[$key]; if (Property_exists ($this, $member)) {if (Is_numeric ($member)) {eval (' $this '). $member                 . ' = '. $value. '; '); }else{eval (' $thiS-> '. $member. ' = '. $value. '; ');   }}}} $this->is_load = true;      The Public Function __call ($method, $param) {$type = substr ($method, 0, 3);      $member = substr ($method, 3);             Switch ($type) {case ' get ': Return $this->getmember ($member);         Break      Case ' Set ': Return $this->setmember ($member, $param [0]);   } return false; Public Function Setmember ($key) {if (Property_exists ($this, $key)) {if (Is_numeric ($val)) {Eva          L (' $this '. $key. ' = '. $val. '; ');          }else{eval (' $this '. $key. ' = '. $val. '; ');       } $this->_modifymap[$key] = 1;       }else{return false;       }} Public Function GetMember ($key, $val) {if (! $this->is_load) {$this->load ();          if (Property_exists ($this, $key)) {eval (' $res = $this '. $key. '; '); return $this-> $key;   } return false; Public Function Save () {if (Isset ($this->_id)) {$sql = "UPDATE". $this->_tablename. "          SET ";                  foreach ($this->arrelationmap as $k 2 = $v 2) {if (Array_key_exists ($k 2, $this->_modifymap)) {                  Eval (' $val = $this '. $v 2. '; ');              $sql _update. = $v 2. "=". $val;          }} $sql. = substr ($sql _update,0,strlen ($sql _update));      $sql. = ' WHERE '. $this->pk. ' = '. $this->_id; }else{$sql = "INSERT into". $this->_tablename. "          (";                  foreach ($this->arrelationmap as $k 3 = $v 3) {if (Array_key_exists ($k 3, $this->_modifymap)) {                  Eval (' $val = $this '. $v 3. '; '); $field. = "'". $v 3. "                   `,";              $values. = $val;          }} $fields = substr ($field, 0,strlen ($field)-1);          $vals = substr ($values, 0,strlen ($values)-1); $sql. = $fiELDs. ")      VALUES (". $vals.") ";}      Echo $sql;   $this->_db->query ($sql); Public Function __destory () {if (Isset ($this->id)) {$sql = ' DELETE from '. $this->_tablename. "        WHERE ". $this->pk." = ". $this->_id;      $this->_db_query ($sql);    }}}class User extends model{protected function gettablename () {return ' test_user ';                      } protected function Getrelationmap () {return array (' id ' = = user_id,    ' user_name ' = user_name, ' user_age ' and ' user_age ';    } public Function Getdb () {return $this->_db; }} $UserIns = new User ();p rint_r ($UserIns); >

?

  • 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.