Update large CakePHP model, but *don't* touch certain fields?

最后都变了- 提交于 2019-12-06 10:08:42

问题


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()?


回答1:


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);



回答2:


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.



来源:https://stackoverflow.com/questions/3764247/update-large-cakephp-model-but-dont-touch-certain-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!