问题
I am developing a joomla 2.5 component where I need to pass data from controller to model. The controller is receiving data from url. I find that controller is getting the value properly. Now I need to move that value to model from controller. From different post I have found a snippet of code for controller like below.
$datevalue = JRequest::getVar('day',$day); //receiving value from view
$item = JRequest::setVar('day',$datevalue); //setting variable
$model =& $this->getModel('WeeklyProgram'); //assign model
$model->setState('dayVar', $item); // assign value for model
The problem is that I don't know how to receive this value 'dayVar' from model. Can anybody help me on this issue? Thanks.
回答1:
Use following things
In Modal
class CommunityModelCevent extends JCCModel
{
var $membersCount   = null;
    function getMembersCount($value) {
    $this->membersCount = $value // set your value here 15 
    // Now you can access this variable into model
    }
}
In controller
$ceventModel    = CFactory::getModel( 'cevent' );       
$membersCount   = $ceventModel->getMembersCount(15);
回答2:
You can do like this . First you make get and set function in the model.Second load the model in the controller and simply pass the values to setter function.Example as follows:
updateratings.php---this is my model
class RatingManagerModelUpdateRatings extends JModelLegacy
    {
     public $data;
    public function get_data(){
    $data=$this->data;
    return $data;
     }
     public function set_data($data){
          $this->data=$data;
     }
    }
Controller.php class RatingManagerController extends JControllerLegacy {
public function save_ratings(){
 $tips = JRequest::getVar('tips'); //get data from front end form
 $model = $this->getModel('UpdateRatings'); //load UpdateRatings model
 $model->set_data($tips); //update setter function of model
 $res=$model->get_data(); // retrieve getter function
  //print_r($res);
} 
 }
来源:https://stackoverflow.com/questions/15872865/passing-data-from-controller-to-model-in-joomla-2-5