Add, delete, modify, and query operations on CURDAction. class. php & lt ;? Php // test the add, delete, modify, and query operations on the database classCURDActionextendsAction {// index page publ...
Adding, deleting, modifying, and querying CURD,First, a set of code CURDAction. class. php is provided.
-
-
- // Test the addition, deletion, modification, and query operations on the database
-
- ClassCURDActionextendsAction
-
- {
-
- // Index page
-
- Publicfunctionindex ()
-
- {
-
- $ This-> assign ('title', 'Add data'); // The template variable {$ title} is set here}
-
- $ This-> assign ('datetime', date ("Y-m-dH: I: s "));
-
- $ This-> display ();
-
- }
-
- // INSERT operation
-
- Publicfunctioncreate ()
-
- {
-
- // Initialize the ing object (O/RMapping) corresponding to the think_demo table)
-
- $ Demo = D ("Demo ");
-
- If ($ demo-> create ()){
-
- // Assign values to the autotime field: obtain the date format accepted by the datetime field type in mysql database by date ("Y-m-dH: I: s.
-
- $ Demo-> autotime = date ("Y-m-dH: I: s ");
-
- // Insert operation
-
- $ Demo-> add ();
-
- // Jump to the display page
-
- // $ This-> display ("read ");
-
- $ This-> redirect ("read ");
-
- } Else {
-
- Header ("Content-Type: text/html; charset = utf-8 ");
-
- Exit ($ demo-> getError (). '[return aaaaaa]');
-
- }
-
- }
-
- // SELECT operation
-
- Publicfunctionread ()
-
- {
-
- $ Demo = D ("Demo ");
-
- $ Data = $ demo-> order ('iddesc')-> limit (10)-> select ();
-
- $ This-> assign ('data', $ data );
-
- $ This-> assign ('title', 'Add data 2 ');
-
- $ This-> display ();
-
- }
-
- // UPDATE operation
-
- Publicfunctionupdate ()
-
- {
-
- $ Demo = D ("Demo ");
-
- // In practice, it is found that the create () method may not be executed if the user does not start the onClick event, because the database is not updated if the user clicks the page.
-
- // Create () method: automatically extract data from html forms and inject it into Model objects.
-
- // $ Demo-> create ();
-
- // $ Demo-> save ();
-
- // Same as the above code
-
- $ Date ['title'] = $ _ POST ['title'];
-
- $ Date ['content'] = $ _ POST ['content'];
-
- $ Date ['id'] = $ _ POST ['id'];
-
- $ Demo-> save ($ date );
-
- $ This-> assign ('title', 'update data 2 ');
-
- $ This-> display ();
-
- }
-
- // DELECT operation
-
- Publicfunctiondelect ()
-
- {
-
- $ Demo = D ("Demo ");
-
- $ Demo-> where ('Id = 5')-> delete ();
-
- $ This-> redirect ("read ");
-
- }
-
- }
-
- ?>
After understanding the operation, you can understand the above code. because you have to get off work, I will go home and explain it again.
1. $ Demo = D ("Demo ");
Creates a ing object for a specified table through the database.
2. $ Demo-> create ()
Automatically loads the values in the form to the ing object using the same form name attribute name.
3. $ Demo-> add ();
Insert operations to insert data from non-empty ing objects into the database
4. $ Date ['title'] = $ _ POST ['title'];
$ Date ['content'] =_ _ POST ['content']; $ date ['id'] =$ _ POST ['id']; $ demo-> save ($ date); update operation. here, unconditional update is performed, and the system will automatically update 5 through the primary key. $ Demo-> where ('Id = 5')-> delete ();
Delete an operation by setting conditions.
6. $ Data = $ demo-> order ('iddesc')-> limit (10)-> select ();
$ This-> assign ('data', $ data );