• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

DataWriter Default Value

tyteen4a03

Well-known member
#1
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?
 

Chris D

XenForo developer
Staff member
#2
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.
 

tyteen4a03

Well-known member
#3
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?
 

Chris D

XenForo developer
Staff member
#4
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.
 

truonglv

Formerly Nobita.Kun
#5
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 \'\'
 

Daniel Hood

Well-known member
#8
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.
 

Daniel Hood

Well-known member
#10
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:{}'),
 

Mike

XenForo developer
Staff member
#13
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).
 

Chris D

XenForo developer
Staff member
#14
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;
    }
}
 

Mike

XenForo developer
Staff member
#15
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. ;))
 

Lawrence

Well-known member
#16
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.
 

tyteen4a03

Well-known member
#18
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!