I have added this behavior to my Subversion repository and created a small blog article about it... I hope someone finds this useful.
Automatically prevent records with depending children from being deleted: http://josh.ch/wordpress/?p=80 On Fri, Nov 5, 2010 at 9:46 AM, Joshua Muheim <psybea...@gmail.com> wrote: > It's done. :-) > > <?php > /** > * ??? > */ > class ForeignKeyVerifiableBehavior extends ModelBehavior { > function setup(&$Model, $settings) { > $Model->orphansProtectableOptions = array_merge(array( > ), $settings); > } > > /** > * ??? > */ > function beforeValidate(&$Model) { > $valid = true; > foreach($Model->belongsTo as $model => $settings) { > $foreignKey = $settings['foreignKey']; > $foreignKeyValue = $Model->data[$Model->name][$foreignKey]; > if(!empty($foreignKeyValue)) { > $Model->{$model}->id = $foreignKeyValue; > if(!$Model->{$model}->exists()) { > $Model->invalidate($foreignKey, sprintf(__('ID %s does not > exist', true), $foreignKeyValue)); > $valid = false; > } > } > } > return $valid; > } > } > ?> > > Maybe I will upload this to my SVN and add a blog post for it, soon... > > On Thu, Nov 4, 2010 at 9:51 PM, cricket <zijn.digi...@gmail.com> wrote: >> On Thu, Nov 4, 2010 at 5:57 AM, psybear83 <psybea...@gmail.com> wrote: >>> Hi everybody >>> >>> Before creating a behavior myself I wanted to ask this (see title). >>> >>> I only found this: >>> http://ask.cakephp.org/questions/view/how_to_check_the_validness_of_a_foreign-key_must_contained_in_primary-key_list >>> >>> ...but there's no solution for the problem. >>> >>> If nothing like this exists yet, I will create a behavior on my own >>> and will be happy to share it with the community. Just don't want to >>> do work that's already been done. >> >> It should be straightforward to create a custom validation method.I've >> never considered using a behavior for validation but I think it could >> be done in principle. >> >> Check out the new CakePHP Questions site http://cakeqs.org and help others >> with their CakePHP related questions. >> >> You received this message because you are subscribed to the Google Groups >> "CakePHP" group. >> To post to this group, send email to cake-php@googlegroups.com >> To unsubscribe from this group, send email to >> cake-php+unsubscr...@googlegroups.com For more options, visit this group at >> http://groups.google.com/group/cake-php?hl=en >> > -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php