Nothing is perfect and you are going to realize that time and again when developing your web app! I do not mean to say that QCubed is perfect or that it will make your app perfect overnight. No, that is not the case and will never be. QCubed, or for that matter no framework can actually make your web app perfect or even near. All that frameworks do is to relax you from the boring part of repeated mundane tasks. What makes the app perfect (or near to perfect) is the way you design it and how you organize it and how you use a framework's features. One of the most useful and astounding features of QCubed is automated Code Generation which we talked about in the last article. Here I explain the most exciting feature provided to you by the CodeGen process - preservation of Table Classes!
The best way I can tell you about what I want to tell is by giving you an example. Get your mind a little cool, you are going to need a little attention to understand what I am about to say. Also, if you do not get it, read again, or drop me a comment on the article. Now, let us begin:
Let us say you have a table in your database with name product (with a integer column product_id as the primary key) which holds the price, type of product, manufacturer name, etc. for a product. Also, you have two other tables with foreign keys to the 'product' table. Let us name those two tables as washing_machine and television, each with a primary key of type integer and named product_id. These tables hold the specific details of the product in different columns.
In such a case, it is very desirable to have a function which would get a row from the 'product' table, determine whether it is a 'television' or a 'washing machine', then fetch data from the right table and give back the product's specifications and price. Given that we make such a function, it will help you a lot in future! So we make that one function and where do we place it? At some place in codebase? Yeah, of course, we have to put it somewhere in the codebase but where?
Given the above scenario, when you do a codegen, QCubed will create new files in the "includes/model" directory and their names are going to be Product, WashingMachine and Television. These are the Table Classes. You get one class per table in your database.If you have more than one database which contain conflicting names, remember to utilize prefix or suffix settings for those databases in the settings.xml file. This file is also located in the "includes/configuration" directory
In the "includes/model" directory, you will also find a subdirectory named 'generated'. In the generated directory, you will find files like ProductGen, WashingMachineGen and TelevisionGen. These files are the TableGen Classes.
If you open the Table and respective TableGen class files for any table, you will notice that the only useful code in a Table class-file is the __tostring magic function and all the functions that are mentioned on the QCubed Example site are contained in the TableGen class and the Table class simply extends the TableGen class!
The fact is that Table classes are there to be modified. That is the sole purpose of their existence. Whenever you would do a code generation, the code generator first sees if the Table class file is present in the "includes/model" directory or not. If the class file is not present, it will create it (thus including a new file for a new table you might have created) and if it is already present, it will not modify or overwrite the file (thus preserving your changes).
When codegen preserves the Table class file, it helps you do two things:
Prevention: You can place all your single table related functions in the Table class file and make them work seamlessly with any Table class object (in out example, any "Product" class object). Also, it is perfect place to hide code for a table from other table classes! This prevents a lot of mess which would occur with custom functions written in different files! It prevents the 'disorganization' that can be caused with time as you progress towards making more tables and include more functions!
Cure: It is a cure to the problem which could have been caused by QCubed itself: overwritten class files. Since the check is first done, it cures the problem of having writing code time and again!
Hence, in the example case I provided, the best place to keep that function to determine the type of product and get the details would be the 'Product' class file, inside the Product class! This is one way QCubed provides you an amazing way to organize your code!