Welcome Guest, Not a member yet? Register   Sign In
problem creating a model
#1

[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
#2

[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);
    }
...
#3

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

Quote:core/Model.php

to

Quote:models/elenco_model.php
#4

[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
#5

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

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

InsiteFX
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB