CodeIgniter Forums
problem creating a model - 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: problem creating a model (/showthread.php?tid=40389)



problem creating a model - El Forum - 04-07-2011

[eluser]Oblium[/eluser]
I have this model:
Code:
class Elenco_model extends CI_Model {
    
    var $titolo = "";
    var $descrizione = "";
    
    function __construct()
    {
        parent::__construct();
    }
    
    function insert_element($t,$d){
        $dati = array( 'titolo' => $t, 'descrizione' => $d );
        
        $this->db->insert('elenco',$dati);
    }
}

saved in a file called elenco_model.php in model folder

I've also this controller:

Code:
class Blog extends CI_Controller {

    public function index(){
        
        $data['title'] = "Titolo del Blog";
        $data['elements'] = array("elemento1","elemento2","elemento3");
        $this->load->view('blogview',$data);
    }
    
    public function insertdata($titolo,$data){
        
        $this->load->model('Elenco_model');
        $this->Elenco_model->insert_element($titolo,$data);
    }
}

in a file called blog.php in the controller folder.

As i try to call path like "../blog/insertdata/titolo1/descrizione1/" it gives me this error:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Blog::$db

Filename: core/Model.php

Line Number: 50

Fatal error: Call to a member function insert() on a non-object in C:\xampp\htdocs\ign\application\models\elenco_model.php on line 15

What's wrong in my code?

Thanks


problem creating a model - El Forum - 04-07-2011

[eluser]toopay[/eluser]
this not the final solution, try to call your model diractly/statically, just check if this work..
Code:
...
    public function insertdata($titolo,$data)
    {    
        $this->load->model('Elenco_model');
        //$this->Elenco_model->insert_element($titolo,$data);
        Elenco_model::insert_element($titolo,$data);
    }
...



problem creating a model - El Forum - 04-07-2011

[eluser]Oblium[/eluser]
Nothing changed, it has just moved the error from

Quote:core/Model.php

to

Quote:models/elenco_model.php



problem creating a model - El Forum - 04-07-2011

[eluser]Oblium[/eluser]
I found my problem, i didn't connect with database ._.

I solved in this way:

Code:
class Blog extends CI_Controller {

    public function index(){
        
        $data['title'] = "Titolo del Blog";
        $data['elements'] = array("elemento1","elemento2","elemento3");
        $this->load->view('blogview',$data);
    }
    
    public function insertdata($titolo,$data){
        
        //$this->load->model('Elenco_model');
        $this->load->model('Elenco_model','',TRUE);
        $this->Elenco_model->insert_element($titolo,$data);
    }
}

Thank you, however Smile


problem creating a model - El Forum - 04-07-2011

[eluser]Unknown[/eluser]
In 50th line made some modification. use double quote and then try. i hope it works for your code.


problem creating a model - El Forum - 04-07-2011

[eluser]InsiteFX[/eluser]
Load the database!

InsiteFX


problem creating a model - El Forum - 04-07-2011

[eluser]Oblium[/eluser]
Yep, i noticed Smile ty