What is the most fundamental role of ORM? What is the most fundamental role of some good phpORM framework ORM? And some good php ORM frameworks
Reply content:
What is the most fundamental role of ORM? And some good php ORM frameworks
ORM: object relation mapping, that is, object relationship ing. In short, it is a ing between the object model and the Relationship Model. Why is there such a ing? It is very simple, because the current development languages are basically oop, but traditional databases are relational. In order to be close to object-oriented development, we want to operate databases just as we operate on objects.
For example, to obtain an article, you must first execute an SQL statement to retrieve data.
select * from post where id = 1
Then output the title and content
echo $post['title']; echo $post['content'];
The above code will be entangled in object-oriented obsessive-compulsive disorder.
So they came up with this thing. They can get an article in The ORM as follows:
$ Post = postTable: getInstance ()-> find (1); # select * from post where id = 1 is executed internally.
Then output:
echo $post->getTitle();echo $post->getContent();
Mom no longer needs to worry about my obsessive-compulsive disorder ^_^
Advanced applications, articles and classifications are one-to-many relationships, articles and tags are multiple-to-many relationships
$ Cate = $ post-> getCategory (); // obtain the article category echo $ cate-> getName (); // obtain the category name $ tags = $ post-> getTags (); // obtain all the tags of an article
Is it because all the data we need is obtained without writing an SQL statement? With ORM, You can implement the application without writing SQL at all. These ORM are done for us.
In addition, the orm can also isolate the underlying database layer. We do not need to worry about whether we use mysql or other relational databases.
I know orm: doctrine and propel
In addition to the orm, there are also odm, that is, object document mapping, object document ing, used in document databases such as mongodb
The core is to support object-oriented development, which will lead to a problem that must be faced. Relational databases at the data source layer do not support complex relationships between objects in the object-oriented model.
It is tricky to make the current state of the system persistent to the data source layer. You can use the data access layer to process data in the business logic layer. ORM is a powerful solution for the data access layer. It isolates the coupling between the business logic layer and the data source to the maximum extent.
Full name object relation mapping
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);