-3

Possible Duplicate:
Codeigniter Message: Undefined property: stdClass

location: application/core/student_model.php

class Student_model extends CI_Model
{
    private $id;

    public function __construct() {
        parent::__construct();
    }

    public function setId($id){
        $this->id = $id;
    }

    public function getId() {
        return $this->id;
    }
}

location: application/controllers/test.php

class Test extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Student_model');
    }

    public function index()
    {
        $student = $this->student_model->setId('1234567');
        echo $student->getId();
    }
}

I am getting the following message/error.

Message: Undefined property: Test::$student_model
Filename: controllers/test.php
Line Number: 13

This line is where I am calling the method setId.

Can anyone see what I am doing wrong?

Community
  • 1
  • 1
Danny Cullen
  • 1,586
  • 4
  • 27
  • 43

3 Answers3

3

Try replacing

$this->student_model->setId('1234567');

with

$this->Student_model->setId('1234567');

Your class is capitalized, so the property should also be capitalized afaik.

Suresh Kamrushi
  • 13,699
  • 12
  • 70
  • 84
g00glen00b
  • 34,293
  • 11
  • 80
  • 106
1

Try

public function __construct()
{
    parent::__construct();
    $this->load->model('Student_model', 's_model');
}

public function index()
{
    $student = $this->s_model->setId('1234567');
    echo $student->getId();
}
Alexander Cogneau
  • 1,216
  • 4
  • 11
  • 25
0

what you are doing wrong is :

you are assigning $student to $this->student_model->setId('1234567');
which is a function or 'setter' that does not return anything; and you won't be able to use $student->getId();

what I would do is that

class Test extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('student_model', 'student');
    }

    public function index()
    {
        $this->student->setId('1234567');
        echo $this->student->getId();
    }
}
Nerdroid
  • 11,584
  • 4
  • 52
  • 62