php 在codeigniter中从视图访问模型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2719876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:28:55  来源:igfitidea点击:

access model from view in codeigniter?

phpcodeigniter

提问by sonill

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

谁能告诉我如何从 codeigniter 的视图中访问模型?

回答by ravisoni

Load a model on the controller

在控制器上加载模型

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

Assign this model to a var like this

将此模型分配给这样的 var

$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 对象来调用模型方法

$model_obj->some_method()

Hope this helps ...

希望这可以帮助 ...

回答by Sarfraz

See the thread:

见线程:

View Calling a Model

查看调用模型

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

通过你为什么需要访问的方式modelview,你可以将模型数据从发送到视图controller过这是通常的和更好的方法。

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

需要注意的是,将您的处理逻辑放在视图之外,您应该使用它controller

回答by bschaeffer

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

CodeIgniter 的$this->load->model()返回完全没有。看看吧: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:

下一个示例将为您提供致命错误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, $modelis not an object.

该函数是否存在并不重要,$model它不是一个对象。

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?

PS:你接受绝对错误的唯一答案是什么?

回答by Pritam Chaudhari

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

回答by newComer

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 itselfby 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 :

在旧版本的 codeigniter 中,以下代码用于工作:

$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

但是在 CI 3.1.9 上测试时,它会抛出以下错误 消息:未定义的属性: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

注意:我在模板视图(侧边栏、菜单...等)中使用此技术,该技术并未在我的应用程序中的任何地方使用,如果您想从应用程序的任何位置访问模型,请考虑通过将其添加到自动加载来全局加载此模型application/config/autoload.php 中的数组

回答by Jatinder Assi

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.

由于 $model 不是对象,因此您可以使用“::”范围解析运算符来调用模型“表”,该运算符可以在没有任何对象实例的情况下调用类本身的函数。

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

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

注意:您还需要在模型“表”中将函数“some_funct”设为静态。

回答by richardhell

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.

嘿。您可以使用与在其控制器上访问相同的模式从视图访问模型。请记住,视图访问导入其控制器的模型。

回答by James Cat

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

在最初的 UML 中,我似乎为 MVC 架构,视图调用模型中的方法..

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

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.

..但在 PHP 应用程序的实践中,因为没有持久性来跟踪请求之间对象的状态变化(或至少不是有效的),我发现最好将所有模型方法调用保留在控制器中并尽可能将结果传递给查看.

回答by pmoksuz

You can access basicly a method from view in codeingiter.

您基本上可以从 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.

我的函数返回一个数组。