Set up a database and model _php tutorial using cakephp scaffolding Tools

Source: Internet
Author: User
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.

 
  
  1. CREATEDATABASE

2, create a "books" table by executing the following SQL statement:

 
  1. Use ' data-access ';
  2. CREATE TABLE ' books ' (
  3. ' id ' int (one) not NULL auto_increment PRIMARY KEY ,
  4. ' ISBN ' varchar (Ten) not NULL ,
  5. ' title ' varchar (127) not NULL ,
  6. ' description ' text not NULL ,
  7. ' Author_name ' varchar (127) not NULL
  8. )

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.

 
  1. var $ default = Array (
  2. ' Driver ' = ' MySQL ' ,
  3. ' Persistent ' = false ,
  4. ' Host ' = ' localhost ' ,
  5. ' Port ' = '' ,
  6. ' Login ' = ' username ' ,
  7. ' Password ' = ' Password ' ,
  8. ' database ' = ' data-access ' ,
  9. ' schema ' = '' ,
  10. ' prefix ' = '' ,
  11. ' encoding ' = ''
  12. );

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

  • 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.