Chapter 5 scaffolding ?)
Scaffolding is a great way to make some Web applications developed earlier run. Early database models were unstable and easy to change. Scaffolding has a downward trend: WebProgramMembers hate forms that may not be used at all after creation. To reduce the repetitive work of programmers, cake contains scaffolding. Scaffolding analyzes the database, creates some standard lists using the add, delete, and edit buttons, creates input forms, and views the standard views of an item in the database. To add scaffolding to the Controller in the program, you need to add the $ scaffold variable:
- class categoriescontroller extends appcontroller
- {
- var $ scaffold ;
- }
for scaffold, pay attention to an important problem: scaffold expects that each filed name ending with _ id is a foreign key and points to a table, the table name is the same as that in front of _ ID (only in lower case ). Therefore, for example, if you nest a category, you 'd better have a column named parent_id. In this version, it is best to name it parentid. similarly, there is a foreign key in the table (for example, titles table has a category_id), and you have joined the appropriate models (see 6.2 understanding join ), in Views of show/edit/newd, the selected table is automatically displayed together with the foreign key table (Category) (original: A select box will be automatically populated with the rows from the foreign table (category) in the show/edit/new views .). Set $ displayfield in foreign model to determine which fields in foreign will be displayed. In our example, category has a title.
- Class Title Extends Appmodel
- {
- VaR $ Displayfield='Title';
- }
Chapter 6 models
Content of this chapter:
1. model function
1.1 User-Defined Functions
1.2 retrieving your data
1.3 save your data
1.4 callbacks)
2. Model Variables
3. Associations
What is model? It is m in MVC mode.
What does model do. It separates domain logic from presentation, independent application logic (it separates domain logic from the presentation, isolating application logic .)
A model is an access point pointing to the database. More specifically, it is a specific table in the database. By default, each model uses a table in the form of a complex name. For example, the user mode uses users.
Table. Models can maintain data-specific rules, link information, and the table method it uses.
1. Model Method
From the perspective of PHP, models are inherited from the appmodel class. The original appmodel class is defined under the/cake directory. You can also create your own app/app_model.php. This file should contain some methods that can be shared by multiple models. The appmodel itself is inherited from the model class. The model class is a standard cake library defined in libs/model. php.
Note:
Although this section describes the common methods in the model, remember: For more detailed reference, please go to the http://api.cakephp.org
1.1 user-defined methods
The following is an example of a specific table in the model. The example shows two methods: Display and hide in the blog.
-
- Example 6.1 Example model Functions
-
- <? PHP
-
- Class Post Extends Appmodel
-
- {
-
- Function Hide ($ ID=Null)
- {
-
- If ($ ID) $ This->Setid($ ID);
- $ This->Set('Hidden','1');
-
- $ This->Save();
-
- }
-
-
- Function Unhide ($ ID=Null)
-
- {
- If ( $ id ) $ this -> setid ( $ id ) ;
- $ This->Set('Hidden','0');
-
- $ This->Save();
-
- }
-
- }
- ?>
1.2 Data Retrieval
The following are some standard methods for getting data using the model:
- ●Findall($ Conditions,$ Fields,$ Order,$ Limit,$ Page,$ Recursive)
○ Return a specific fields. Fields is composed of $ limit (50 by default) records. It matches $ conditions (if any) and starts from page $ page (1 by default, $ conditions content should be the same as that in SQL statements, for example: $ conditions = "race = 'wookie' and thermal_detonators> 3"
○ When the $ recursive option is set to an integer from 1 to 3, findall () will attempt to return the models of all items found in the joined model. this recursive search can go deep into three layers.
- ●Find($ Conditions,$ Fields,$ Order,$ Recursive)
○ Return the specified fields that matches the first record of $ conditions (if not specified, return all)
○ $ Recursive serves the same purpose as above
- ●Findallby<Fieldname>($ Value) And Findby<Fieldname>($ Value)
○ These wonderful methods can be used to specify a specific field and a specific value to quickly search for a row. All you need to do is add your field to the backend by using the hump expression. For example)
- $ This->Post->Findbytitle('My first blog post');
-
- $ This->Author->Findbylastname('Rogers');
- $ This->Property->Findallbystate('AZ');
-
- $ This->Specimen->Findallbykingdom('Animalia');
The returned result is an array, and the result returned by find () and findall () is a form.
- ●Field($ Name,$ Conditions,$ Order)
○ Sort by $ order and return the field value in the first record as a string based on the condition $ conditions.
- ●Findcount($ Conditions)
○ Returns the number of records matching $ conditions.
- ●Generatelist
-
-
-
- ($ Conditions=Null,$ Order=Null,$ Limit=Null,$ Keypath=Null,$ Valuepath=Null)
○ obtain a series of key values based on the lists of models, especially the values created based on the model list.