Thursday, August 14, 2008

Redirecting to a page

Say after an action in Xataface (eg. finished adding a new record), by default it goes to the edit action for that record you just added. But say you want it to go to the new action to add another another. There are a few ways to do this:


  1. One way to do this is to grab the current query:


    The --msg query string variable allows you to display a confirmation message. We put an exit after the header function to make sure nothing else in the script runs.

  2. The above way works, but it leaves the primary id of the record you just added in the url. Probably not the best thing to do, another way to do it is just use the header function and manually enter the query parameters:

    header('Location: '.DATAFACE_SITE_HREF.'?-table=tablename&-action=new


    I used the DATAFACE_SITE_HREF constant variable here which provides the path your application. You NEVER want to hardcode your application path since portable. You can put this function like in your after_action_new() trigger so that after you add a record, it allow you to add another record right away.

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.