In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to use CakePHP scaffolding tool to set up database and model, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
The CakePHP scaffolding tool is mainly used to set up databases and models. The following describes the operation principle and method of this CakePHP scaffolding tool.
We already know that model classes are usually used to interact with databases. In CakePHP, a model class usually corresponds to multiple tables in the database. All database operations on the table are implemented through the corresponding model class. The correspondence between CakePHP's model and database tables does not need to be set. Instead, CakePHP uses some simple naming conventions to achieve this effect, and in this section, we will learn how to create existing model classes for tables 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 feature to do this.
Create a model for tables in the 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 this table. Then we will also use the scaffolding feature to do a quick test of the newly created model and data table.
Hands-on time: create a database table and the corresponding model
1. At the MySQL command prompt, we enter the following database command to create a new database named data-access.
CREATE DATABASE `data- access`
Create a "books" table by executing the following SQL statement:
USE `data- access`; CREATE TABLE `books` (`id` int (11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `isbn` varchar (10) NOT NULL, `title`varchar (127th) NOT NULL, `substitution`text NOT NULL, `author_ name` varchar (127th) NOT NULL)
3. Place a brand new CakePHP folder in the root of your web page. Rename the folder of Cake to data-access.
4. Go to the / app/config directory under the Cake installation folder. You find a file called 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 paragraph
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 http://localhost/data-access in your browser. The browser will display a message that "CakePHP has been able to connect to the database", and we can confirm that the configuration of the database is very *. If it is not configured correctly, it will display a message that "Cake cannot connect to the data". If you encounter this situation, it is very likely that the database information you provided is incorrect. Immediately return to step 4 and fill in the $default array with the correct database name, database user name and password
6. Create a new PHP file under the / app/models folder and name it book.php
7. In the book.php file, add the following code:
8, now create another PHP file under the / app/controllers folder and name it books_controller.php
9, add the following code to the books_controller.php file:
10. Now, open this address http://localhost/data-access/books/ using a browser and add some test data.
What's going on?
First, we created a database called data-access. Next, we create a database table called books in this database. The books table is structured as follows:
The database table and field names we use here are not randomly selected. CakePHP has a definite naming convention for database tables and fields:
◆ table names must be lowercase in plural (such as boooks,catergories,articles, etc.)
If ◆ needs to name tables and fields with two or more words, we need to use underscores to separate words (such as author_name,user_photos, etc.)
Fields in ◆ database tables must have a primary key named id
In our example, the database table name books we are using is in the English plural. We also have a primary key called id. We need to use two words to name the fifth field of the table, author_name, and we use an underscore to connect the two words.
[if you've written PHP/MySQL applications before, you might name database tables in any way you like, and then write SQL statements to access those databases. However, when you use CakePHP, you should follow its naming convention. Although we do not have to fully follow its naming rules, following the rules will help us save a lot of valuable time. In addition, sticking to this rule usually improves the quality and maintainability of your code. Through this and other chapters of this book, we will see how these rules of CakePHP can help us get things done in a more convenient and cool way. ]
Our database is ready, we have configured our application and connected to the database. In Chapter 3, we have learned how to configure our application to connect to the database. After completing the database configuration, we began to write code for our model.
We already know that in CakePHP, model classes are usually used to access corresponding database tables, so to access books tables, we need to create corresponding model classes. CakePHP has a fixed naming convention for model classes and model files. The specific rules are as follows:
The file name of the ◆ model must be named after the table name of the corresponding database, but it is singular. For example, if your datasheet name is user_photos, then the file name of the model should be user_photo.php
The class name of the ◆ model should be the camel spelled form of the model file name (CamelCased form). If our model file name is user_photo.php, then the model class name should be UserPhoto
The ◆ model files are placed in the / app/models directory.
[CakePHP automatically knows which model class corresponds to which database table through naming conventions. In our example, the class name Book of our model implies that its corresponding database table is a books table. Look, it's so easy, we don't have to do anything to set it manually. ]
Then we create a model class named Book, then name the class file book.php, and save it in the / app/models folder, because we correctly follow the naming convention, CakePHP will now automatically detect that our Book class is the model class corresponding to the database table books.
Once we have created database tables and models, we can use the scaffolding features of CakePHP to create a simple application that can perform basic database operations (such as add, edit, and delete). Between using scaffolding with the Book model, we first need to create a related controller class, BooksController. Then we add a line of variable $scaffold to the controller class. This short piece of code will build a scaffolding for the Book model corresponding to the database table books-yes, it's that simple!
Visiting this link http://localhost/data-access/books/ Magi CakePHP scaffolding has created a basic application for us with add, read, update and delete functions. After adding some data, the page should look like the following figure:
[what is needed to create a scaffolding includes a database table, a corresponding model and controller, and some rules. Once everything is ready, we just need to set a $scaffold property in the controller to create a basically working application! ]
Scaffolding is a shortcut we use to test database tables, which are often changed frequently in the early stages of the development cycle. But it is important to remember that the scaffolding function is only a temporary means. Nor does it have full flexibility. In specialized applications, we all need to customize our own logic and user interface. To do this, we take down the scaffolding and add some of our own code. We'll soon see how to write some actual code with CRUD (create, read, update, delete) functionality.
This is the answer to the question about how to use the CakePHP scaffolding tool to set up the database and model. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.