Default value for fields

A place for users and developers of the Xataface to discuss and receive support.

Default value for fields

Postby halodrio » Tue Mar 16, 2010 12:04 pm

Hi,

I want to insert new records to a table. So far, its no problem. But I would like to make this inserts easier.
Is it possible to set up the fields with default values in the “new record” tab? The content for the fields is a result out of a php function.

Thanks
halodrio
 
Posts: 4
Joined: Tue Mar 16, 2010 11:38 am

Re: Default value for fields

Postby shannah » Tue Mar 16, 2010 12:33 pm

Use the xxx__default() delegate class method.

e.g.
Code: Select all
function first_name__default(){
    return "Bob";
}
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Default value for fields

Postby forbin3000 » Tue Jun 22, 2010 3:19 pm

Is there a place online, in the Dataface application directory, or elsewhere to reference all delegate class methods, their inputs and outputs?

How about for directives for the fields.ini file and other .ini files? Thanks!
forbin3000
 
Posts: 6
Joined: Wed May 19, 2010 3:32 pm

Re: Default value for fields

Postby shannah » Tue Jun 22, 2010 3:26 pm

Check the wiki.
http://xataface.com/wiki/

There are pages for most config files and delegate classes. There are some things missing, but most of it is there. If you see an omission on one of the wiki pages, you can leave a comment on the page with your questions or additions so that they can be added to the documentation.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Default value for fields

Postby forbin3000 » Tue Jun 22, 2010 9:14 pm

I'm a stuck here. I'm trying to set the default Entity_ID on my form creating a new "individual" table record:

Code: Select all
CREATE TABLE `individual` (
  `Entity_ID` int(10) unsigned NOT NULL,
   ...
  PRIMARY KEY (`Entity_ID`),
  KEY `fk_individual_entity` (`Entity_ID`),
  CONSTRAINT `fk_individual_entity` FOREIGN KEY (`Entity_ID`) REFERENCES `entity` (`Entity_ID`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


individual.Entity_ID is not AUTO INCREMENT. It will always having a matching Entity_ID with it's parent table "entity" :

Code: Select all
CREATE TABLE `entity` (
  `Entity_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
   ...
  PRIMARY KEY (`Entity_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;


I want to use the delegate class to set the default Entity_ID based on the next auto-increment value of MAX(entity.Entity_ID):

Code: Select all
<?php
class tables_individual {

function Entity_ID__default(){
   $query="[b]SELECT MAX(Entity_ID) FROM entity[/b]";
   $res = df_get_record('entity', $query);
   return $res->val('Entity_ID')+1;
   }
}
?>


The default value returns as 2 (1+1). How can I use the SQL statement MAX(Entity_ID) to get a result set that is an integer? Is there a better way than what I'm trying to do?
forbin3000
 
Posts: 6
Joined: Wed May 19, 2010 3:32 pm

Re: Default value for fields

Postby shannah » Tue Jun 22, 2010 9:51 pm

So at the time when you are creating a new individual, will the parent entity always already exist? How are you ensuring their synchronized entity id?
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Default value for fields

Postby forbin3000 » Wed Jun 23, 2010 8:33 am

The parent entity will be populated with an afterInsert trigger on the individual table.
forbin3000
 
Posts: 6
Joined: Wed May 19, 2010 3:32 pm

Re: Default value for fields

Postby shannah » Wed Jun 23, 2010 9:20 am

Personally, it would make me feel more comfortable if the parent table first got its ID, then that ID is used to populate the child table. That way there is less that can go wrong (e.g. race conditions).

So here's what I would do. Set the default value of the child table to whatever you like (or nothing if the field allows nulls). Then in the beforeInsert method, you set the ID of the child table to match that of the newly created parent record.

e.g.

child table delegate class:
Code: Select all
function beforeInsert(&$record){
     $parent = new Dataface_Record('parent_table', array());
     $parent->setValues(array(
           // .... The values that we're setting on the parent
     ));
     $parent->save();
     $record->setValue('ENTITY_ID', $parent->val('ENTITY_ID'));
     
}


Now you also need to think this through carefully. What happens if you insert a record into the child table and the corresponding parent record already exists? Can that ever happen? Make sure you take that into account for your solution.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Default value for fields

Postby forbin3000 » Wed Jun 30, 2010 2:44 pm

Thanks for pointing these things out Steve. Will the $parent->save() method update the $parent object with the auto-incremented Entity.ID? If not, I'm unsure how I would get it and set it to the $record object.
forbin3000
 
Posts: 6
Joined: Wed May 19, 2010 3:32 pm

Re: Default value for fields

Postby shannah » Mon Jul 05, 2010 4:36 pm

Yes it should update the auto increment value.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 16 guests

Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved