CakePHP does not have a built in mechanism for handling reordering of records, as far as I know. So, I’m using the Ordered Behavior that, as near as I can tell, is the defacto behavior for reordering in CakePHP. So far it’s working well as I add it to various models, however, I’ve got a situation I’m not sure how to deal with.
My model hierarchy is as follows.
Section > Heading > Category > Item
However, Items can be attached directly to sections:
Section > Item
The Item model’s table has both category_id and section_id defined, though only one is actually used for any given record.
The ordered behavior has two parameters set when you set the model’s $actsAs. Here’s the one for my Heading model:
var $actsAs = array('Ordered' => array('field' => 'order','foreign_key' => 'section_id'));
How should I define the $actsAs member for the Item model where it has two foreign keys section_id and category_id to ensure that the ordering/sequence is properly maintained?
I haven’t tested this extensively, but with my first try this appears to be a workaround that will let me dynamically manage the ordering. Inside of my
ItemsControllerI’ve got the following actions:My model doesn’t set the ordered behavior (and thus the
foreign_key) via the$actsAsmember. Instead, prior to any order manipulation, I determine the parent item type and attach the behavior, with appropriateforeign_keyat run time.