Home >Backend Development >PHP Tutorial >A brief description of the magic method in ThinkPHP query_PHP tutorial
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)!