Difference between revisions of "How do I store empty values as NULL in the database?"

From Joomla! Documentation

m (Hutchy68 moved page J2.5:Store empty values a NULL in database to J2.5:How do I store empty values as NULL in the database? without leaving a redirect: correcting title to make it a question)
m (added Category:Database using HotCat)
Line 19: Line 19:
[[Category:Tips and tricks]]
[[Category:Tips and tricks]]

Revision as of 05:43, 16 May 2013

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) {
	return parent::store(true);


protected function prepareTable($table)
	$defnull = array('array','of','columns','that','can','have','null','value');
	foreach ($defnull as $val)
		if (!strlen($table->$val))
			$table->$val = NULL;