CakePHP manual Chinese translation 3

Source: Internet
Author: User

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:

    1. class categoriescontroller extends appcontroller
    2. {
    3. var $ scaffold ;
    4. }

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.

    1. Class Title Extends Appmodel 
    2. {
    3. VaR $ Displayfield='Title';
    4. }

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.

  1. Example 6.1 Example model Functions
  2. <? PHP
  3. Class Post Extends Appmodel
  4. {
  5. Function Hide ($ ID=Null)
  6. {
  7. If ($ ID) $ This->Setid($ ID);
  8. $ This->Set('Hidden','1');
  9. $ This->Save();
  10. }
  11.  
  12. Function Unhide ($ ID=Null)
  13. {
  14. If ( $ id ) $ this -> setid ( $ id ) ;
  15. $ This->Set('Hidden','0');
  16. $ This->Save();
  17. }
  18. }
  19. ?>

1.2 Data Retrieval

The following are some standard methods for getting data using the model:

    1. ●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.

    1. ●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

    1. ●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)

  1. $ This->Post->Findbytitle('My first blog post');
  2. $ This->Author->Findbylastname('Rogers');
  3. $ This->Property->Findallbystate('AZ');
  4. $ This->Specimen->Findallbykingdom('Animalia');

The returned result is an array, and the result returned by find () and findall () is a form.

    1. ●Field($ Name,$ Conditions,$ Order)

○ Sort by $ order and return the field value in the first record as a string based on the condition $ conditions.

    1. ●Findcount($ Conditions)

○ Returns the number of records matching $ conditions.

  1. ●Generatelist
  2.  
  3. ($ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.