Welcome Guest, Not a member yet? Register   Sign In
How i can use model in other model?
#1

[eluser]Pitrsonek[/eluser]
Hi, please how i can use model in other model? I cant finde it in user guide.

Ther you can see, i want use model comments but it doesnt work.

THX

Code:
<?php
class Posts extends Model {
  
  function Posts() {
    parent::Model();
  } // Posts

  
    function getPostOrderId($id) {
        $result = dibi::fetchAll('SELECT * FROM posts JOIN users ON posts.user_id=users.user_id where POST_ID =%i',$id);
        
$this->load->model('comments');
        var_dump($comments);
        $comments = $this->comments->getCommentsForPost($id);
        return $result;        
    }    

} // class Posts
?>
#2

[eluser]Phil Sturgeon[/eluser]
Sadly models are not loaded in the model class, so using $this-> doesnt give you any access to them. You need to grab the instance using get_instance() then use THAT new object pointer to access your models.

Code:
<?php
class Posts extends Model {
  
  function Posts() {
    parent::Model();
  } // Posts

  
    function getPostOrderId($id) {
        $CI =& get_instance();
        $CI->load->model('comments');
        
        $comments = $CI->comments->getCommentsForPost($id);
        return $result;        
    }    

} // class Posts
?>
#3

[eluser]Craig A Rodway[/eluser]
Load the CI instance into a variable, and load/access other models through it, like this:

Code:
<?php
class Posts extends Model {

  var $CI;

  function Posts() {
    parent::Model();
    // Assign CI instance to variable so it is available to all functions in this model
    $this->CI =& get_instance();
  }


  function getPostOrderId($id) {
    $result = dibi::fetchAll('SELECT * FROM posts JOIN users ON posts.user_id=users.user_id where POST_ID =%i',$id);
        
    // Load comments model from CI instance
    $this->CI->load->model('comments');
    // Call function in the comments model using the CI instance
    $comments = $this->CI->comments->getCommentsForPost($id);

    var_dump($comments);
    return $result;        
    }

}
?>
#4

[eluser]Phil Sturgeon[/eluser]
Sometimes PHP4 has trouble with the instance when loaded that way. Its better to assign it each time you need it, as creating a pointer takes barely any time at all, so there is no real benefit to loading it upon contruction.

If you are doing it that way, dont forge to set the property. :p

Code:
var $CI;
#5

[eluser]Craig A Rodway[/eluser]
Ah yes, you're right.. it seems PHP4 requires them to be declared first.

I still find it easier to make it available to the whole model in the constructor - I could use it once or I could use it a dozen times and it keeps each function 2 lines shorter Wink
#6

[eluser]jurosik[/eluser]
Craig, could you write code of what you say, please?

"to make it available to the whole model in the constructor"

It will help me a lot! I'm tryin' to load more models in one model, but it doesn't work.

Thank you very much
#7

[eluser]Phil Sturgeon[/eluser]
He already did :p

Code:
<?php
class Posts extends Model {

  var $CI;

  function Posts() {
    parent::Model();
    // Assign CI instance to variable so it is available to all functions in this model
    $this->CI =& get_instance();
  }
#8

[eluser]jurosik[/eluser]
Maybe I should write more...

I had one big model with all functions, but now i need to make this model more comprendious, so i allocate my functions into tematic models. but functions have calls between models.

I need to load model once, and then just call them anywhere in model...

Code:
model1
   load model2 (model3, 4...)

function1() {

   model2->function2()

}

i have tried this:
Code:
class Data_model extends Model {

    function Data_model()
    {
        parent::Model();        
        $this->CI =& get_instance();
        $this->CI->load->model('main_model');
        $this->CI->load->model('file_model');
    }

...

$this->CI->main_model->getSomething($value);

But when i load file Data_model in Main_model (here is function 1 which i am calling from Data_model) it doesn't work
Code:
class Main_model extends Model {

    function Main_model()
    {
        parent::Model();
        $this->CI =& get_instance();
        $this->CI->load->model('data_model');
        $this->CI->load->model('file_model');
    }

    function getSomething($value)
    {
        return $value;
    }
}

mybe it's very complicated to explain...
#9

[eluser]Craig A Rodway[/eluser]
What errors are you getting and what isn't working?
#10

[eluser]jurosik[/eluser]
Browser won't open the page: "the server unexpectedly dropped the connection"

it happens after i load model in another model which i am calling function

In controller
Code:
class Jobs extends Controller {

    function Jobs()
    {
        parent::Controller();
        
        $this->load->model('main_model');
        $this->load->model('data_model');
    }
      
       function something()
       {
                $data = $this->data_model->getData('jobs');
       }

In Data_model
Code:
class Data_model extends Model {

    function Data_model()
    {
        parent::Model();
        $this->CI =& get_instance();
        $this->CI->load->model('main_model');
        $this->CI->load->model('file_model');
    }
        
        function getData($table)
        {
                $output =     $this->db->get($table);
        
                return $output;
        }

Main_model
Code:
class Main_model extends Model {

    function Main_model()
    {
        parent::Model();
        $this->CI =& get_instance();
        $this->CI->load->model('data_model');  // HERE is problem
        $this->CI->load->model('file_model');
    }




Theme © iAndrew 2016 - Forum software by © MyBB