Saturday, August 2, 2008

Displaying Xataface record values

Taking a look at the Xataface Record API presents you with several techniques to display the values of the record. At the lowest level, there is a function called val and can be called like this (assuming $rec is a Xataface record)

$rec->val('fieldname')


What this does is it displays the raw value of field. As in exactly what is stored in the database.

But what happens if the value is say a foreign key to another table say like a number of a company. We don't really want to display just the number, but rather the company name of the id. If a valuelist has been setup for this company_id in a valuelist.ini file, then you can easily display the field you really want by simply using the "display" function.

So say for example, in your application delegate's valuelists.ini file you have this:


[companies]
__sql__="select company_id,company_name from companies order by company_name"


So you are basically telling Xataface to use the company_name as a replacement for the company_id. Since this is the application's delegate, this valuelist is accessible by any table and can be overwritten the own table's valuelist. In any case, if in your other table you just call:

$rec->display('company_id')


Xataface will be smart enough to use the company_name in replacement for the company_id. This display function is also useful for database field types datetime, time, or date. If you were simply use the "val" function it would return the date as an array with keys "year", "month", "seconds", etc. And you would be left with having to piece together the information yourself. But by using the display() function you can quickly circumvent this problem.

No comments: