Wordpress Web Application Development(Third Edition)
上QQ阅读APP看书,第一时间看更新

Extending the database with custom tables

A default WordPress database can be extended by any number of custom tables to suit our project's requirements. The only thing we have to consider is the creation of custom tables over existing ones. There are two major reasons for creating custom tables:

  • Difficulty of matching data to existing tables: In the previous section, we considered real application requirements and matched the data to existing tables. Unfortunately, it's not practical in every scenario. Consider a system where the user purchases books from a shopping cart. We need to keep all the payment and order details for tracking purposes, and these records act as transactions in the system. There is no way that we can find a compatible table for this kind of requirement. Such requirements will be implemented using a collection of custom tables.
  • Increased data volume: As I mentioned earlier, the posts table plays a major role in web applications. When it comes to large-scale applications with a sizeable amount of data, it's not recommended to keep all the data in a posts table. Let's assume that we are building a product catalog that creates millions of orders. Storing order details in the posts table as a custom post type is not the ideal implementation. In such circumstances, the posts table will go out of control due to the large dataset. The same theory applies to the existing metatables as well. In these cases, it's wise to separate different datasets into their own tables to improve performance and keep things manageable.