Page 1 of 1

Default value for fields

PostPosted: Tue Mar 16, 2010 12:04 pm
by halodrio
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

Re: Default value for fields

PostPosted: Tue Mar 16, 2010 12:33 pm
by shannah
Use the xxx__default() delegate class method.

e.g.
Code: Select all
function first_name__default(){
    return "Bob";
}

Re: Default value for fields

PostPosted: Tue Jun 22, 2010 3:19 pm
by forbin3000
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!

Re: Default value for fields

PostPosted: Tue Jun 22, 2010 3:26 pm
by shannah
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

Re: Default value for fields

PostPosted: Tue Jun 22, 2010 9:14 pm
by forbin3000
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?

Re: Default value for fields

PostPosted: Tue Jun 22, 2010 9:51 pm
by shannah
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?

Re: Default value for fields

PostPosted: Wed Jun 23, 2010 8:33 am
by forbin3000
The parent entity will be populated with an afterInsert trigger on the individual table.

Re: Default value for fields

PostPosted: Wed Jun 23, 2010 9:20 am
by shannah
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

Re: Default value for fields

PostPosted: Wed Jun 30, 2010 2:44 pm
by forbin3000
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.

Re: Default value for fields

PostPosted: Mon Jul 05, 2010 4:36 pm
by shannah
Yes it should update the auto increment value.