In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the specific usage of ThinkPHP's data method". In daily operation, I believe many people have doubts about the specific usage of ThinkPHP's data method. Xiaobian consulted various materials and sorted out simple and easy to use operation methods. I hope to help you answer the doubts of "the specific usage of ThinkPHP's data method"! Next, please follow the small series to learn together!
ThinkPHP CURD method data method is also one of the coherent operation methods of the model class, used to set the value of the current data object to be operated, many developers are not used to using this method, today to explain how to use the data method.
The specific usage is as follows:
1. write operation
Usually we generate data objects through the create method or assignment method, and then write them to the database, for example:
$Model = D ('User ');$Model->create(); //skip the specific automatic generation and validation judgment $Model->add();
Or assign values directly to data objects, for example:
$Model = M('User');$Model->name ='flow year';$Model->email ='thinkphp@qq.com';$Model->add();
Then the data method directly generates the data object to be operated on, for example:
$Model = M('User');$data <$'name'] = 'flow year';$data <$'email'] = 'thinkphp@qq.com';$Model->data($data)->add();
Note: If we create a data object using both the create method and data, the method called later works.
The data method supports arrays, objects, and strings. Objects are as follows:
$Model = M('User');$obj = new stdClass;$obj->name ='stream year';$obj->email ='thinkphp@qq.com';$Model->data($obj)->add();
String mode usage is as follows:
$Model = M('User');$data = 'name= stream year &email=thinkphp@qq.com';$Model->data($data)->add();
You can also add data directly by passing in a data object in the add method, for example:
$Model = M('User');$data <$'name'] = 'flow year';$data <$'email'] = 'thinkphp@qq.com';$Model->add($data);
But this way the data parameter can only be used in arrays.
Of course, the data method can also be used to update data, for example:
$Model = M('User');$data <$'id'] = 8;$data <$'name '] = ' flow year';$data <$'email '] = 'thinkphp@qq.com';$Model->data($data)->save();
Of course, we can also use this directly:
$Model = M('User');$data <$'id'] = 8;$data <$'name '] = ' current year';$data <$'email '] = 'thinkphp@qq.com';$Model->save($data);
Similarly, the data argument can only be passed into arrays.
When the save method is called to update data, it will automatically determine whether there is a primary key value in the current data object, and if there is, it will automatically be used as an update condition. That is, the following usage is equivalent to the above:
$Model = M('User');$data <$'name']='flow year';$data <$'email ']='thinkphp@qq.com';$Model->data($data)->where('id=8')->save();
2. read operation
In addition to write operations, the data method can also be used to read the current data object, for example:
$User = M ('User ');$map <$'name']='Year';$User->where($map)->find(); //Read the current data object $data = $User->data(); At this point, the study of "the specific usage of ThinkPHP's data method" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.