XF 2.0 How to save null (empty) to a already saved record.

ludak

Active member
I have the schemaManager defining the columns as this

Code:
 $table->addColumn('result', 'enum')->values(['1', 'X', '2'])->nullable();

When I originally save the form everything is fine if I do not save with any values in there.

But once I set the value in the row, I cannot go back and unset it to be null again.

I am getting this.

XF\Db\Exception: MySQL query error [1265]: Data truncated for column 'result' at row 1 in src\XF\Db\AbstractStatement.php at line 212

How can I unset this property prior to saving the edit record to database so that it saves as null again.
 
The data truncated error shows you, that you have a data loss upon saving, because you're trying to save something into the field that doesn't belong there. My wild guess would be, that you're not saving null (the NULL value) but 'null' (a string) into the field.
 
I tried as setting just pure null but then I am getting this exception.

InvalidArgumentException: Attempted to convert NULL to string/binary [result]

Basically this is the code.

I get the record existing (which already has result set as 1,x or 2 (those are enum allowed values)

Code:
$game = $this->assertGameExists([$g['game_id'],$g['round_id']]);
                if($result == ''){
                    $game->set('result', null);
                }
 
I added 'nullable' => true in the entity and that worked.

Code:
'result' => ['type' => self::STR, 'required' => false, 'nullable' => true]];

Its just that I just tried this nullable for heck of it, since I did not even know there was a such thing.

Is there a list of things we can pass or change on the structure of the columns somewhere listed. I feel like there are lots of things not really great documented, and in order to understand something you can either search existing xenforo code (or if you have some other addons to look how other people overcome some issues, or ask here)

Many times i ask question here but not really getting fast and responsive answer :(
 
I added 'nullable' => true in the entity and that worked.

Code:
'result' => ['type' => self::STR, 'required' => false, 'nullable' => true]];

Its just that I just tried this nullable for heck of it, since I did not even know there was a such thing.

Is there a list of things we can pass or change on the structure of the columns somewhere listed. I feel like there are lots of things not really great documented, and in order to understand something you can either search existing xenforo code (or if you have some other addons to look how other people overcome some issues, or ask here)

Many times i ask question here but not really getting fast and responsive answer :(
I believe the main entity class has a list of possible options.
 
Yeah, I think I saw it somewhere in code and tried to use it :) Was not part of the structure of the columns, but it still worked.
 
Back
Top Bottom