access model from view in codeigniter?

2019-01-27 15:32发布

问题:

can anyone tell me how do i access model from view in codeigniter?

回答1:

Load a model on the controller

$this->load->model('yourmodel');

Assign this model to a var like this

$data['model_obj'] = $this->yourmodel; 

and assign this data array to your view template

Use $model_obj object on the view template for calling model methods

$model_obj->some_method()

Hope this helps ...



回答2:

See the thread:

View Calling a Model

By the way why do you need to access the model from the view, you can send the model data to the view from the controller too which is the usual and better approach.

As a good note, keep your processing logic out of the view, you should use controller instead.



回答3:

CodeIgniter's $this->load->model() returns absolutely nothing. Look at it: system/libraries/Loader.php.

This will output absolutely nothing:

$model = $this->load->model('table');

print_r($model);

And this next example will give you the fatal error Call to a member function some_func() on a non-object:

$model = $this->load->model('table');

$model->some_func();

It doesn't matter whether that function even exists, $model is not an object.

The thing to do is have a method in your model that returns data, then call that function and pass the results to your view file:

$this->load->model('table');
$data = $this->table->some_func();
$this->load->view('view', $data);

PS: How is the only answer you've accepted the absolute wrong thing to do?



回答4:

You can use following code:

   $ci =&get_instance();
   $ci->load->model(your model);
   $ci->(your model)->(your function);     
   Note: You have to call your model in your controller.Its working fine


回答5:

Since $model is not an object, you can make a call to the model "table" using "::" scope resolution operator, which can call the function of the class itself without any object instance.

$this->load->model('table'); 
table::some_funct();

Note: you also need to make the function "some_funct" static inside your model "table".



回答6:

Hey. You can access from view to models the same mode as you access on its controller. Remember that the view access to models that import its controller.



回答7:

in the original UML I've seem for MVC architecture, view calls methods in model..

http://www.as3dp.com/wp-content/uploads/2010/02/mvc_pope_krasner.png

..but in practice with PHP apps, because there is no persistence to track state changes in objects between requests (or at least not efficiently), I find it better to keep all model method calls in controller and pass the result to view if possible.



回答8:

In cases when you want to access a model function from within a shared view , you don't have to load the needed model in every controller that will call that view. you can load the model inside the view itself by using the following code :

   $ci =&get_instance();
   $ci->load->model(model_name);
   $ci->model_name->function_name(); 

in older versions of codeigniter the following code used to work :

$this->load->model('model_name'); 
model_name::function();   

but when tested on CI 3.1.9 it throw the following error Message: Undefined property: CI_Loader::$model_name_model

Note: I use this technique in template views (sidebar, menus ...etc) which is not used everywhere in my application , if you want to access a model from anywhere in your application considre loading this model globally by adding it to the autoload array in application/config/autoload.php



回答9:

You can access basicly a method from view in codeingiter.

public function index()
{

    $this->load->model('persons');
    $data['mydata'] = $this->persons->getAllSessionData();
    $this->load->view('test_page', $data);
}

in view

print_r ($mydata);

my function returned an array.