【问题标题】:How to call a model from view如何从视图中调用模型
【发布时间】:2014-03-04 06:28:02
【问题描述】:

您好,我正在尝试从视图中调用模型,但出现错误。

A PHP Error was encountered Severity: Notice Message: Undefined property: 
CI_Loader::$Stopsmap_Model Filename: stopsmap/index.php Line Number: 59

我在视图中尝试了下面的代码。但是没有运气

foreach ($routes as $route_details):
//calling model

//$trip_max_min_sequence_details = $this->load->Stopsmap_Model->fullTripdetails($trips->trip_id); i tried this also but same error
//$this->load->model('Stopsmap_Model');
//$data = $this->stopsmap_model->fullTripdetails($route_details->route_id);  i tried this also but same error
$this->load->model('fixedtransit_model/Stopsmap_Model');
$get_route_related_trips = $this->Stopsmap_Model->getRoutetrips($route_details->route_id);
  if(count($get_route_related_trips)>=1):
    echo '1 stop';
  endif;
endforeach;

提前感谢您的帮助

【问题讨论】:

  • 为什么不从加载视图并将变量传递给视图的控制器调用它?
  • !!为什么要在你的视图中调用模型..
  • 实际上有一个行程名称列表。其中会有停靠点。我需要显示至少有一个停靠点的行程名称列表,所以我正在检查 foreach 循环。你可以查看代码
  • 你做错了,Model in View,完成你的控制器中的一切。
  • 我如何在控制器中动态传递行程 ID。我需要检查每个根...在控制器中使用相同的模型它工作正常...但为什么它不能在视图中工作。 ..

标签: php codeigniter view model


【解决方案1】:

在你的控制器中,你可以有这样的:

function routes(){
  //call model
  $this->load->model('fixedtransit_model/Stopsmap_Model');

  $routes = $this->topsmap_model->get_routes();

  foreach ($routes as $route_details){

   $get_route_related_trips[] = $this->Stopsmap_Model->getRoutetrips($route_details->route_id);

  }

  $data['related_routes'] = $get_route_related_trips;

  //load view and pass data
  $this->load->view('myview' , $data);

}

在你看来:

<?php foreach($related_routes){

//do something

}
?>

【讨论】:

  • 一个问题我的问题现在解决了..但是如果我们能够在视图中调用模型代码行会更少..我们可以在视图中做到这一点
【解决方案2】:

在我这边 我在应用程序/模型目录中创建了一个名为 accountmodel 的模型 并在视图中加载(最好在控制器中加载模型):-

$this->load->model('AccountModel','Account');
//AccountModel (model class name)
//Account (object) you can further use this model using object

更多信息:- http://ellislab.com/codeigniter/user-guide/general/models.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 2016-09-27
    相关资源
    最近更新 更多