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.
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);
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);
}
}
Related
I passed parameter from view to controller via URL. Now I want to send it from controller to model so that I can use it to pick data from tables. Here is my code:
controller:
function view(){
if(isset($_GET['r'])) {
$rank = $_GET['r'];
}
$rank=$this->uri->segment($rank);
$this->load->model('names_rank');
$data=$this->names_rank->get_names($rank);
print_r($rank);
}
model:
function get_names($rank){
$this->db->select('u.*,v.*');
$this->db->from('unit_member u, Vyeo v');
$this->db->where('v.fno = u.fno');
$this->db->where('u.present = ""');
$this->db->where('v.rank', $rank);
$this->db->where('v.date_of_end="0000-00-00"');
$query = $this->db->get();
return $query->result_array();
}
this is the result:
A PHP Error was encountered Severity: Warning Message: Missing
argument 1 for Names_rank::get_names(), called in
C:\xampp\htdocs\unit\application\controllers\names.php on line 32 and
defined
This will work to send to model but your code isn't understandable for me, you re-declare the variable after setting it in the IF? are you trying to print_r() the output from the model?
I think you are trying to achieve this maybe?
function view() {
if(isset($_GET['r'])) {
$rank = $_GET['r'];
}else{
$rank = $this->uri->segment($rank);
}
$this->load->model('names_rank');
$data = $this->names_rank->get_names($rank);
print_r($data);
}
You can pass a Parameter to your model. First you have to call your model within your controller if you not enable it on autoload.
Your Model:
<?php
class AwesomeModel extends CI_Model
{
publif function do_work($param, $anotherParam)
{
//code here
}
}
Then your controller:
<?php
class AwesomeController extends CI_Controller
{
public function __construct()
{
/*
* load in constructor so not need to recall every time you want use it
* second parameter is model renaming (optional)
*/
$this->load->model('AwesomeModel', 'awe');
}
public function pass_data()
{
$this->awe->do_work($param1, $param2);
}
?>
Thats all.
Simple example
I would like to know if there is any possible way that $data could already exist without being set in that method, and if so how to set it.
public function index(){
if(isset($data)){
//how is this possible?
} else if(isset($this->data){
// set in parent::__construct
// ok i'm going to have to set $data in every method in every controller
$data = $this->data;
}
}
additional info
This is my specific problem,
I am using a framework with a controller class which is extended for every controller.
class ControllerBlog extends Controller {}
Every method in every controller perform a few almost identical tasks. Some of these tasks return data within the scope of the method called.
//e.g
public function index(){
$this->loadthis('blog');
$this->loadthat('blog');
$data = $this->get_this('blog');
...
...
$data['title'] = 'blog title';
use_data($data);
}
I would like to move these tasks to Controller class function __construct to limit the amount of code repeated.
<?php
class Controller {
public function __construct() {
//load this and that and return data;
$data = $this->load_and_return_all(get_class($this));
//class level
$this->data = $data;
}
}
is there a way to get the $data variable for use within the scope of the method without the need of adding any additional code to every method of every controller?
class ControllerBlog extends Controller {
public function index(){
//adding this to every method seems silly
$data = $this->data;
// i would like $data to be set in the construct;
}
}
I am currently trying to pass a variable from the controller to my model. From a previous view I was passed $id in a button.
<a class="btn btn-info" type="button" href="index.php?r=recipient/index&id=<?php echo $data->id; ?>">Manage</a>
From there, I have accessed $id inside my controller and used it. However, now I am creating a function in the model that needs to use that $id. I can't pass it in as a argument because it is the function beforeSave (which updates my database before I've saved a new entry)
This looks like this in the model Recipient.php
// before you save the function here are the things you should do
public function beforeSave()
{
if($this->isNewRecord)
{
// updates the list_id of the individual with the selected ListId
$this->list_id = Recipient::model()->getListId;
}
return parent::beforeSave();
}
Finally, I am trying to create the function getListId in the same model.
public function getListId()
{
// my code here
}
Is there a simple way to pass a variable from the controller to the model to use? Or, is there a way for a function in the model to access a variable passed to the controller?
Add a property to your model and set that property in your controller.
class Model
{
public $var;
... more code ...
}
actionIndex($idList)
{
$model = Model::model()->find(...);
$model->var = $idList;
... more code ...
}
I've tried many solutions that had the same questions like mine. But didn't found a working solution.
I have a controller:
event.php
And two views:
event.phtml
eventList.phtml
I use eventList to get data via ajax call so I want to populate both views with a variable named "eventlist" for example.
Normally I use this code for sending a variable to the view:
$this->view->eventList = $events;
But this variable is only available in event.phtml.
How can I make this available for eventlist.phtml? (without adding a second controller)
Edit:
I get this error now
Call to undefined method Page_Event::render()
Function:
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
$this->eventList = $this->_event;
$this->render();
$this->render('eventlist');
}
If I use $this->view->render('event.phtml') and eventlist.phtml it won't pass the data
I'm using zend version 1
You can pass variables to other views using render()
public function fooAction()
{
// Renders my/foo.phtml
$this->render();
// Renders my/bar.phtml
$this->render('bar');
}
Copy and paste this in your controller and rename your controller from event.php to EventController.php
class EventController extends Zend_Controller_Action
{
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
// You're calling the index.phtml here.
$this->eventList = $this->_event;
$this->render('event');
$this->render('eventlist');
}
}
To specify that only written #Daan
In your action:
$this->view->eventList= $events;
$this->render('eventList'); // for call eventList.phtml
In you View use : $this->eventList
You could render it within the view itself (eventList.phtml), rather than within the controller, using the same line of code you used above:
$this->render('event[.phtml]');
How do I pass properly the models data to the view from controller in joomla 3.1. On start I initialize one of my sub controllers method to gather data on item which should fill up my form layout. Is accessed with the following url ?option=com_unis&task=unis.edit&layout=edit&id=1 than my controllers method looks like
public function edit()
{
$input = JFactory::getApplication()->input;
$model = $this->getModel ( 'item');
$view = $this->getView('item', 'html');
$view->setModel($model);
$view->setLayout('edit');
// Display the view
$view->display();
return $this;
}
than if I try to access the model in my view is returning null
Found it! But maybe is not the best workaround
in the view I init my model like
$model = $this->getModel('mymodel');
$data = $model->my_method($args);
than associate to the layout with a public variable
$this->data = $data;
After all I found out the workaround. In the view I call my model as it follows
$model = $this->getModel('mymodel');
$data = $model->my_method($args);
than I created a public variable which holds the layout data
$this->data = $data;
The controller picks up the view to be used. The model functions can be called in the views/somefolder/view.html.php from there the assignes variables can be viewed in the template default file.
class MyPageViewMyPage extends JViewLegacy {
/**
* Display the Hello World view
*
* #param string $tpl The name of the template file to parse; automatically searches through the template paths.
*
* #return void
*/
public $data;
function display($tpl = null) {
// Assign data to the view
$this->msg = $this->get('Msg'); // call a method msg in the model defined
$model = $this->getModel(); // creating an object for the model
$this->data = $model->getFilter(1); // call a method defined in model by passing the arguments
// Check for errors.
if (count($errors = $this->get('Errors'))) {
JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');
return false;
}
// Display the view
parent::display($tpl);
}
}
in the default template file
echo $this->msg;
print_r($this->data);