In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use the CI model, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The details are as follows:
It is not appropriate for the business logic in MVC to be placed in the controller or in the model, so the business logic is separated here, and an additional layer is used to deal with the business logic, and the model is only regarded as the data access layer, so the model will become lighter. Parameters are not passed through entity objects in CI, and the input and return of parameters are controlled by developers, which is more flexible. In many cases, it is passed in or returned as an array.
The use of the model is also relatively simple, here are only a few questions that come to mind before using it.
1. Now that we have a data access layer, we should avoid querying the database directly through SQL in the controller or some classes. All database access operations should be obtained through the model, and in most cases a table name corresponds to a model class.
2. The model should be very convenient to connect multiple databases. The configuration of multiple libraries is mentioned in the database configuration file, and different libraries can be easily connected according to different group_name. If there is a master-slave, you can also consider the use of master-slave switching.
3. Does the model still need to be distinguished by module? There is a common controller distribution practice in the controller, which may not be a good idea in the model, but it can be achieved by inheriting different common model classes that in turn inherit CI's MY_Model. In some business, it may be useful to inherit according to the module. In most cases, you can directly inherit MY_Model,MY_Model to initialize the connection to the database and some common methods.
4. The mode of operation provided by the database is relatively basic, which requires us to assemble according to our own needs, but in our daily operations, many operations are similar, such as obtaining information according to the primary key, obtaining information according to ID, obtaining information according to attributes, etc., these basic operations can be encapsulated once, which is more convenient to use. Because if you want to use AR to operate the database, you need to remember a lot of methods, such as we query according to the user name:
$query = $this- > db- > from ('user')-> where (array (' username' = > 'BobbyPeng'))-> get (); return $query- > row_array ()
If encapsulated, you only need to remember one method, such as:
Public function findByAttributes ($where = array ()) {$query = $this- > db- > from ($this- > tableName ())-> where ($where)-> get (); return $query- > row_array ();}
In this way, the method of adding a tableName to each model returns the table name, and then the method can be easily used through the model.
5. The above method belongs to a common method, which we will write in MY_Model, but there will be many similar methods. Can we separate this type of method from a file? Because this method will not be changed in most cases, and putting it in MY_Model indicates that it is open to its changes, which may affect these methods. If this class is called the ActiveRecord class, you can have MY_Model inherit the ActiveRecord class and the ActiveRecord class inherit CI_Model. See below for the reference code.
Most of the time, the methods provided by the class library are relatively detailed, so we can encapsulate them to reduce the difficulty of using them. The encapsulation of common methods in the model is still under consideration, the following is only a simple operation for a single table, complex operations have to be implemented in a specific model, and there are some common operations or non-AR operations that can be unified to see if there is a chance to consider this problem in the future.
Public AR wrapper classes, which can perform common operations, need to assign the db property to the database connection object, and set several methods in the model, such as primary key and table name
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.