Data Entry with Xataface
Written by Nikos Vaggalis   
Monday, 29 February 2016
Article Index
Data Entry with Xataface
Creating a Brand
Using the Grid Widget

Inserting a new record

So let's add a new Brand item. We simply have to click Brand on the top left of the interface so we navigate to the relevant section,  then New Entry and finally filling the form with the value Linx:


1.linx insert1
So without typing any code,SQL or otherwise, Xataface automatically made the BrandName column available for data entering,and furthermore associates with a BrandId Primary Key. BrandId is defined as an AutoIncrement field thus Mysql takes care of it,in this instance assigning it the id 206

We can also Edit and View the record:

Edit:
2.linx edit
View :
3.linx view

 

Associating a Brand with a Model 

The fun part is that we can then associate the newly created Brand record to a Model record when building such a Model (tablet or smartwatch) by selecting it from a list:

4.linx modelscreenchoosebrand

 

That is part of the story however. To enable such a relationship there is some tweaking to be done. It's where we need to fill the .ini files inside Model's directory. So we create the following files inside the Model directory:

 

fields.ini
releationships.ini
valuelists.ini 


Now add the following inside field.ini:

 

[Brand_BrandId]
widget:type = select
vocabulary = Brands 

 

We tell Xataface to create a form field with a widget of  type Select (which is going to be rendered as a HTML selection list),call it [Brand_BrandId], and instruct it to be filled with values from the Brands vocabulary.

Next onto the Brands vocabulary. Add the following inside valuelists.ini:

 

[Brands]
__sql__ = "SELECT BrandId,BrandName from Brand" 

 

This populates the select list with the Brand Names as in the picture  above which shows a list of all the Brand Names existing in the Brand table, but selecting just Linx from the available options. Xataface is smart enough to display the character based column of the table (Brand Name) but underneath refer to it with a numeric Brand Id

Next,add the following inside relationships.ini:

 

[Brands]
Brand.BrandId="$Brand_BrandId" 


We define an association that maps the Primary Key BrandId to $Brand_BrandId,the actual renderd form field  (see [Brand_BrandId] in fields.ini).We could forgo this step and let Xataface handle it but it's better to be explicit

Saving the form will insert a record in the Model table with 206 (Linx) as the value of Foreign key Brand_BrandId  (the actual values will vary from installation to installation)

 

A more complex example

Let's now move to a more complex example and colour to our Linx tablet through the use of the checkbox widget. We first create each colour in the Color table:


6.color insert
 

We append the following into fields.ini of the ModelColor directory:

 

__sql__ = "SELECT ModelColor.*,ColorName from
                                                   Model,ModelColor,Color
  where ModelColor.Model_ModelId=Model.ModelId
           and ModelColor.Color_ColorId=Color.ColorId"
 

Then we append the following to the Model's directory files:

entries for field.ini :

[ColorId]
widget:type = checkbox
transient=1;
repeat=1
relationship=Color
 

 

entries for valuelists.ini,which fill the checkboxes :

[Colors]
__sql__ = "SELECT ColorId,ColorName from Color"
 

 

We depict the Model to Color m:n relationship through the joint ModelColor table inside the relationships.ini file:

[Color]
__sql__ = "SELECT * from Color,ModelColor
       where Color.ColorId=ModelColor.Color_ColorId and
             ModelColor.Model_ModelId ='$ModelId'"
 

 

This instructs Xataface to update the intermediate ModelColor joint table with a Model_ModelId value and its associated Color_ColorID.

In simple words,when we check the 'black' checkbox and press Save, a new record of, say, Model_ModelId=731, Color_ColorID=1 is inserted in ModelColor. If we were to check another color box, an additional entry would be inserted in ModelColor,say: 731,9 (blue) and so on:

10 - dataface application - 2016-02-19 11.57.23

 

So this what the final site depicts:

10.modeldetailscolor
(Note the internal ModelId is 731. The number 10 in the Model row is the name of the Model/tablet)



Last Updated ( Monday, 29 February 2016 )