Welcome Guest, Not a member yet? Register   Sign In
object of model class
#1

[eluser]developer123[/eluser]
Hi All,

I am really new to codeigniter framework.
I am working on one project.
I am facing following error.

Fatal error: Class 'abc' not found in C:\wamp\www\system\application\controllers\abc_controller.php on line 13


Model
Code:
<?php    
class abc extends Model
{

    // Private Attributes
    private $id;
    private $name;
    private $species;
    private $weight;
    private $colour;

    // Constructor
    function __construct($id, $name, $species, $weight, $colour)
    {
          parent::Model();
        $this->id = $id;
        $this->name = $name;
        $this->species = $species;
        $this->weight = $weight;
        $this->colour = $colour;
    }

    // Public Getters
    public function get_id()
    {
        return $this->id;
    }

    function get_name()
    {
        return $this->name;
    }

    /**
     *
     * @param newVal
     */
    function set_name($newVal)
    {
        $this->name = $newVal;
    }

    function get_species()
    {
        return $this->species;
    }

    /**
     *
     * @param newVal
     */
    function set_species($newVal)
    {
        $this->species = $newVal;
    }

    function get_weight()
    {
        return $this->weight;
    }

    /**
     *
     * @param newVal
     */
    function set_weight($newVal)
    {
        $this->weight = $newVal;
    }

    function get_colour()
    {
        return $this->colour;
    }

    /**
     *
     * @param newVal
     */
    function set_colour($newVal)
    {
        $this->colour = $newVal;
    }

}
/* End of file */

controller
Code:
<?php
class abc_controller extends Controller
{
    private $xyz = array();

    /**
     * Constructor
     */
    function __construct()
    {
        parent::__construct();
        
        $xyz = new abc('1','name','6','1000','blue');
    }

    /**
     * Default view
     */
    public function index()
    {
        $data = array();
        $this->load->view('abc_index', $data);
    }
}
/* End of file */


could you please help me?
#2

[eluser]Julian Mesa[/eluser]
Hello !
You need load the model in the controller:

Code:
<?php
class abc_controller extends Controller
{
    private $xyz = array();

    /**
     * Constructor
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->model(abc);
        $xyz = $this->abc('1','name','6','1000','blue');
    }

    /**
     * Default view
     */
    public function index()
    {
        $data = array();
        $this->load->view('abc_index', $data);
    }
}
/* End of file */
#3

[eluser]developer123[/eluser]
Thanks
Now I've loaded model as well.
and I am getting following:

Quote:A PHP Error was encountered
Severity: Warning

Message: Missing argument 1 for abc::__construct(), called in C:\wamp\www\system\libraries\Loader.php on line 184 and defined

Filename: models/abc.php

Line Number: ..

A PHP Error was encountered
Severity: Warning

Message: Missing argument 2 for abc::__construct(), called in C:\wamp\www\system\libraries\Loader.php on line 184 and defined

Filename: models/abc.php

Line Number: ..

A PHP Error was encountered
Severity: Warning

Message: Missing argument 3 for abc::__construct(), called in C:\wamp\www\system\libraries\Loader.php on line 184 and defined

Filename: models/abc.php

Line Number: ..

A PHP Error was encountered
Severity: Warning

Message: Missing argument 4 for abc::__construct(), called in C:\wamp\www\system\libraries\Loader.php on line 184 and defined

Filename: models/abc.php

Line Number: ..

A PHP Error was encountered
Severity: Warning

Message: Missing argument 5 for abc::__construct(), called in C:\wamp\www\system\libraries\Loader.php on line 184 and defined

Filename: models/abc.php

Line Number: ..

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: id

Filename: models/abc.php

Line Number: ..

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: name

Filename: models/abc.php

Line Number: ..

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: species

Filename: models/abc.php

Line Number: ..

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: weight

Filename: models/abc.php

Line Number: ..

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: colour

Filename: models/abc.php

Line Number: ..

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: xyz

Filename: controllers/abc_controller.php

Line Number: ..
#4

[eluser]djenniex[/eluser]
Actually, your models constructor can not accept argument in CogeIgniter, therefore, your going to have to change your code to something like this:

Code:
class abc extends Model
{

    // Private Attributes
    private $id;
    private $name;
    private $species;
    private $weight;
    private $colour;

    // Constructor
    public function __construct()
    {
          parent::Model();
    }

    public function initialize($id, $name, $species, $weight, $colour)
    {
        $this->id = $id;
        $this->name = $name;
        $this->species = $species;
        $this->weight = $weight;
        $this->colour = $colour;
    }

    etc ...
}

The you can use the Loader to load the model, and initialize it in the controller, ie.
Code:
<?php
class abc_controller extends Controller
{
    private $xyz = array();

    /**
     * Constructor
     */
    function __construct()
    {
        parent::__construct();
        $this->load->model('abc');
        $this->abc->initalize('1', 'name', '6', '1000', 'blue');
    }

    /**
     * Default view
     */
    public function index()
    {
        $data = array();
        $this->load->view('abc_index', $data);
    }
}
#5

[eluser]developer123[/eluser]
Actually, I wanted to create objects of model class.
If I'll do like this then How I gonna create multiple objects of abc model class?
#6

[eluser]zee[/eluser]
When you load a model, you can specify the name of the object to be created as an optional 2nd parameter, and whether database connection is to be given or not as another optional 3rd parameter:

Code:
$this->load->Model('myfolder/mymodelclass', 'objectname', TRUE);

Here the first parameter is your abc class, in 2nd parameter you can give any name to the object thus created, and in the third parameter TRUE for granting a DB connection and FALSE otherwise.




Theme © iAndrew 2016 - Forum software by © MyBB