Create multiple entries in the table with one submitted form

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

Create multiple entries in the table with one submitted form

Postby GrosBedo » Wed Dec 31, 2008 9:11 am

Hello again :-)

I would like to create multiple entries in the same table, when an admin create one new entry. In detail, what I want to do is to duplicate this new entry, modify a field (username) for each of these new instances, and then submit these new entries in the database.

I think I should use a beforeinsert trigger, and then get by myself every fields contained in the record submitted and then make some custom sql calls. Is there a better way, by using some Xataface method ?

Another thing: if I indeed do some custom sql calls to insert my duplicated records, how can I cancel the original one submitted by the admin ?

BTW, Happy New Year and long life to Xataface ! :D
GrosBedo
 
Posts: 16
Joined: Wed Aug 13, 2008 10:44 am

Postby shannah » Thu Jan 01, 2009 1:02 pm

Probably best to use the after_action_new trigger.

e.g.

Code: Select all
function after_action_new($params=array()){
    $record =& $params['record'];
    $vals = $record->vals();
    unset($vals['id']);  // (i.e. remove fields that should not be in the
         // duplicate records).
    // Now create all of the duplicates
    foreach ( array('steve','john','peter') as $username ){
        $newrec = new Dataface_Record('mytable', array());
        $newrec->setValues($vals);
        $newrec->setValue('username', $username);
        $newrec->save();
    }
}


This trigger is called after the new action has successfully taken place. This is different than the afterInsert() trigger which is called after a record is inserted (i.e. every time save() is called).
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Create multiple entries in the table with one submitted form

Postby barryrs » Tue Feb 15, 2011 1:11 pm

I may be crazy, but I've used the snippet above to: grab the base data for a new record, loop thru and create 3 new records.
What happens is: My error log shows the 3 entries, but the db ends up with only 1 new record (ClientsId=3)
Thoughts, Ideas? Thanks -Baer

function after_action_new($params=array())
{
$record =& $params['record'];
$vals = $record->vals();
unset($vals['ClientsId']); // (i.e. remove fields that should not be in the duplicate records).
$sql = "select ClientsId from clients where HomeOwner=1";
$res = mysql_query($sql, df_db());

//$row = mysql_fetch_array($res, MYSQL_NUM);

for($i=0;$i<3;$i++) //while ($row = mysql_fetch_row($res, MYSQL_NUM))
{
$row = mysql_fetch_row($res, MYSQL_NUM);
$newrec = new Dataface_Record('transaction', array());
$newrec->setValues($vals);
$newrec->setValue('ClientsId', $row[0]);
error_log($row[0]." Save ".$newrec->getValue('ClientsId')." ClientsId", 0);
$newrec->save();
}
}
barryrs
 
Posts: 14
Joined: Tue Feb 15, 2011 11:37 am

Re: Create multiple entries in the table with one submitted form

Postby shannah » Tue Feb 15, 2011 1:50 pm

strange. try calling
Code: Select all
unset($newrec);

at the end of each iteration.
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 20 guests

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