0

如何model在类中调用类函数controller?就像我$_POSTView文件中有数据一样。Mongo DB现在我想通过model类将该数据插入。我正在使用yiiframeworkMVC。

4

3 回答 3

0

嗯,您如何要求控制器顶部的模型?我在深入到 mvc 时遇到了问题,因为包含路径回到了原始调用的位置。检查您的包含路径

get_include_path()

&& 包含哪些文件

get_included_files()

于 2013-01-14T03:09:38.037 回答
0

使用 MVC 编程模式,您需要在构建控制器时初始化模型对象。这与您对视图所做的相同。MVC 的好处是您的 $_POST 将跟随您的函数链,您不必将数组传递给每个函数。

controller{
  public $view = null;
  public $model = null;

  function __construct(){
    $this->view = new View();
    $this->model = new Model();
  }

  function form_submit(){
    $this->model->insertDB_func();  // whatever your func is
  }
}
于 2013-01-12T18:10:42.600 回答
0

这是一个简单的例子

public function actionViewContact()
{
   $contact = new Contact; // you can also do Contact::model(); 
   $contact->scenario='add';
   if (isset($_POST['Contact'])) 
   {
      $contact->attributes = $_POST['Contact'];
      $contact->save(); // calling save function in contact model which is activerecord
   }
}
于 2013-01-12T18:37:25.727 回答