databasesymfony-1.4propeldatabase-integrity

How to update further tables on adding a relationship in propel


I'm trying to work out where is the best place to update other related tables when changing a foreign-key dependency.

To be precise, I have a table called 'flat' and another called 'room' which contains

flat_id: {type: INTEGER, foreignTable:flat, foreignReference: id, required: true }

As well as method Room::SetFlatId(), Propel (1.3) has therefore given me Room::SetFlat()

But when I add a room to a flat, I want to automatically make changes to some other related tables, to keep consistency at a higher level than the database definition. I thought I would override Flat::addRoom(Room $room) to call its parent and then make these further changes.

But I've run into a problem because, since the 'flat_id' column is required, one of SetFlatId() and setFlat() will always be called before the Room object is saved.

But before the Room object is saved, it has no id, and therefore my extended addRoom cannot work because there is no room_id to identify the related records in other tables.

This seems to suggest that I mustn't put code into Flat::addRoom() which depends on the Room having been saved. But then where can I put my code?

Obviously, I could just require it to be called manually after making or changing a Room object; but since what it is actually doing is maintaining a kind of higher-level database integrity, I'm unwilling to do that. I could put it into one or both of the 'save' methods; but it logically needs to be run only when an association between a Room and a Flat is changed.

Where should this code go?


Solution

  • It's a typical case for a custom behavior.