0

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

<?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
 }

 }
?>

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

4

1 回答 1

2

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

// controller
function myFunction(){
    // codes here
    $myvariable = "anything";
    $this->Model->myModelFunction($myvariable);
}

// model
function myModelFunction($myVariable) {
    // $myVariable will have the value of "anything"
}
于 2012-08-20T18:35:35.690 回答