CakePHP scaffolding tool is mainly used to set up databases and models. The following introduces the operating principles and methods of this CakePHP scaffolding tool.
We already know that model classes are usually used to interact with the database. In CakePHP, a model class usually corresponds to one table in the database. All database operations on tables are implemented through the corresponding model classes. The correspondence between CakePHP's model and database table does not need to be set. Instead, CakePHP uses some simple naming rules to achieve this effect. In this part, we will learn how to create the model class for the table in the database. CakePHP provides a tool called "Scaffolding" to help us inspect previously created models and database tables. We'll also see how to do this using the "Scaffolding" feature.
Create a model for a table in the database
Before understanding how the model class interacts with the database table, we first need to create a database table. In this next section, we'll first create a database table and then see how to create a model class for this table. Then we will also use the scaffolding function to conduct a quick test on the newly created model and data table.
Hands-on time: Create a database table and corresponding model
1. In the MySQL command prompt, we enter the following database command to create a new database named data-access.
<ol class="dp-sql"><li class="alt"><span><span class="keyword"><strong><font color="#006699">CREATE</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">DATABASE</font></strong></span><span> `data-access`; </span></span></li></ol>
2. Create a "books" table by executing the following SQL statement:
<ol class="dp-sql"> <li class="alt"><span><span>USE `data-access`; </span></span></li> <li class=""> <span></span><span class="keyword"><strong><font color="#006699">CREATE</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">TABLE</font></strong></span><span> `books` ( </span> </li> <li class="alt"> <span>`id` </span><span class="keyword"><strong><font color="#006699">int</font></strong></span><span>( 11 ) </span><span class="op"><font color="#808080">NOT</font></span><span> </span><span class="op"><font color="#808080">NULL</font></span><span> AUTO_INCREMENT </span><span class="keyword"><strong><font color="#006699">PRIMARY</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">KEY</font></strong></span><span> , </span> </li> <li class=""> <span>`isbn` </span><span class="keyword"><strong><font color="#006699">varchar</font></strong></span><span>( 10 ) </span><span class="op"><font color="#808080">NOT</font></span><span> </span><span class="op"><font color="#808080">NULL</font></span><span> , </span> </li> <li class="alt"> <span>`title` </span><span class="keyword"><strong><font color="#006699">varchar</font></strong></span><span>( 127 ) </span><span class="op"><font color="#808080">NOT</font></span><span> </span><span class="op"><font color="#808080">NULL</font></span><span> , </span> </li> <li class=""> <span>`description` text </span><span class="op"><font color="#808080">NOT</font></span><span> </span><span class="op"><font color="#808080">NULL</font></span><span> , </span> </li> <li class="alt"> <span>`author_name` </span><span class="keyword"><strong><font color="#006699">varchar</font></strong></span><span>( 127 ) </span><span class="op"><font color="#808080">NOT</font></span><span> </span><span class="op"><font color="#808080">NULL</font></span><span> </span> </li> <li class=""><span>) </span></li> <li class="alt"><span> </span></li> </ol>
3. Place a new CakePHP folder into the root of your web page directory. Rename the Cake folder to data-access.
4 and enter the /app/config directory under the Cake installation folder. You find a file named database.php.default. Rename this file to database.php. Open it using your favorite editor. Edit the $default array in the file to configure your database. After editing, it should look similar to the following content
<ol class="dp-sql"> <li class="alt"><span><span>var $</span><span class="keyword"><strong><font color="#006699">default</font></strong></span><span> = array( </span></span></li> <li class=""> <span> </span><span class="string"><font color="#0000ff">'driver'</font></span><span> => </span><span class="string"><font color="#0000ff">'mysql'</font></span><span>, </span> </li> <li class="alt"> <span> </span><span class="string"><font color="#0000ff">'persistent'</font></span><span> => </span><span class="keyword"><strong><font color="#006699">false</font></strong></span><span>, </span> </li> <li class=""> <span> </span><span class="string"><font color="#0000ff">'host'</font></span><span> => </span><span class="string"><font color="#0000ff">'localhost'</font></span><span>, </span> </li> <li class="alt"> <span> </span><span class="string"><font color="#0000ff">'port'</font></span><span> => </span><span class="string"><font color="#0000ff">''</font></span><span>, </span> </li> <li class=""> <span> </span><span class="string"><font color="#0000ff">'login'</font></span><span> => </span><span class="string"><font color="#0000ff">'username'</font></span><span>, </span> </li> <li class="alt"> <span> </span><span class="string"><font color="#0000ff">'password'</font></span><span> => </span><span class="string"><font color="#0000ff">'password'</font></span><span>, </span> </li> <li class=""> <span> </span><span class="string"><font color="#0000ff">'database'</font></span><span> => </span><span class="string"><font color="#0000ff">'data-access'</font></span><span>, </span> </li> <li class="alt"> <span> </span><span class="string"><font color="#0000ff">'schema'</font></span><span> => </span><span class="string"><font color="#0000ff">''</font></span><span>, </span> </li> <li class=""> <span> </span><span class="string"><font color="#0000ff">'prefix'</font></span><span> => </span><span class="string"><font color="#0000ff">''</font></span><span>, </span> </li> <li class="alt"> <span> </span><span class="string"><font color="#0000ff">'encoding'</font></span><span> => </span><span class="string"><font color="#0000ff">''</font></span><span> </span> </li> <li class=""><span> ); </span></li> </ol>
5. Now, enter the following address in your browser