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

DataWriter Default Value

Discussion in 'XenForo Development Discussions' started by tyteen4a03, Sep 22, 2013.

  1. tyteen4a03

    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?
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  3. tyteen4a03

    tyteen4a03 Well-Known Member

    My addon does not set direct queries.

    So basically XenForo cannot be trusted?
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  5. Nobita.Kun

    Nobita.Kun Well-Known Member

    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 \'\'
     
  6. tyteen4a03

    tyteen4a03 Well-Known Member

    That is equal to adding a default value to the field.

    @Chris Deeming Noted. I'll just add a default value. *sigh*
     
  7. Chris D

    Chris D XenForo Developer Staff Member

    Actually... can you have a default value on BLOB or TEXT fields? I don't think you can.
     
  8. Daniel Hood

    Daniel Hood Well-Known Member

    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.
     
  9. tyteen4a03

    tyteen4a03 Well-Known Member

    I can't set default values other than an empty string to BLOBs.
     
  10. Daniel Hood

    Daniel Hood Well-Known Member

    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:{}'),
     
  11. tyteen4a03

    tyteen4a03 Well-Known Member

    MySQL. I have already set the DataWriter default value of which MySQL ignores.

    @Chris Deeming I wonder if Mike will know more...
     
  12. Chris D

    Chris D XenForo Developer Staff Member

    Of course, @Mike knows everything.

    I can't see why this wouldn't work.
     
  13. Mike

    Mike XenForo Developer Staff Member

    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).
     
    Dan likes this.
  14. Chris D

    Chris D XenForo Developer Staff Member

    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;
        }
    }
     
  15. Mike

    Mike XenForo Developer Staff Member

    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. ;))
     
    tyteen4a03, Nobita.Kun and Chris D like this.
  16. Lawrence

    Lawrence Well-Known Member

    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.
     
  17. Mike

    Mike XenForo Developer Staff Member

    BLOBs/text fields can't have an actual default value, though you can allow NULL and that will be the implicit default.
     
    Lawrence likes this.
  18. tyteen4a03

    tyteen4a03 Well-Known Member

    That would explain the issue. Thanks!
     

Share This Page