Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Associate will remove existing relations?

posted by mblarsen 9 years ago

I'm seeing some strange deletes in my SQL debug when I'm associating my records.

Will associateRelatedRecords() remove any existing relations?

$some_record->associateOtherRecords(array($otherRecord1, $otherRecord2))

This will invoke DELETE FROM join_table WHERE id = <some_record_id>

Then INSERT INTO join_table SET ... id = <some_record_id>, other_id = <other_record1> and INSERT INTO join_table SET ... id = <some_record_id>, other_id = <other_record2>

So first it will empty the joins between my record and the relations, then it will insert the relations passed on as an array.

(pardon my pseudo code)

Yes, the expected behavior of associate is to accept the list of records as the definitive list. If records are not listed there, they will be removed.

If you have the use for adding records to an association without passing all of the records to associate, I think it would be reasonable to add a new method to perform that function.

posted by wbond 9 years ago

Fair enough - just had to understand how it worked.

So if I wanted to a method that would append a record to the related set, I would use for instance fORM::registerActiveRecordMethod() or are you suggesting to extend the functionality of fActiveRecord?

posted by mblarsen 9 years ago

I think ideally it would be built into Flourish and part of fORMRelated.

posted by wbond 9 years ago