DataWriter Default Value

tyteen4a03

Well-known member
I can't for the love of $DEITY figure out what is going on, so I'm just going to leave it here.

Here is my code: http://github.com/tyteen4a03/3ps_cmfu.

My users are getting
Zend_Db_Statement_Mysqli_Exception: Mysqli statement execute error : Field '3ps_cmfu_options' doesn't have a default value - library/Zend/Db/Statement/Mysqli.php:214. However, this should not happen as I have set a default value in the DataWriter.

What is wrong?
 
I pulled my hair out over this once.

Turns out I'd forgotten that, under a very rare circumstance, I was setting something directly using a MySQL query instead of using the DataWriter.

Maybe that is something you have overlooked. And if so, the solution would be to update that code to use the DataWriter only.

If it's not that... and actually, even if it is that... you should also set a default value in your column definition.
 
I pulled my hair out over this once.

Turns out I'd forgotten that, under a very rare circumstance, I was setting something directly using a MySQL query instead of using the DataWriter.

Maybe that is something you have overlooked. And if so, the solution would be to update that code to use the DataWriter only.

If it's not that... and actually, even if it is that... you should also set a default value in your column definition.
My addon does not set direct queries.

So basically XenForo cannot be trusted?
 
I wouldn't say it cannot be trusted, but the fact is, it's good practice to set a default value on database columns.

That being said, I can't say I've ever experienced this outside of my own error. So you may want to keep looking for a solution.

I'll take a peek at your current code.
 
I got it when try to code some Addon. Simply fixed:
Code:
ALTER TABLE `xf_user` ADD `3ps_cmfu_options` BLOB NOT NULL
Change to:
Code:
ALTER TABLE `xf_user` ADD `3ps_cmfu_options` BLOB NOT NULL DEFAULT \'\'
 
I got it when try to code some Addon. Simply fixed:
Code:
ALTER TABLE `xf_user` ADD `3ps_cmfu_options` BLOB NOT NULL
Change to:
Code:
ALTER TABLE `xf_user` ADD `3ps_cmfu_options` BLOB NOT NULL DEFAULT \'\'
That is equal to adding a default value to the field.

@Chris Deeming Noted. I'll just add a default value. *sigh*
 
Actually... can you have a default value on BLOB or TEXT fields? I don't think you can.
 
XenForo sets defaults for blobs in a couple of places. I don't have ide open at the moment but I'll find an example in a little while.
 
Are you referring to in the datawriter or in mysql?

library/XenForo/DataWriter/DiscussionMessage.php line 230
Code:
'like_users'             => array('type' => self::TYPE_SERIALIZED, 'default' => 'a:0:{}'),
 
Are you referring to in the datawriter or in mysql?

library/XenForo/DataWriter/DiscussionMessage.php line 230
Code:
'like_users'             => array('type' => self::TYPE_SERIALIZED, 'default' => 'a:0:{}'),
MySQL. I have already set the DataWriter default value of which MySQL ignores.

@Chris Deeming I wonder if Mike will know more...
 
If you're adding columns to existing tables, you must add a default value. Why? It's not for your code, it's not for the data writer.

It's for when people disable your add-on (or if someone does happen to insert into the DB without a data writer).
 
  • Like
Reactions: Dan
I wholeheartedly agree, @Mike.

It's a massive oversight not to.

But @tyteen4a03 has code in his DataWriter to set a default value, but for some reason it is being ignored:

https://github.com/tyteen4a03/3ps_c...tudio/CustomMarkupForUser/DataWriter/User.php

Looks ok to me:

PHP:
<?php
/*
* Custom Username Markup For User v1.0.0 written by tyteen4a03@3.studIo.
* This software is licensed under the BSD 2-Clause modified License.
* See the LICENSE file within the package for details.
*/

class ThreePointStudio_CustomMarkupForUser_DataWriter_User extends XFCP_ThreePointStudio_CustomMarkupForUser_DataWriter_User {
    protected function _getFields() {
        $fields = parent::_getFields();
        $fields['xf_user']['3ps_cmfu_options'] = array('type' => self::TYPE_SERIALIZED, 'default' => 'a:2:{s:8:"username";a:0:{}s:9:"usertitle";a:0:{}}');
        return $fields;
    }
}
 
Yeah but that code won't be run if the add-on is disabled. I haven't seen a backtrace to indicate where the error is being hit and any comments about whether the add-on is disabled, but that's the only cause really. (Unless of course the DW extension isn't running correctly for some reason, but you still have the add-on disabled issue as well. ;))
 
BLOBs can not have a default value, and as a disabled add-on cannot pass a default value to the XF User Data Writer (or any Data Writer) then you need to create your own table to store the object. This is how it should be done, and there will be no disabled add-on issues.
 
BLOBs/text fields can't have an actual default value, though you can allow NULL and that will be the implicit default.
 
Yeah but that code won't be run if the add-on is disabled. I haven't seen a backtrace to indicate where the error is being hit and any comments about whether the add-on is disabled, but that's the only cause really. (Unless of course the DW extension isn't running correctly for some reason, but you still have the add-on disabled issue as well. ;))
That would explain the issue. Thanks!
 
Top Bottom