Update large CakePHP model, but *don't* touch certain fields?
Using CakePHP 1.3
I have a fairly large model in CakePHP, and I'd like to have some hidde开发者_StackOverflow中文版n 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()
?
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);
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.
精彩评论