CodeIgniter Forums
problems with Database Forge class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: problems with Database Forge class (/showthread.php?tid=6261)



problems with Database Forge class - El Forum - 02-20-2008

[eluser]chejnik[/eluser]
Hello,
I have problems with Database Forge class.

I call this function in my model view called Creation_model
Code:
function create_DB()
    {
         $this->load->dbforge();
    if ($this->dbforge->create_database('my_db'))
    {
    echo 'Database created!';
    }

   }

There comes the error. In log file (level 2)

DEBUG - 2008-02-20 16:50:22 --> Model Class Initialized
DEBUG - 2008-02-20 16:50:22 --> Creation model started
DEBUG - 2008-02-20 16:50:22 --> Database Forge Class Initialized
ERROR - 2008-02-20 16:50:22 --> Severity: Notice --> Undefined property: Creation_model::$dbforge /var/www/apache2-default/codeIgniter/system/application/models/creation_model.php 332

Thanks for any help. With regards Ales


problems with Database Forge class - El Forum - 02-20-2008

[eluser]chejnik[/eluser]
Hello again,
I am suprised but it is solved now. To access Database Forge class one has to load Database Utility class instead.

Code:
function create_DB()
        {
        $this->load->dbutil();
    $dbs = $this->dbutil->list_databases();

    foreach($dbs as $db)
    {
        echo $db;
    }    
    
    if ($this->dbforge->create_database('my_db'))
        {
       echo 'Database created!';
       }
       }
Both functions works, one on dbutil, the second on dbforge class, interesting.


In log file
DEBUG - 2008-02-20 20:24:10 --> Creation model started
DEBUG - 2008-02-20 20:24:10 --> Database Forge Class Initialized
DEBUG - 2008-02-20 20:24:10 --> Database Utility Class Initialized
DEBUG - 2008-02-20 20:24:10 --> Create Database started

Is it kind of bug or it is common behaviour of Database Forge class?
Thanks Ales


problems with Database Forge class - El Forum - 02-23-2008

[eluser]chejnik[/eluser]
Yet another question.
I would like to create table using Forge class using mysql database driver

Code:
function create_Structure_User($tableName)
    {
         $this->load->dbutil(); // note - $this->load->dbforge(); doesnot work
         $fields = array(
                        'nickname' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                
              'password' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                'realName' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                'email' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                 'street' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                  'psc' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                   'town' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                    'country' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                 'timezone' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                   'phone' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                    'alternativePhone' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                     'msn' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                      'icq' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => '40'
                    ),
                    'alternativeEmail' => array(
                                                 'type' => 'VARCHAR',
                                                'constraint' => '40'
                    ),
                    'web' => array(
                                                 'type' => 'VARCHAR',
                                                'constraint' => '40'
                    ),
                    'notes' => array(
                                                 'type' => 'TEXT'
                                                   )
                );
    print_r($fields);
        $this->dbforge->add_field($fields);
        $this->dbforge->create_table($tableName, TRUE);
        }

The result of print_r
Array ( [nickname] => Array ( [type] => VARCHAR [constraint] => 40 ) [password] => Array ( [type] => VARCHAR [constraint] => 40 ) [realName] => Array ( [type] => VARCHAR [constraint] => 40 ) [email] => Array ( [type] => VARCHAR [constraint] => 40 ) [street] => Array ( [type] => VARCHAR [constraint] => 40 ) [psc] => Array ( [type] => VARCHAR [constraint] => 40 ) [town] => Array ( [type] => VARCHAR [constraint] => 40 ) [country] => Array ( [type] => VARCHAR [constraint] => 40 ) [timezone] => Array ( [type] => VARCHAR [constraint] => 40 ) [phone] => Array ( [type] => VARCHAR [constraint] => 40 ) [alternativePhone] => Array ( [type] => VARCHAR [constraint] => 40 ) [msn] => Array ( [type] => VARCHAR [constraint] => 40 ) [icq] => Array ( [type] => VARCHAR [constraint] => 40 ) [alternativeEmail] => Array ( [type] => VARCHAR [constraint] => 40 ) [web] => Array ( [type] => VARCHAR [constraint] => 40 ) [notes] => Array ( [type] => TEXT ) )


An Error Was Encountered
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' `nameRealProject` VARCHAR(100), `nicknameAdmin` VARCHAR(40), `passwordAdmin`' at line 2

CREATE TABLE IF NOT EXISTS Project ( Array, `nameRealProject` VARCHAR(100), `nicknameAdmin` VARCHAR(40), `passwordAdmin` VARCHAR(40), `passwordProject` VARCHAR(40), `emailAdmin` VARCHAR(40), PRIMARY KEY (`id`) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;


Please note the in sql statement at the beginning the word Array - I have no idea how to put it away. Seems to come from mysql.forge.php.


problems with Database Forge class - El Forum - 02-23-2008

[eluser]chejnik[/eluser]
Hi,
problems seems to be in Forge class itself

When I comment a lot of lines it works
Code:
// --------------------------------------------------------------------

    /**
     * Add Field
     *
     * @access    public
     * @param    string    collation
     * @return    void
     */
    function add_field($field = '')
    {
    /*    if ($field == '')
        {
            show_error('Field information is required.');
        }
        
        if (is_string($field))
        {
            if ($field == 'id')
            {
                $this->fields[] = array('id' => array(
                                        'type' => 'INT',
                                        'constraint' => 9,
                                        'auto_increment' => TRUE
                                        )
                                    );                                    
                $this->add_key('id', TRUE);
            }
            else
            {
                if (strpos($field, ' ') === FALSE)
                {
                    show_error('Field information is required for that operation.');
                }
                
                $this->fields[] = $field;
            }
        }
    */    
        if (is_array($field))
        {
            $this->fields = array_merge($this->fields, $field);
        }
        
    }

I leave only if (is_array($field)) condition.
Ales