Welcome Guest, Not a member yet? Register   Sign In
dbforge not loading
#1

[eluser]Bryan Zera[/eluser]
In my model, I load the dbforge class with
Code:
$this->load->dbforge();
just like the user guide says to. Then I build a table like so:
Code:
$fields = array(
  $this->db_table_name => array(
    'type'=>'int'
  ),
  $key => array(
    'type'=>'int'
  )
);
$this->dbforge->add_field($fields);
But I get this error:
Code:
Fatal error: Call to a member function on a non-object in /var/www/web2/modele/system/application/models/modele.php on line 51
Where line 51 is the $this->dbforge->add_field() line.

Other forum postings suggest loading dbutil or making sure your driver is not the mysqli driver, but neither helped. Why won't dbforge load?

MySQL Version: 5.0.32
CodeIgniter Version: 1.6.2
PHP Version: 4.4.2 (don't yell)
#2

[eluser]AgentPhoenix[/eluser]
Have you tried moving the DB Forge stuff to a controller? I have an install script and I'm just doing all of the forge work right in the controller and it works beautifully.
#3

[eluser]Bryan Zera[/eluser]
I can't/won't put it in a controller. My model is the entry point for the DB and I want to create tables on the fly.
#4

[eluser]usmc[/eluser]
@Bryan Zera

Is your database loaded prior to loading dbforge?

Example (I know you don't have to load this way but it shows the database loading prior to dbforge)

Code:
class my_model extends Model {


    function my_model()
    {
        parent::Model();
        $this->load->database();
        $this->load->dbforge();
    }
}
#5

[eluser]Bryan Zera[/eluser]
database is autoloaded. I have no problem with other DB-related functions.
#6

[eluser]usmc[/eluser]
[quote author="Bryan Zera" date="1215554635"]In my model, I load the dbforge class with
Code:
$this->load->dbforge();
just like the user guide says to. Then I build a table like so:
Code:
$fields = array(
  $this->db_table_name => array(
    'type'=>'int'
  ),
  $key => array(
    'type'=>'int'
  )
);
$this->dbforge->add_field($fields);
But I get this error:
Code:
Fatal error: Call to a member function on a non-object in /var/www/web2/modele/system/application/models/modele.php on line 51
Where line 51 is the $this->dbforge->add_field() line.

Other forum postings suggest loading dbutil or making sure your driver is not the mysqli driver, but neither helped. Why won't dbforge load?

MySQL Version: 5.0.32
CodeIgniter Version: 1.6.2
PHP Version: 4.4.2 (don't yell)[/quote]

Show it all baby Smile

I need to see your model in order to try and help.
#7

[eluser]Bryan Zera[/eluser]
Modele.php: Lines 41-51

It's too big to post here and MIME'd too weird to attach to a PM or to this forum.
#8

[eluser]usmc[/eluser]
Your problem stems from running init in the constructor and init then attempts to run a dbforge method. dbforge doesn't exist yet!

Code:
function Modele()
        {
            parent::Model();
            if (get_class($this) != 'modele') $this->init();
        }
#9

[eluser]Bryan Zera[/eluser]
I relented, put it in the controller and what do you know it works.

Thanks all.
#10

[eluser]caseyamcl[/eluser]
I found a dirty little trick for loading dbforge from within a model Smile Just use $ci =& get_instance();

Not sure if this is on-par with CI techniques, but it worked for me in a pinch.

Code:
Class My_model extends Model()
{
  // ... some code ...

  function uses_dbforge()
  {
    $ci =& get_instance();

    $ci->load->dbforge();
    $ci->dbforge->create_table('some_table');
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB