Friday, May 1, 2009

Text getting cut off when using the $record->val() function

One thing that got me really frustrated for the longest time was the fact that I didn't realize that Xataface Records when retrieved from the df_get_records() or df_get_records_array() function would have their values chopped off at 255 characters if it got longer than that.

I am sure this feature was added in so that during list views of the table records we don't blow up main memory by loading the entire field of each record. But this can be a tricky situation when you sometimes require the full value and don't realize it! It can have some really funny effects on your code. In my case, I had a field which stored a serialized array of conference ids that a particular user belonged to. So when we viewed the user, we would have to check whether the conference we are currently viewing falls in that list of conference ids.

You can begin to see where the potential problem lies right? If the conference_ids serialized array string is cut off then its incomplete and so when we go to unserialize it, we get basically an empty array! Frustrating!

The solution is quite simple actually. I will list three potential solutions to the problem:


  1. The first solution is to simply add the attribute struct=1 to the appropriate field in the fields.ini file. According to the wiki:

    A boolean (0 or 1) value indicating whether this field is considered a structure. A value of 1 indicates that this field is a structure and should not be truncated under any circumstances. Normally fields are truncated at 255 chars in list view. This is useful if the field contains XML or other structured data so that attempts to truncate it would destroy integrity.


  2. This above solution works, however it then becomes on every request we retrieve the entire text when sometimes it's not actually necessary. If you were using Xataface as a back-end to a webpage, you can control when to actually retrieve the whole 255 characters by sending in a particular parameter to the df_get_records function. According to the API, the df_get_records_array function looks like this:

    function &df_get_records_array($table, $query=null, $start=null, $limit=null, $preview=true){

    There is actually a parameter call $preview which when set to false will return you the whole thing. Nice and easy. That's the way it should be!


  3. This last method gets down to the nitty grity code and can be used for very explicit purposes (eg. one record should display 255 characters while another could also all). All we have to do is just check if the field value is at least 255 which signifies that it could be longer. And then we would just either use the df_get_record() function (This function DOES return the full length of the fields. It is the df_get_records() functions that don't) to get the record and the full value. Or we can just write a quick sql query to get that one column:



No comments: