【问题标题】:Use a variable from the controller to be used in the model in CakePHP使用控制器中的变量在 CakePHP 中的模型中使用
【发布时间】:2012-08-21 02:35:35
【问题描述】:

我想在我的控制器中有一个可以在我的模型中访问的变量。例如,

<?php class MyController extends AppController{
   function myFunction(){
      // codes here
      myvariable = "anything";
   }
}
?>

在我的模型中,

<?php class myModel extends AppModel
 function myModelFunction(){
  // here i will use my variable to check on something.
 if(myVariable != 0){ // myVariable here is from the controller
   // do something here
 }

 }
?>

现在,我的代码是否可行,我的意思是是否可以从我的控制器访问一个变量,然后在我的模型上使用它进行检查或其他什么?谢谢。

【问题讨论】:

    标签: php cakephp model controller


    【解决方案1】:

    是的,将它作为 arg 传递给您的模型方法。

    // controller
    function myFunction(){
        // codes here
        $myvariable = "anything";
        $this->Model->myModelFunction($myvariable);
    }
    
    // model
    function myModelFunction($myVariable) {
        // $myVariable will have the value of "anything"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多