Trying to create a Column Type of ENUM for my plugin

I am trying to create an ENUM column with the values Cash or Tournament


Gdn::structure()
	->table('Discussion')
	->column('TestGameType', 'ENUM('Cash','Tournament')', true)
	->set(false,false)

It throws an error on the word Cash when enabling the plugin how would I create an enum field?

Comments

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator
    edited February 25

    IIRC you just have to pass in the array of values. But you can look into the dashboard structure.php file and the Gender field of the user table


    MrCaspan
  • NM figured it out

    Gdn::structure()
    	->table('Discussion')
    	->column('TestGameType', array('Cash','Tournament'), true)
    	->set(false,false)
    


  • hehe we posted at the same time :) Thanks it is a passed array!

    R_J
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    By the way : if you add info to the discussion table that doesn't need to be included in the where part of a sql query, you should consider using the Attributes column for it.


  • whats an attribute column ?? Now i am all curious


    BTW have a look at your plugin http://pf.main.nu you can do a year and month view... day view coming but its as easy as just showing all discussion on that date

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Vanilla uses a column called "Attributes" in several tables, most interesting tables are User, Discussion and Comment. That column is used to store "misc" information in an array. The Gdn_Model class (filename is class.model.php) has some helper methods, from which only the saveToSerializedColumn() method might be of interest.

    Here is a snippet showing how to handle that column:

       public function pluginController_abracadabra_create($sender, $args) {
           $discussionModel = GDN::getContainer()->get(DiscussionModel::class);
           $discussionID = 22;
    
           $discussion = $discussionModel->getID($discussionID);
           decho($discussion->Name, 'Discussion Title');
           decho($discussion->Attributes, 'Discussion Attributes (before)');
           $discussion = null;
    
           $discussionModel->saveToSerializedColumn(
               'Attributes',
               $discussionID,
               'Foo',
               'Bar'
           );
           $discussionModel->saveToSerializedColumn(
               'Attributes',
               $discussionID,
               'MarxBrothers',
               ['Groucho', 'Chicco', 'Harpo']
           );
    
           $discussion = $discussionModel->getID($discussionID);
           $attributes = $discussion->Attributes;
           decho($discussion->Attributes, 'Discussion Attributes (after)');
           decho(count($discussion->Attributes), 'Attributes count');
           decho($discussion->Attributes['Foo'], 'Foo');
           decho(count($discussion->Attributes['MarxBrothers']), 'Number of Marx Brothers');
           $discussion = null;
    
           $discussionModel->saveToSerializedColumn(
               'Attributes',
               $discussionID,
               'MarxBrothers'
           );
    
           $discussion = $discussionModel->getID($discussionID);
           decho($discussion->Attributes, 'saveToSerializedColumn cannot delete a key');
    
           $attributes = $discussion->Attributes;
           unset($attributes['MarxBrothers']);
           $discussionModel->setField($discussionID, 'Attributes', $attributes);
    
           $discussion = $discussionModel->getID($discussionID);
           decho($discussion->Attributes, 'Discussion Attributes (final)');
        }
    

    (Create yourself a test plugin where you attach that method or attach it to any other plugin you ar working on and afterwards, logged in as the admin, open yourforum.com/plugin/abracadabra)

    Here are two insightful discussions which at least note that column shortly:

    https://open.vanillaforums.com/discussion/comment/232157/#Comment_232157

    https://open.vanillaforums.com/discussion/comment/207196#Comment_207196

    As you can see, adding more and more columns to the above mentioned tables isn't the best practice. If you can avoid it by storing some "meta information" in the Attributes column, you should definitly do that.


Sign In or Register to comment.