Welcome Guest, Not a member yet? Register   Sign In
forge->addColumn
#1

Hi All,

CI 4.6.1.

I am trying to add a new column to an existing table. I am not having any luck.


PHP Code:
        $val                  = array();
        $obj['email_private'] = $this->getEmailField();
        $val                  $obj['email_private'] + ['after' => 'email_public'];
        $this->forge->addColumn('entry_requests'$val); 


getEmailFields looks like


PHP Code:
    protected function getEmailField()
    {
        $str = ['type' => 'VARCHAR''constraint' => '128''DEFAULT' => ' ''null' => 'false',];
        return $str;
    // end of method 

The error I am getting is an exception inside forge->addField. It seems that for some reason, it doesn't like the VARCHAR part of the definition. It throws a "Field information is required for that operation." exception, from line 387.

The variable $val contains:
  • type = "VARCHAR"
  • constraint = "128"
  • DEFAULT = " "
  • null = "false"
  • after = "email_public"

I don't see what I have done wrong and cannot find the error on the internet to track it down further.

Any help is much appreciated.
Reply
#2

I'm not understanding the majority of what your code does, but here is what it looks like in the simplest form to do what I think you're trying to achieve (try it and report back):
PHP Code:
$this->forge->addColumn('entry_requests', [ //'entry_requests' is the table name
 
'email_private' => [ // the column name needs to be the key of the passed array
 
'type' => 'VARCHAR',
 
'constraint' => 128// 128 should be an int type (check the docs)
 
'default' => ''// keys are case-sensitive (check the docs); empty string as opposed to space
 
'null' => false// false should be boolean type (check the docs)
 
'after' => 'email_public'// the column after which to insert new 'email_private' column
 
],
]); 
Reply
#3

@grimpirate, thanks for your suggestions. This led me down the right path. There was a logical error in my code.


PHP Code:
        $val = [
            'email' => [
                  'name'      => 'email_public',
                  'type'      => 'VARCHAR',
                  'constraint' => '128',
                  'DEFAULT'    => ' ',
                  'null'      => 'false',
            ],
        ]; 

Note the:  'email' => [

My code was not generating the array inside the array. It was at the top level only, the 'email' level was missing. This is why it was not working. It was making this:

PHP Code:
        $val = [
             'name'      => 'email_public',
             'type'      => 'VARCHAR',
             'constraint' => '128',
             'DEFAULT'    => ' ',
             'null'      => 'false',
        ];
  

I have updated my structure to generate the correct format and all is well.

I am now fully working. How I missed that yesterday is a mystery.

Thanks for your help, it is appreciated.

C
Reply




Theme © iAndrew 2016 - Forum software by © MyBB