The following code adds, deletes, and modifies form data in CakePHP. here we declare a point. in the previous example, we accidentally typed the lastupd field in the database table into lastudp. In this example, we will correct it.
Except for the appeal field, the database is consistent with the previous example.
The project still follows the example above, for example:
The code is as follows:
Database. php: same as the previous example.
Companies_controller.php:
-
- ClassCompaniesControllerExtendsAppController
- {
- Var $ Name='Companys';
-
- FunctionIndex ()
- {
- $ This-> Set ('Companys',$ This-> Company-> findAll ());
- }
-
- FunctionView ($ Id= Null)
- {
- $ This-> Company-> id =$ Id;
- $ This-> Set ('Company',$ This-> Company-> read ());
- }
-
- FunctionAdd ()
- {
- If(!EmptyEmpty($ This-> Data ))
- {
- If($ This-> Company-> save ($ This-> Data ))
- {
- $ This-> Flash ('Your post has been saved .','/Companies');
- }
- }
- }
-
- FunctionEdit ($ Id= Null)
- {
- If(EmptyEmpty($ This-> Data ))
- {
- $ This-> Company-> id =$ Id;
- $ This-> Data =$ This-> Company-> read ();
- }
- Else
- {
- If($ This-> Company-> save ($ This-> Data ['Company'])
- {
- $ This-> Flash ('Your post has been updated .','/Companies');
- }
- }
- }
-
- Function Delete($ Id)
- {
- $ This-> Company-> del ($ Id);
- $ This-> Flash ('The post with id :'.$ Id.'Has been deleted .','/Companies');
- }
- }
- ?>
Company. php:
-
- ClassCompanyExtendsAppModel
- {
- Var $ Name='Company';
-
- Var $ Validate=Array(
- 'Company'=> VALID_NOT_EMPTY,
- 'Price'=> VALID_NOT_EMPTY,
- 'Change'=> VALID_NOT_EMPTY,
- 'Lastup'=> VALID_NOT_EMPTY
- );
- }
- ?>
Index. thtml:
- Test companies
- Foreach($ Companies As $ Company):?>
- Endforeach;?>
Id |
Company |
Price |
Change |
Last update |
Echo $ Company['Company'] ['Id'];?> |
- Echo $ Html-> Link ($ Company['Company'] ['Company'],"/Companies/view /".$ Company['Company'] ['Id']);?>
-
- Echo $ Html-> Link ('Delete',"/Companies/delete/{$ company ['Company'] ['id']}", Null,'Are you sure? ')?>
|
Echo $ Company['Company'] ['Price'];?> |
Echo $ Company['Company'] ['Change'];?> |
Echo $ Company['Company'] ['Lastup'];?> |
- Echo $ Html-> Link ('Add',"/Companies/add");?>
View. thtml:
- Company: Echo $ Company['Company'] ['Company']?>
Id: Echo $ Company['Company'] ['Id']?>
Price: Echo $ Company['Company'] ['Price']?>
Change: Echo $ Company['Company'] ['Change']?>
LastUpdate: Echo $ Company['Company'] ['Lastup']?>
- Echo $ Html-> Link ('Edit',"/Companies/edit /".$ Company['Company'] ['Id']);?>
Add. thtml:
- Add Company
Edit. thtml:
- Edit Company
Access http: // localhost/cakephp/companies to test the code.