search
top

Model::save blacklist – in reply to teknoid

It’s not that i did not know what else to post, but here’s a more general approach for those who like using blackList over whiteList when tighten up save() calls.

This post is ment as follow up to the one teknoid posted. Instead of doing the array_diff stuff in your controller, you could do it in the model (respectively, all models). So here it goes. I made the $blackList part optional, but feel free to change that.

class AppModel extends Model {

	function save($data = null, $validate = true, $fieldList = array(), $blackList = false) {
		if (!empty($fieldList) && $blackList) {
			$fieldList = array_diff(array_keys($this->schema()), $fieldList);
		}
		return parent::save($data, $validate, $fieldList);
	}
}

In your controller you can then use blacklisting like so:

function some_action() {
	if ($this->Whatever->save($this->data, true, a('user_id'), true)) {
		//...
}

I also had ideas to allow both methods by using blacklist/whitelist as array keys of fieldList, but then again.. lets not overdo this. :)

3 Responses to “Model::save blacklist – in reply to teknoid”

  1. [...] Yesterday, Kjell Bublitz (aka m3nt0r) published the following solution for using a blacklist with CakePHP’s Model::save(): [...]

  2. [...] then this is your week. First up is teknoid’s quick way. m3nt0r countered by moving the functionality into AppMode by overriding the default save call. But that approached changed the parameters for Model::save, so [...]

  3. [...] full article on the original website.Yesterday, Kjell Bublitz (aka m3nt0r) published the following solution for using a blacklist with CakePHP’s Model::save(): // app/app_model.php class AppModel extends Model { function save($data = null, $validate = true, [...]

Leave a Reply

top