Re: Many-to-many / cascading deletes with DataObject and FormBuilder

From: Date: Mon, 31 Jan 2005 23:11:29 +0000
Subject: Re: Many-to-many / cascading deletes with DataObject and FormBuilder
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-17302@lists.php.net to get a copy of this message
On Mon, 31 Jan 2005 13:50:59 -0800, Jamie Alessio <Jamie.Alessio@ucop.edu> wrote: > I'm using FormBuilder to manage many-to-many relationships and it's > working great. It has occured to me that although the inserts and > updates to tables in my many-to-many table are handled auto-magically, > the deletes on the tables referencing my join table leave orphaned rows > in the join table. > > What's the suggested method for handling deletes of this nature? My > initial thought is to override the delete() method in my dataobject > class doing something like this (psuedo-code): > > function delete() { > parent::delete(); > $joinTable_obj =& DB_DataObject::factory('myJoinTable'); > $joinTable_obj->tableAkey = $this->primaryKey; > $joinTable_obj->delete(); > } > > Ideally I could tie into the table relationship information in my > links.ini file to determine what fields to link together for the delete, > but short of that I could just instantiate the related object on my own > in the delete() method. > > Is there a better way to do this? Anyone doing something similar? Thanks. > The best way is to, of course, use cascading deletes on the DB side. If you use the InnoDB table type in Mysql4 you should be able to set up cascading deletes. Without that, I would suggest basically what you say. Either set it up manually for each DO (cumbersome) or create your own DO class which extends DB_DataObject and have your DOs extend that. Override the delete method and check $GLOBALS['_DB_DATAOBJECT']['LINKS'] for links to the current table and check those tables for links to the current record and delete them. -- Justin Patrin

« previous php.pear.general (#17302) next »