Pass NULL value when saving form, instead of empty string

1,065 views
Skip to first unread message

Antanas Vipartas

unread,
Apr 30, 2012, 7:12:03 AM4/30/12
to joomla-de...@googlegroups.com
Hi, I developed custom component (Joomla 2.5), and in back-end I want to create a new record of "Game", but after saving it, database field (i.e. "Location") is filled with empty string instead of being NULL.

I have created a form in administrator/com_mycomponent/models/forms/game.xml, where all fields are listed. I tried to use attribute default="NULL", but it didn't help.

Should I override "save" method in controller, in order to achieve this? Thanks

Antanas Vipartas

unread,
Apr 30, 2012, 8:23:44 AM4/30/12
to joomla-de...@googlegroups.com
After reading some threads I have found a solution.

Lets say I have a component named "Basketball" and a model called "Game". I want Game's "location" (which is a DB column of String) every time be NULL. In order to achieve this you have to:
1. Override method prepareTable() in file administrator/components/com_basketball/models/game.php. There should be a class called BasketballModelGame which extends from JModelAdmin. Write method like this

    protected function prepareTable(&$table)
    {
        $table->location = NULL;
    }

2. But this is not enough as Viet Vu mentioned, since by default NULLS are not updated. So if you want Joomla to always update fields which values are NULL, edit file administrator/components/com_basketball/tables/game.php and override method store() like this:

    function store($updateNulls) {
        return parent::store(true);
    }

Alberto Massidda

unread,
Sep 18, 2014, 5:42:26 PM9/18/14
to joomla-de...@googlegroups.com
Antanas thank you for your solution, it has been very helpful to me because I had a similar problem.
I just changed the prepareTable method this way:

protected function prepareTable(&$table)
{
if(!$table-> location) $table-> location = NULL;
}

so that it is saved as NULL only when the field is empty.

Reply all
Reply to author
Forward
0 new messages