Data Entry with Xataface
Written by Nikos Vaggalis   
Article Index
Data Entry with Xataface
Creating a Brand
Using the Grid Widget

 

Model to Operating System

As a final example we will examine another m:n relationship, that
of a Model to OS, by using the grid widget . One model can have one or more OSs installed and one OS can be installed in one or more models.

Let's make an entry for Windows Phone, Version 8.1:

11.osinsert

We see that it was automatically assigned the id 44:

12.osview
For making the ModelOS Edit screen:

windows phone 8.1 44 dataface application 2016 02 18 23.18.35
we have to add the following to the files inside the ModelOS directory:

fields.ini

__sql__ = "SELECT ModelOS.*,concat(OSName,' ',OSVersion) as
                                             OSName from OS,ModelOS,Model
         where ModelOS.OS_OSid=OS.OSid and
                           ModelOS.Model_ModelId=Model.ModelId" 
;So OS OSId does not display a number but
              instead display Windows Phone 8.10

[OS_OSid]
widget:type = select
vocabulary = OSs
 
[Model_ModelId]
widget:type = select
vocabulary = Models

[ModelOSInstalled]
widget:type=select
vocabulary = ModelOSInstalled
order=4

[ModelOSUpgraded]
widget:type=select
vocabulary = ModelOSUpgraded
order=5

[ModelOSSupported]
widget:type=select
vocabulary = ModelOSSupported
order=6
 

 

relationships.ini

[OSs]
OS.OSid="$OS_OSId"

[Models]
Model.ModelId="$Model_ModelId"
 

 

valuelists.ini

[OSs]
 __sql__ = "SELECT OSid,Concat(OSName,OSVersion) from OS"

[Models]
__sql__ = "SELECT ModelId,ModelName from Model"

[ModelOSSupported]
Y=Y

[ModelOSInstalled]
Y=Y

[ModelOSUpgraded]
Y=Y
 


For making an OS grid appear in Model's Edit form,we have to add the following to the files inside the Model directory:


entries for field.ini:

[modelos]
transient=1
relationship=modelos
widget:type=grid
widget:columns="OS_OSid,
                         ModelOSInstalled,
                             ModelOSUpgraded,
                                ModelOSSupported"


'transient' means that the field/column does not exist in the Model table but comes from another table. 

entries for relationships.ini :

[modelos]
__sql__ = "SELECT * FROM ModelOS
                 WHERE Model_ModelId='$ModelId'"
 


We use the grid widget which gives us the capability of making even more complex selections:

13.modelosinsertoneos

14.mysqlmodelos


19.tabletdetailsos

According to the Model to OS relationship, we can even add a second OS:

15.modelosinserttwoos

 

More advanced functionality

That's the basic idea but additionally Xataface offers more advanced functionality and  customizations through its API. You can for example use triggers and Delegate classes to update tables that require special treatment not covered by the defaults.For example the following code is fired before and after saving a Model's record: 

Model/Model.php

 <?php
class tables_Model {

function afterSave(Dataface_Record $record){
         
   if ( $record->valueChanged('FrequencyId') ){
     if ($record->val('FrequencyId')) {
             $modelid=$record->val('ModelId');
             $frequencyid=$record->val('FrequencyId');

         df_q( "INSERT INTO ModelFrequency    
           (Model_ModelId,Frequency_FrequencyId)
             VALUES ($modelid,$frequencyid)");
             df_clear_views(); df_clear_cache();
        }
    }
  
  }

function beforeSave(Dataface_Record $record){

   if ( $record->valueChanged('FrequencyId') ){
       df_q("delete ModelFrequency.*
             from ModelFrequency,Frequency
             where Model_ModelId='".$record->val('ModelId')."' and
               ModelFrequency.Frequency_FrequencyId=
                   Frequency.FrequencyId and
                                 Frequency.G_GId=2");
   }
 
 }

}

?>


This is also an example of a Delegate class. A delegate class is a PHP class that defines custom behavior, functions, and fields for a table in a Xataface application. 

A table may have only one delegate class, thus there can be only one .php file per table directory, i.e Model/Model.php

(Note that to avoid SQL injections you have to always validate input and escape strings that you include in your SQL queries.)


The available triggers are:

  • beforeSave - called just before a record is saved to the database.

  • afterSave- called just after a record is saved to the database.

  • beforeInsert - called just before a record is inserted into the database.

  • afterInsert - called just after a record is inserted into the database.

  • beforeUpdate - called just before an existing record is updated in the database.

  • afterUpdate - called just after an existing record is updated in the database 

Other examples of what you can do:

  • Check whether the user is logged in : Dataface_AuthenticationTool::getInstance()->isLoggedIn();

  • have custom fields,called 'grafted',which can be attached to a table as virtual fields and subsequently used for grouping values etc

  • export data into pdf or xml

  • use Javascript and CSS

  • a wide variety of widgets to choose from : autocomplete,calendar,datepicker,
                             depselect,grid,file,password etc

and much more through the exposed API

Summary

Summing up, Xataface is a feature rich framework that can be further extended and customized, easing the tedious but crucial task of data entry. Give it a try and make a gift to your data entry personnel;they'll love it!

(You can download the files used in this tutorial from this Google Drive link) 

xatafacesq

More Information

Xataface

Xataface Getting started

Xataface API

Smart Device Seeker

Related Articles

MySQL Workbench 6.0 Released

A Generic SQL Performance Test Harness

Full Text Database Indexing with dtSearch

Linq to ADO - using the TableAdapter

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter,subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin

 

Banner


Google's Ban On Paid Chrome Extensions Now Permanent
29/09/2020

Back in March 2020 Google temporarily suspended publishing Chrome paid extensions. Now it has announced that this ban has been made permanent and from February 2021 all paid Chrome extensions wil [ ... ]



Lightstep Adds New GitHub Action
22/10/2020

Lightstep has announced a new GitHub action, Lightstep Pre-Deploy Check, designed to help developers be proactive about ensuring the quality and performance of their software before it’s actually de [ ... ]


More News

 

square

 



 

Comments




or email your comment to: comments@i-programmer.info