Sending and receiving data from controller to view

1,322 views
Skip to first unread message

foysal foysal

unread,
Mar 12, 2013, 4:07:12 AM3/12/13
to joomla-de...@googlegroups.com
I have a function in controller like below. I would like to send $data_for_edit variable to view and receive that variable in view. Can anyone say how can do that ??



    public function edit()
    {       
        $id = JRequest::getVar('id');
        $model  = $this->getModel('mouse');           
        $data_for_edit = $model->getaData($id);   
        $viewName= JRequest::getVar('view','edit');
        $view = $this->getView($viewName,"html"); 
        $view->display(); 
    }



Thanks

Foysal

hoochicken

unread,
Mar 12, 2013, 4:43:37 AM3/12/13
to joomla-de...@googlegroups.com
Hi,
Have had the same problem:-D
Maybe this helps,
https://groups.google.com/forum/#!topic/joomla-dev-general/T2rF32zwI8Q

Greetings Mareike

Herman Peeren

unread,
Mar 12, 2013, 4:45:42 AM3/12/13
to joomla-de...@googlegroups.com
Sorry, I've read your question and know I'm not answering it. But the idea is that the data of the model is rendered by the view, so the view is to ask the model for those data, without the controller being in between. In that way any controller could fit together that model and view. The controller sets the state of a model and tells a view which model to use.

Allon Moritz

unread,
Mar 12, 2013, 4:47:41 AM3/12/13
to joomla-de...@googlegroups.com
I would use setState() on the model to set the state and in the view getState() on the model




Foysal

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Herman Peeren

unread,
Mar 12, 2013, 5:31:58 AM3/12/13
to joomla-de...@googlegroups.com
I often used a "state"-variable with an explicit name, not hided in $state. It is a little bit more work, but also immediately clear what is happening. In the model I then have a property:

protected $something_id;

with a setter for that variable:

    public function setSomething_id($id)
    {
        $this->something_id = (int) $id;
    }

and a getter for the something-object:

    public function getSomething()
    {
        // Use $this->something_id to retrieve a something-object
        // return $something
    }


In the controller  I would then get something like:

    public function edit()
    {      
        $input = JFactory::getApplication()->input;

        // Get the model

        $model  = $this->getModel('mouse');

        // Set the state of the model
        $id = $input->getInt('id');          
           $model->setSomething_id($id);

        // Get the view  
        $viewName= $input->get('view','edit');

        $view = $this->getView($viewName,"html");

        // Set the right model to the view
        $view->setModel($model);

        $view->display();
    }

Now I can get('Something') in the view.

Using the $state-variable of the model is more generic, so you can make a generic view in which you always retrieve the state. Sometimes I prefer a little bit more verbosity: if it clarifies the purpose of what I'm doing.

Herman Peeren

unread,
Mar 12, 2013, 5:44:02 AM3/12/13
to joomla-de...@googlegroups.com
$view->setModel($model) should have a second parameter "true" to set this model as the default one:

$view->setModel($model, true);

foysal foysal

unread,
Mar 12, 2013, 5:45:04 AM3/12/13
to joomla-de...@googlegroups.com
Thanks Herman for your reply. That means, model send data to view and view collect data from model. Am I right ?? If so, then what is the role of controller ?? Then I have to know how data send from one view to another view (for my component).
 
Thanks

Foysal

--

Herman Peeren

unread,
Mar 12, 2013, 5:56:34 AM3/12/13
to joomla-de...@googlegroups.com
On Tuesday, 12 March 2013 10:45:04 UTC+1, nowphp wrote:
Thanks Herman for your reply. That means, model send data to view and view collect data from model. Am I right ??

Yes: the view gets data from the model.
But no: the model doesn't send anything by itself to the view. The model doesn't "know" which view (or other class) is asking.
 

 On Tuesday, 12 March 2013 10:45:04 UTC+1, nowphp wrote:
If so, then what is the role of controller ?? Then I have to know how data send from one view to another view (for my component).

The controller only controls the trafic:
  • get the input and depending on that input a model is set, the model's state is set, a view is set and the model is tied to the view
  • a view-method is called
  • and eventually some redirect

Herman Peeren

unread,
Mar 12, 2013, 6:00:06 AM3/12/13
to joomla-de...@googlegroups.com
On Tuesday, 12 March 2013 10:45:04 UTC+1, nowphp wrote:
Then I have to know how data send from one view to another view (for my component).

Better: set the same model to the other view and let the other view get its data from there.

Herman Peeren

unread,
Mar 12, 2013, 6:23:53 AM3/12/13
to joomla-de...@googlegroups.com
On Tuesday, 12 March 2013 10:45:04 UTC+1, nowphp wrote:
Then I have to know how data send from one view to another view (for my component).

Or do you have a form-view and a result-view? Then the form-view renders the form -> the user fills in the form and submits it -> the controller reads the input, sets model, model-state, view and ties the model to the view. Then displays the view.

A step in between can be: to save the data to the database. Joomla makes that a two-step proces: first it saves the data (controller calls some save-method in the method), but instead of immediately going to the view it then redirects the whole thing and in the second phase the controller does its normal job of reading the input, seting model, model-state, view, tieing the model to the view, and displaying the view.I don't like that redirect-thing, but it is how Joomla works; it separates saving and deleting from displaying.

Herman Peeren

unread,
Mar 12, 2013, 6:25:53 AM3/12/13
to joomla-de...@googlegroups.com
On Tuesday, 12 March 2013 11:23:53 UTC+1, Herman Peeren wrote:
controller calls some save-method in the method

must of course be:  "controller calls some save-method in the model"
Reply all
Reply to author
Forward
0 new messages