How to add calculated fields to your list and details viewSometimes it may be desirable to show data in the list and details view that is not actually stored in the record. This how-to shows how you can do this in Dataface 0.6.3.
Xataface provides nice list and details views for your tables, but the information displayed on these pages are limited to fields that are physically part of the table. But what if you want to show some related information that is helpful. For example, what if you have tables users and articles and where the articles table has a column author which stores the user_id of the user who wrote the article. Further, suppose that we want to display the number of articles that each user has written in the list view and the details view? Prior to Xataface 0.6.3, you would have to override the list view template and create your own list, in order to incorporate this column. However, you are now (as of 0.6.3) able to show this information by adding the following to the beginning of the users fields.ini file: __sql__ = "select Note that in PHP 4.x multiline values in ini files were not supported, so you would have to place the above on one line. Now, when Xataface loads the records (or a record) from the users table it will use the query specified here rather than the usual "select * from users". Explanations
ResultsSo what have we really done here? Now the list tab of the users table will include a num_articles column that show the number of articles that each user has written. Similarly the View tab will show this information for each record. What did this NOT do? So far this only helps us for the list tab and the View tab. This extra column will not show up in related records lists. Possibly in future versions, this support will be added. Caveats: Minimum RequirementsThe internal Xataface implementation uses subqueries to seamlessly handle custom SQL select statements such as this. Older versions of MySQL (older than 4.1) do not support subqueries (AFAIK), so this feature will work with MySQL 4.1 or higher. |