Home  >  Article  >  Backend Development  >  A brief description of the magic method in ThinkPHP query_PHP tutorial

A brief description of the magic method in ThinkPHP query_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:26:09852browse

When we develop using thinkphp, we sometimes use the getById('1') method to quickly obtain the content of a message. This method is better than using where(" id =1 ")->find() It is much easier to use, and the query efficiency is also faster than find.

When many people first came into contact with this method, they did not pay much attention to its internal implementation mechanism and just thought that information was obtained through ID, which is not the case.
For example, there are methods such as getByTitle("nihao") and getByMoney('1000') in some systems. These are very similar to the getById('1') mentioned just now. At the same time, the functional principles implemented are also the same as getById('1'). )Same.
In fact, similar to this getById($id), getByTitle($title), getByUserName($username), etc., the "getBy+field" method has a proper name called "magic method" , The fields behind it can be any field in the data table, and the returned result can only be the first piece of data queried . This method of querying is very efficient and easy to remember.
Sometimes our fields are not connected, for example: for fields like user_name, should we write it like this (getByUser_name($name))? The answer is no. Thinkphp does not support such a method. We need to use camel case to name . It should be written like this: getByUserName($name)!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824803.htmlTechArticleWhen we develop using thinkphp, we sometimes use the getById('1') method to quickly obtain The content of a message, this method is much easier to use than where(" id =1 ")-find(), and at the same time...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn