Use the CakePHP scaffold tool to set databases and models. The CakePHP scaffold tool is mainly used to set databases and models. The following describes how to operate the CakePHP scaffold tool. We already know that model classes are usually used as CakePHP scaffolding tools to set databases and models. The following describes how to operate the CakePHP scaffold tool.
We already know that model classes are usually used to interact with databases. In CakePHP, a model class usually corresponds to Mo tables in the database. All database operations on the table are performed through the corresponding model class. You do not need to set the correspondence between CakePHP models and database tables. On the contrary, CakePHP uses some simple naming rules to achieve this effect. in this section, we will learn how to create an existing model class for tables in the database. CakePHP provides a tool named "scaffolding" to help us check previously created models and database tables. We will also learn how to use the scaffolding function to complete this task.
Create a model for a table in the database
Before learning how a model class interacts with a database table, we must first create a database table. In this section, we will first create a database table and then learn how to create a model class for this table. Then we will use the scaffolding function to quickly test the newly created model and data table.
Start time: create a database table and corresponding model
1. in the MySQL command prompt line, enter the following database command to create a new database named data-access.
- CREATE DATABASE `data-access`;
2. execute the following SQL statement to create a "books" table:
- USE `data-access`;
- CREATE TABLE `books` (
- `id` int( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
- `isbn` varchar( 10 ) NOT NULL ,
- `title` varchar( 127 ) NOT NULL ,
- `description` text NOT NULL ,
- `author_name` varchar( 127 ) NOT NULL
- )
-
3. place a new CakePHP folder under the root directory of your webpage. Rename the Cake folder to data-access.
4. go to the/app/config directory in the Cake installation folder. You can find a file named database. php. default. Rename this file to database. php. Open it in your favorite editor. Edit the $ default array in the file to configure your database. After editing, it looks like the following content
- var $default = array(
- 'driver' => 'mysql',
- 'persistent' => false,
- 'host' => 'localhost',
- 'port' => '',
- 'login' => 'username',
- 'password' => 'password',
- 'database' => 'data-access',
- 'schema' => '',
- 'prefix' => '',
- 'encoding' => ''
- );
5. Now, enter the following address in your browser:
Bytes. The following describes how to operate the CakePHP scaffold tool. We already know that model classes are usually used...