Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Trying to create db structure using db object (Gdn::Structure()) but "varchar" type is not working

Howdy!

Here is my code:

public function Setup()
{
$Structure = Gdn::Structure();
$Structure->Table('TestPlugin')
->Column('Number', 'int(11)', FALSE, 'primary')
->Column('Email', 'varchar', '200')
->Set(TRUE, FALSE);
}


This code gives me this error.

but if I use "TEXT" instead of varchar, everything is working. But I wanna go with varchar and according to this doc I think I'm sure there are no any syntax errors in my code right?
If you guys can help me to figure out this issue, that will be a great help to me and I really appreciate it :smile:

Best Answers

  • R_JR_J Ex-Fanboy Munich Admin
    Answer ✓

    Try $DataSet = Gdn::SQL()->Select('Users')->From($PluginTable)->Get()->ResultArray();
    I guess that willgive you the result you are searching for.

Answers

  • Vanilla core 2.0.18.11

  • @Shadowdare
    Thanks and it worked :smiley:
    But, the new documentation also have the same instructions in this link: http://docs.vanillaforums.com/developers/framework/database/

    where did you find that stuff? will you give me the exact location?

  • R_JR_J Ex-Fanboy Munich Admin

    You can get a lot of information simply by looking at Vanillas source code: https://github.com/vanilla/vanilla/blob/2.0/library/database/class.databasestructure.php#146-160

  • peregrineperegrine MVP
    edited May 2014

    same as r_j pointed out line 95 but delving further... shows

       protected function _CreateColumn($Name, $Type, $Null, $Default, $KeyType) {
              $Length = '';
              $Precision = '';
    
              // Check to see if the type starts with a 'u' for unsigned.
              if(is_string($Type) && strncasecmp($Type, 'u', 1) == 0) {
                 $Type = substr($Type, 1);
                 $Unsigned = TRUE;
              } else {
                 $Unsigned = FALSE;
              }
    
              // Check for a length in the type.
              if(is_string($Type) && preg_match('/(\w+)\s*\(\s*(\d+)\s*(?:,\s*(\d+)\s*)?\)/', $Type, $Matches)) {
                 $Type = $Matches[1];
                 $Length = $Matches[2];
    

    from the preg match you can see the Type length must be in parenthesis in the Type field.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Guys

    I've been trying this for the whole day and I am really tired of finding out about how this DB Class works. there is no any correct documentation about using the DB Object and it was really pain full to me. Please some one tell me how to run a select query using the Database Object and get those results to array.

    This is all I found, and this also gives me some useless output :(
    $DataSet = Gdn::SQL()->Select('Users')->From($PluginTable)->Get()
    print_r ($DataSet);

    Its show me:

    object(Gdn_DataSet)[80]
    public 'Connection' =>
    object(PDO)[30]
    private '_Cursor' => int -1
    protected '_DatasetType' => string 'object' (length=6)
    protected '_EOF' => boolean false
    private '_PDOStatement' =>
    object(PDOStatement)[44]
    public 'queryString' => string 'select Domain
    from GDN_BanDisposableMails BanDisposableMails' (length=60)
    protected '_Result' => null


    I just need the results :( guys I really need help.

  • R_JR_J Ex-Fanboy Munich Admin
    Answer ✓

    Try $DataSet = Gdn::SQL()->Select('Users')->From($PluginTable)->Get()->ResultArray();
    I guess that willgive you the result you are searching for.

  • R_JR_J Ex-Fanboy Munich Admin

    By the way: it might tiring to work through the code, but once you have the correct class, it's really worth looking at the functions in there. Most of the function names are speaking for themselves and so you don't have to study the source

  • @R_J oh finally that worked and you saved me <3
    Thanks x1000 times.
    hmmm yeah but where is that Gdn::SQL class located? (.php file?)
    https://github.com/vanilla/vanilla/blob/master/library/database/class.sqldriver.php
    this one?

  • R_JR_J Ex-Fanboy Munich Admin

    Best is to use the file on your hard drive but it's easier to reference to GitHub from here (but then it is more save to include the right branch: https://github.com/vanilla/vanilla/blob/2.1/library/database/class.sqldriver.php)

  • businessdadbusinessdad Stealth contributor MVP

    @Shadowdare said:
    The Column method's parameter list has changed.

    public function Column(
        $Name,
        $Type,
        $Length = '',
        $Null = FALSE,
        $Default = NULL,
        $KeyType = FALSE,
        $AutoIncrement = FALSE
    )
    

    Good to know, even if it was a bad idea to change the method, breaking backward compatibility...

Sign In or Register to comment.