Page 1 of 1

beforesave only is value is true

PostPosted: Tue Feb 23, 2010 10:06 am
by PolderBoy
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

Re: beforesave only is value is true

PostPosted: Tue Feb 23, 2010 10:16 am
by shannah
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...
    }
}

Re: beforesave only is value is true

PostPosted: Tue Feb 23, 2010 3:06 pm
by PolderBoy
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

Re: beforesave only is value is true

PostPosted: Tue Feb 23, 2010 3:20 pm
by shannah
There is an example here: http://xataface.com/documentation/tutor ... d/triggers
See section on handling errors.

-Steve

Re: beforesave only is value is true

PostPosted: Tue Feb 23, 2010 3:27 pm
by PolderBoy
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

Re: beforesave only is value is true

PostPosted: Tue Feb 23, 2010 3:32 pm
by shannah
Yes. That's correct.

Re: beforesave only is value is true

PostPosted: Wed Feb 24, 2010 4:07 am
by PolderBoy
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