For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > January 2005 > Re: [PEAR] Many-to-many / cascading deletes with DataObject and FormBuilder









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PEAR] Many-to-many / cascading deletes with DataObject and FormBuilder
Justin Patrin

2005-01-31, 8:57 pm

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com