Thinkphp dynamically creates a data table structure

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn the business. You need to add a table every month to deal with a large amount of data. The following is the source code for the dynamic creation of data table structures in thinkphp.
$ Table = 'activelog'. date ('ym ');
$ SQL = "CREATE TABLE IF NOT EXISTS '$ table '(
'Id' int (11) not null AUTO_INCREMENT,
'System' tinyint (4) not null comment 'xxxxxx ',
'Pfid' smallint (4) not null,
'Uid' int (11) not null,
.....
'Update _ time' timestamp not null default CURRENT_TIMESTAMP ON update CURRENT_TIMESTAMP,
Primary key ('id ')
) ENGINE = InnoDB default charset = utf8 COMMENT = 'Active statistics' AUTO_INCREMENT = 1 ;";
M ('','', 'db _ CONFIG_ACTIVE ')-> execute ($ SQL); // note that if you create a table here, do not set the first parameter, otherwise, the table information will be detected, but the table does not exist, resulting in an error.


Let's look at the details:

Thinkphp3.2 will enable data table field detection by default, in Model. class. php

View Source Code
// Whether to automatically check the field information of the data table
Protected $ autoCheckFields = true;
In the db method, there are 1409 lines of code:

If (! Empty ($ this-> name) & $ this-> autoCheckFields) $ this-> _ checkTableInfo ();
$ This-> name is set in the constructor, that is, the first parameter of M ('','', 'xxx. Parameter 3 is the database connection information.

I encountered this when writing business logic today. By the way, I hope it will help you.

You can also refer to my personal website: http://www.isbala.com/article/270.html

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.