簡體   English   中英

使用控制器中的變量在CakePHP中的模型中使用

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

我想在我的控制器中有一個可以在我的模型中訪問的變量。 例如,

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

 }
?>

現在,我的代碼是否可行,我的意思是可以從我的控制器訪問變量然后在我的模型上使用它來檢查或其他什么? 謝謝。

是的,將它作為arg傳遞給您的模型方法。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM