What is the most fundamental role of ORM? And some of the better PHP ORM frameworks
Reply content:
What is the most fundamental role of ORM? And some of the better PHP ORM frameworks
Orm:object relation mapping, i.e. object-relational mapping, is simply a mapping of the object model and the relational model. Why do you have such a mapping? Very simple, because the current development language is basically oop, but the traditional database is relational. In order to be close to object-oriented development, we want to manipulate the database like an object.
For example: Get an article, the traditional way to first execute a SQL to retrieve data
select * from post where id = 1
Then output the title and content using
echo $post['title']; echo $post['content'];
The code above encounters an object-oriented obsessive-compulsive disorder, and they're going to be obsessed with death.
So they come up with this thing and get an article in an ORM that can do this:
$post = postTable::getInstance()->find(1);#会再内部执行select * from post where id = 1
Then output:
echo $post->getTitle();echo $post->getContent();
Mom doesn't have to worry about my obsessive-compulsive disorder anymore. ^_^
Advanced point applications, articles and classifications are one-to-many relationships, articles, and tags are many-to-many relationships
$cate = $post->getCategory(); //获取文章分类echo $cate->getName(); //获取分类名$tags = $post->getTags(); //获取一个文章的所有标签
Is it a SQL that gets all the data we need without writing? Using ORM to implement the application without writing SQL at all, these Orm have done for us.
In addition, ORM can isolate the underlying database layer, and we don't need to worry about whether we're using MySQL or other relational databases.
I know orm:doctrine and propel.
In addition to the ORM, there are ODM, object document mapping, objects mapping, using the document database such as MongoDB
The core is to support the development of pure object-oriented, so that it will have to face the problem, the relational database at the data source layer does not support the object-oriented complex relationship between objects.
How to persist the current state of the system to the data source layer will be tricky. You can handle it in the business logic layer, through the data access layer. ORM is a powerful solution for the data access layer. It allows you to isolate the degree of coupling between the business logic layer and the data source to the maximum.
Full Name Object relation mapping
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 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);