TMOTTM
TMOTTM

Reputation: 3381

CodeIgniter Hello World MVC with two models

My CI controller looks like this:

 // Controller.
 class Hello extends CI_Controller
 {
     public function one($name)
     {
         $this->load->model("hello_model");

         $profile = $this->hello_model->getProfile("Me");
         //$profile2 = $this->hello_model->otherAction();

         $this->load->view('header');

         $data = array("name" => $name);
         $data['profile'] = $profile;
         $this->load->view('one.html', $data);
     }
 }

and here is/are the model(s)

 class Hello_model extends CI_Model
 {
     public function getProfile($name)
     {
         return array("fullName" => "Martin", "Age" => 28);
     }
 }

 class Hello_model_2 extends CI_Model
 {
     public function otherAction()
     {
         echo "Data";
     }
 }

When I enable the $profile2 statement and visit the controller in the browser, I find this error message in Apache Error log:

[Mon Apr 01 ...] [error] [client 127.0.0.1]
PHP Fatal error: 
Call to undefined method Hello_model::otherAction() in 
/.../CodeIgniter_2.1.3/application/controllers/Hello.php on line x

where x is the line of the profile2 statement.

Can I not have two classes in a "model"-file? Btw, what are .php files called in CI-speak? Modules?

Upvotes: 0

Views: 355

Answers (4)

Azam Alvi
Azam Alvi

Reputation: 7055

If you want to create 2 models file Hello_model and Hello_model_2 then also load Hello_model_2 like this

class Hello extends CI_Controller
{
 public function one($name)
 {
     $this->load->model("hello_model");
     $this->load->model("hello_model_2");

     $profile = $this->hello_model->getProfile("Me");
     $profile2 = $this->hello_model_2->otherAction();

     $this->load->view('header');

     $data = array("name" => $name);
     $data['profile'] = $profile;
     $this->load->view('one.html', $data);
 }
}

And if you are using one model and two function then u should use this code into your model

class Hello_model extends CI_Model
{
   public function getProfile($name)
   {
       return array("fullName" => "Martin", "Age" => 28);
   }

   public function otherAction()
   {
      echo "Data";
   }
}

Hope you understand.

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

You can try with "EXTENDS" of one model into another like

class Hello_model extends CI_Model
{
   public function getProfile($name)
   {
      return array("fullName" => "Martin", "Age" => 28);
   }

   public function otherAction()
   {
      echo "Data";
   }
}

then create second model like Hello_model_2.php

class Hello_model_2 extends Hello_model
{ 
    //Here access those from Hello_model
}

Upvotes: 0

Dino Babu
Dino Babu

Reputation: 5809

you have to create two seprate class files

hello_model.php

 class Hello_model extends CI_Model
 {
     public function getProfile($name)
     {
         return array("fullName" => "Martin", "Age" => 28);
     }
 }

hello_model_2.php

 class Hello_model_2 extends CI_Model
 {
     public function otherAction()
     {
         echo "Data";
     }
 }

and call in controller

     $this->load->model("hello_model");
     $this->load->model("Hello_model_2");

     $profile = $this->hello_model->getProfile("Me");
     $profile2 = $this->hello_model_2->otherAction();

or

you can use multiple methods in your model

Upvotes: 1

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16086

Each file must have one model only.

http://ellislab.com/codeigniter/user-guide/general/models.html

Instead create another method in same class like-

class Hello_model extends CI_Model
 {
     public function getProfile($name)
     {
         return array("fullName" => "Martin", "Age" => 28);
     }

     public function otherAction()
     {
         echo "Data";
     }
 }

Upvotes: 1

Related Questions