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

Modularization / Override relation check

posted by ben 8 years ago

Hi!

I have the following problem/question. I have a table 'images' and I have a table 'products', which references 'images'. My application is modularized, 'images' is in core and 'products' belongs to a module. Not all modules are loaded all the time, but the database-tables are still present.

So the problem is, if the fActiveRecord 'Product' is not present, i can not delete an entry from 'images'. An exception ("The noun specified could not be singularized") is thrown. I think this message comes from a relation check?

Is there a way to implement a "clean" solution for this problem? Can i override the relation check?

Please let me know if you need some code or a database-model. Thanks for your help!

Greets Ben

So I can definitely tell you that it is trying to load the Product class to call ->delete() on all child records. This ensures that any delete hook callbacks get called on the children.

The message "The noun specified could not be singularized" is not a normal occurrence, or are you mapping table names to class names? If so, perhaps try moving your fORM::mapClassToTable() calls to your common init code.

Right now there is no way to prevent calling ->delete() on related fActiveRecord objects, precisely because you are (normally) going to want to execute delete hook callbacks if they exist. It is also entirely possible the related fActiveRecord ->delete() method has been overridden and does more than the default.

Perhaps your core should include a skeleton of all fActiveRecord classes that can be loaded if they don't exist in a module? You could also use something like fORM::defineActiveRecordClass(), but I wouldn't recommend it for production since it uses eval(). This technique seems like it could end up causing the wrong class/functionality to be loaded, but if you are careful to ensure a module is never disabled once database contents exist for it, it may work.

posted by wbond 8 years ago

Thanks for your reply. Maybe I could include the skeletons in core, although in my opinion, this isn't the right way if trying to implement a clean modularization. The solution with fORM::defineActiveRecordClass() seems a little bit cleaner to me, because it is more dynamic. Maybe I could try to override the delete-method, so i can prevent the records from being deleted. A simple exception if the image is referenced would be enough for me.

Another thought: Maybe I should explain a little bit more precise what I want to do. I don't want to cascade. I would like to prevent image from being deleted if it is referenced. Like 'ON DELETE NO ACTION' in SQL. Is there a support for defining this 'relation-type'?

posted by ben 8 years ago

Sorry for the late follow-up. If in your database you define the ON DELETE clause of your foreign key to be ON DELETE NO ACTION, Flourish will respect that. It should also give you nice error messaging if it hits that situation.

posted by wbond 8 years ago