When CakePHP save doesn’t work – a possible solution

After some frustrating experiences with Cake’s save() and saveAll() functions. They would simply not save the data! I thought the whole thing was messed up and I actually wrote a massive blog post about how to fix it last night. However in my editing and checking dismorning I found the following snippet of code in model.php’s method set.

731
732
733
734
735
736
737
738
739
740
741
if (is_array($one)) {
	$data = $one;
	if (empty($one[$this->alias])) {
		$keys = array_keys($one);
		if (in_array($keys[0], array_keys($this->_schema))) {
			$data = array($this->alias => $one);
		}
	}
} else {
	$data = array($this->alias => array($one => $two));
}

I was trying to save some data which had some fields that wasn’t going to be saved into the database, these were things like credit card numbers which I would process in beforeSave etc. Since the code above will disable your save attempts if the first key in your data array is not a field in the model schema.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">