views:

77

answers:

2

Using CakePHP 1.3

I have a fairly large model in CakePHP, and I'd like to have some hidden elements on the form page to (manually) compare/validate against before saving, but when doing a saveAll() (with validation), I don't want these fields present (essentially to avoid them being updated).

What's the proper way to handle this? Remove them from $this->data before handing that to saveAll()?

+2  A: 

I'll usually use unset() prior to the saveAll(). If you think about it, it's the smarest/easiest way. That is, unless you want to manually name the hidden input fields different than the default data[Model][field] that is generated by the form helper.

But then you'd have to access them manually and validate them manually.

unset() is fast and clear.

Stephen
Stephen, this looks great, and is definitely simple. Thanks for taking the time to answer!
anonymous coward
+2  A: 

Use the 'fieldlist' option:

$this->Model->saveAll($data, array('fieldlist' => array('fields', 'to', 'save')));

$fields = array_keys($this->Model->_schema);
$fieldsNotToSave = array('field1', 'field2');
$fieldsToSave = array_diff($fields, $fieldsNotToSave);
deceze
That's something I was looking into, but - since my model is a bit large, is there a complimenting function that does it the other way around: defines fields NOT to save? I have far fewer to NOT save than to save.
anonymous coward
@anon No, you should always go for *whitelisting* instead of *blacklisting*, or it can bite you in the rear later. See my edit though for an alternative.
deceze