0

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?

1
  • 1
    CI loads each Class from file with the same name.. even if you have 100 classes in 1 file it will load only the one with the name as your file name.. So you should make files for each model / controller etc you want to use. Commented Apr 1, 2013 at 10:43

4 Answers 4

1

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";
     }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the ref although I can not read anywhere explicitly that only one class per model file is allowed!
1

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

2 Comments

is that CI only or is that PHP general?
this is for CI only, not a php
0

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
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.