简体   繁体   中英

Use a variable from the controller to be used in the model in CakePHP

I wanted to have a variable in my controller that is accessible in my model. For instance,

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

and in my model,

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

 }
?>

Now, is my code possible, I mean is it possible accessing a variable from my controller then use it on my model for checking or something else? Thanks.

Yes, pass it as an arg to your model method.

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

// model
function myModelFunction($myVariable) {
    // $myVariable will have the value of "anything"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM