CakePHP Scaffolding tools are primarily used to set up databases and models. The operation principle and method of this cakephp scaffold tool are described below.
We already know that model classes are usually used to interact with the database. In CakePHP, a model class typically corresponds to a table in a database. All database operations on a table are implemented by the corresponding model classes. The correspondence between CakePHP's model and the database table does not need to be set. Instead, CakePHP uses some simple naming conventions to achieve this effect, and in this part we will learn how to create a model class for a table in the database. CakePHP provides a tool called scaffolding to help us examine previously created models and database tables. We will also learn how to use the scaffolding function to accomplish this task.
To create a model for a table in a database
Before we understand how model classes interact with database tables, we first create a database table. In the next section, we will first create a database table and then learn how to create a model class for the table. We will then use the scaffolding function to perform a quick test of the newly created model and data table.
Hands-On time: Create a database table and a corresponding model
1, in the MySQL command prompt line, we enter the following database command to create a new database named Data-access.
- CREATEDATABASE
2, create a "books" table by executing the following SQL statement:
- Use ' data-access ';
- CREATE TABLE ' books ' (
- ' id ' int (one) not NULL auto_increment PRIMARY KEY ,
- ' ISBN ' varchar (Ten) not NULL ,
- ' title ' varchar (127) not NULL ,
- ' description ' text not NULL ,
- ' Author_name ' varchar (127) not NULL
- )
3, place a new cakephp folder in your Web root directory. Rename the cake folder to Data-access.
4, enter the/app/config directory under the Cake installation folder. You have found a file named Database.php.default. Rename this file to database.php. Use your favorite editor to open it. Edit the $default array in the file to configure your database. After editing it, it should look similar to the following text.
- 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
http://www.bkjia.com/PHPjc/446579.html www.bkjia.com true http://www.bkjia.com/PHPjc/446579.html techarticle cakephp Scaffolding tools are primarily used to set up databases and models. The operation principle and method of this cakephp scaffold tool are described below. We already know that model classes are often used to ...