before_create trigger problems

[prev] [thread] [next] [Date index for 2004/12/21]

From: Emile Aben
Subject: before_create trigger problems
Date: 01:39 on 21 Dec 2004
Hi,

I have a before_create/before_update that I'm trying
to get to update/delete from the same table as the
create/update is working on. The background of this is
that I store start and end dates for specific data and
I don't want them to overlap. In case I find an
overlap I want the old value to be deleted, for
instance:

NEW timerange: 8700->9300  value A
OLD timerange: 4000->9000  value B

should insert the new (timerange, A) and update the
old (timerange, B) so the old timerange ends at 8699.

I have code that detects al kinds of overlaps and
tries to update the old rows, but somehow they won't
get actually updated, I get a warning that says:

MonitorData=HASH(0x6c3498) destroyed without saving
changes to end_date at
/usr/local/perl-5.6.0/lib/site_perl/5.6.0/Class/Trigger.pm
line 51

Is there anything that prevents updates/deletes from
within a trigger?


thanks,
Emile

PS: the complete sub that is called from both
before_insert and before_update:

sub _fixup_exclusive {
	my ($this) = @_;
	# shouldn't be too many @attrs that match
	my @mondata_list = __PACKAGE__->search ( 
		{ 
			monitor => $this->monitor,
			attribute => $this->attribute,
		}
	);
	foreach my $old (@mondata_list) {
		next if ( $this->id && $this->id eq $old->id);
		next if ($old->end_date && ($old->end_date <
$this->start_date));
		print STDERR "NEW: ". $this->start_date ."->".
$this->end_date . "\n";
		print STDERR "OLD: ".  $old->start_date ."->". 
$old->end_date . "\n";

		# Overlapping patterns
		# o = old, n = new 
		if ( !$this->end_date || ( $old->end_date &&
$this->end_date >= $old->end_date ) &&
$this->start_date <= $old->start_date ) {
			# case:  nnnnnnnnn(...)
			#           ooo(...)
			# solution: delete old completely
			print STDERR "nnnnnnnnnnn(...)\n";
			print STDERR "    ooo(...)\n";
			$old->delete();
		} elsif ($this->end_date && $this->end_date >=
$old->start_date && $this->start_date <=
$old->start_date ) {
			# case: nnnnn
			#          ooooo(...)
			# solution: cut off begin-part of old
			print STDERR "nnnnn\n";
			print STDERR "   ooo(...)\n";
			$old->start_date( $this->end_date + 1 );
			$old->update();
		} elsif ($old->end_date && $this->start_date <=
$old->end_date && $this->start_date >=
$old->start_date ) {
			# case:     nnnnn(...)
			#        ooooo
			# solution: cut off end-part of old
			print STDERR "     nnnnn(...)\n";
			print STDERR "   ooooo\n";
			$old->end_date( $this->start_date - 1 );
		} elsif ($this->end_date && $this->start_date >=
$old->start_date && (!$old->end_date ||
$this->end_date <= $old->end_date ) ) {
			# case:     nnnn
            #        ooooooooo(...)
			# solution: cut old in 2
			print STDERR "     nnnn\n";
			print STDERR "   ooooooooo(...)\n";
			my $old2 = $old->clone();
			$old2->start_date( $this->end_date + 1 );
			$old2->insert();
			$old->end_date ( $this->start_date - 1 );
			$old->update();
		}
		print STDERR "\n\n";
	}
}



		
__________________________________ 
Do you Yahoo!? 
Send holiday email and support a worthy cause. Do good. 
http://celebrity.mail.yahoo.com

(message missing)

before_create trigger problems
Emile Aben 01:39 on 21 Dec 2004

Re: before_create trigger problems
Tony Bowden 09:09 on 21 Dec 2004

Re: before_create trigger problems
Drew Taylor 16:21 on 21 Dec 2004

Re: before_create trigger problems
Emile Aben 17:13 on 21 Dec 2004

Re: before_create trigger problems
Emile Aben 18:35 on 21 Dec 2004

Re: before_create trigger problems
Charles Bailey 19:01 on 21 Dec 2004

Re: before_create trigger problems
Emile Aben 19:28 on 21 Dec 2004

Re: before_create trigger problems
Charles Bailey 15:56 on 22 Dec 2004

Re: before_create trigger problems
Emile Aben 17:24 on 22 Dec 2004

Generated at 12:15 on 16 Jan 2005 by mariachi v0.52