In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to achieve laravel orm". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In laravel, the full name of orm is "Object-Relational Mapping", which means "object-relational mapping". Its function is to make a mapping between relational database and business entity objects, so that when operating business objects, there is no need to deal with complex SQL statements, only object properties and methods need to be manipulated.
The operating environment of this tutorial: windows7 system, Laravel6 version, Dell G3 computer.
What is ORM?
ORM, full name Object-Relational Mapping (object-relational mapping), its function is to make a mapping between relational database and business entity objects, so that when we operate specific business objects, we no longer need to deal with complex SQL statements, we only need to simply manipulate the properties and methods of objects.
ORM implementation method
The two most common implementations are ActiveRecord and DataMapper (the former is used in laravel)
One-to-one correspondence between models and data tables in ActiveRecord (very popular)
In DataMapper, the model is completely separate from the data table.
The Eloquent ORM of Laravel is implemented by ActiveRecord, and each Eloquent model class corresponds to a table in the database. We add, delete, modify and query the database by calling the corresponding methods of the model class.
Understand the two magic functions _ _ call () and _ _ callStatic ()
Class Test {/ / execution of _ _ call () method public function _ call ($method, $parameters) {echo 222222222222; return (new Rest)-> $method (. $parameters);} / / execution of _ _ callStatic () method public static function _ callStatic ($method, $parameters) {echo 1111111111 when static call does not find this function Return (new static)-> $method (... $parameters);}} class Rest {public function foo ($name,$age) {echo 333; dump ($name,$age);}} / / called _ _ callStatic () first, called _ _ call (), then called foo (); Test::foo ('Zhang San', 17); / / only called _ _ call (), and then called foo () (new Test ())-> foo ('Li Si', 16); die
After understanding the difficulties of the first two magic functions in laravel Eloqument ORM, let's take a look at the source code in Model.
/ * Handle dynamic method calls into the model. * * @ param string $method * @ param array $parameters * @ return mixed * / public function _ _ call ($method, $parameters) {if (in_array ($method, ['increment',' decrement'])) {return $this- > $method (. $parameters);} return $this- > newQuery ()-> $method (. $parameters);} / * * Handle dynamic static method calls into the method. * * @ param string $method * @ param array $parameters * @ return mixed * / public static function _ _ callStatic ($method, $parameters) {return (new static)-> $method (. $parameters);}
New static returns the caller's instance, and new self () returns its own instance.
When using eloqument queries
$list = Politician::where ('party_id', 1)-> count ()
If the where method is not in Model, it will first execute the callStatic () function to get the App\ Models\ Politician instance, then execute call (), and look for methods such as where () count () in the returned instance of $this- > newQuery ().
Take a closer look at the instance returned in the newQuery () method. Understand the difficulties in the implementation of orm in laravel by these two magic functions.
Query constructor in laravel
$list = DB::table ('categoty')-> get ()
Eloquent ORM actually encapsulates the query construction, which makes it more convenient to operate.
This is the end of the content of "how to achieve laravel orm". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.