views:

83

answers:

2

Hello everybody. I want to save all but some excluded fields. I know that I can do it this way

$this->Blah->save($this->data,false,$fieldList)

Where $fieldList contains all the data fields of the table but these I don't want to get saved. I have some tables that have maaany data fields, and I don't want to write the whole list from scratch in every single controller action (yes, the fields that should not be saved differ from action to action). Additionally, it looky messy and confusing. Is cakePHP providing something ready-to-use for this case? If not, I guess, I'd have to implement it by myself by adding a $fieldList property to every controller and doing something like this (ugly-hacked-together-solution):

$tmp = $fieldList;
unset(array_search('fieldtoexclude', $tmp));
$this->Blah->save($this->data,false,$tmp);

Best Regards

+1  A: 
function blacklist($blackList = array()) {
    return array_diff(array_keys($this->schema()), $blackList);
}

shoud work

Take a look at: http://www.dereuromark.de/2010/09/21/saving-model-data-and-security/ for details

mark
A: 

If the field list changes from action to action, then you're looking for an automagic function that can read your mind. Cake doesn't provide that!

Somewhere you have to say which fields are to be excluded and doing so longhand in a clear way will make your code much more maintainable.

If it is only one controller, define the list as a class variable, or alternatively subclass the save action on the model.

Leo