How do I store empty values as NULL in the database?

From Joomla! Documentation

Revision as of 11:23, 16 May 2013 by Jhattara (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

If you want to store empty values as NULLs into the database, you need to override JTable::store() and JModelAdmin::prepareTable(). Add following snippets of code into your component's admin/tables/nameoftable.php and admin/models/nameofeditview.php


public function store($updateNulls = true) {
	/* overload JTable::store() to enable updating values into NULL */
	return parent::store(true);


protected function prepareTable($table)
	/* define which columns can have NULL values */
	$defnull = array('array','of','columns','that','can','have','null','value');
	foreach ($defnull as $val)
		/* define the rules when the value is set NULL */
		if (!strlen($table->$val))
			$table->$val = NULL;