beforesave only is value is true

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

beforesave only is value is true

Postby PolderBoy » Tue Feb 23, 2010 10:06 am

Hello all,

I would like to save a record only if the value in an other table doesn't exist.

So something like
Code: Select all
function beforesave(&$record)
{
if (value_from_other_table is true)
{
    save
} else {
     do_nothing
}
}


Can't think of a way of doing this.
Could't find it in the search.

Thanks PolderBoy
PolderBoy
 
Posts: 72
Joined: Fri Apr 03, 2009 12:53 am

Re: beforesave only is value is true

Postby shannah » Tue Feb 23, 2010 10:16 am

Code: Select all
function beforeSave(&$record){
    $otherRecord = df_get_record('other_table', array('other_record_id'=>'='.$record->val('other_record_foreign_key')));
    if ( $otherRecord and !$otherRecord->val('fieldname') ){
        // do something...
    }
}
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: beforesave only is value is true

Postby PolderBoy » Tue Feb 23, 2010 3:06 pm

hello Steve,

Thanks for the reply, but maybe I wasn't clear in my question.
What I meant was that the save shouldn't happen when the value from the other table is true and the save should happen when the value is false.

Sorry for the miscommunication.

PolderBoy
PolderBoy
 
Posts: 72
Joined: Fri Apr 03, 2009 12:53 am

Re: beforesave only is value is true

Postby shannah » Tue Feb 23, 2010 3:20 pm

There is an example here: http://xataface.com/documentation/tutor ... d/triggers
See section on handling errors.

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

Re: beforesave only is value is true

Postby PolderBoy » Tue Feb 23, 2010 3:27 pm

Thanks Steve,

Is this code you refere to:
Code: Select all
<?
class tables_Course {
    function beforeInsert(&$record){
        $response =& Dataface_Application::getResponse();
        if ( mail('shannah@sfu.ca', 'Notification', 'Your record was created')){
            $response['--msg'] .= "\nEmail sent successfully to shannah@sfu.ca";
        } else {
            return PEAR::raiseError(
                        "Errors occurred while sending email.  Record could not be inserted",
                        DATAFACE_E_NOTICE);
        }
    }
     
}
?>


Am I correct is thinking that when using the function:
return PEAR::raiseError()
the record doesn't get saved?

PolderBoy
PolderBoy
 
Posts: 72
Joined: Fri Apr 03, 2009 12:53 am

Re: beforesave only is value is true

Postby shannah » Tue Feb 23, 2010 3:32 pm

Yes. That's correct.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: beforesave only is value is true

Postby PolderBoy » Wed Feb 24, 2010 4:07 am

Hello Steve,

I tried your solution and it works like a charm.
But the problem now is that the value (although not saved) still is in the textbox.
So I removed the PEAR line and tried to do a header("Location:index.php?-table=ab_pro_reservations&-action=browse");
And this reloads the page (so that the 'old' values are in the text boxes).
But this gives me the problem that there is no message saying that the save couldn't happen because of the value in the other table.

So I put the message in the link:
header("Location:index.php?-table=ab_pro_reservations&-action=browse&--msg=Kan+niet+opgeslagen+worden.");

Then I realised that the page loads the first record and not the current one so I changed the link:
header("Location:index.php?-table=ab_pro_reservations&-action=browse&id=" . $record->val('id') . "&--msg=Kan+niet+opgeslagen+worden.");


God I love Xataface.
Thanks Steve

PolderBoy
PolderBoy
 
Posts: 72
Joined: Fri Apr 03, 2009 12:53 am


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 14 guests

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