The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Mon Apr 30, 2012 3:49 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Jan 06, 2010 11:58 am
Posts: 8
Location: London
Hi,

I am trying to develop a component where the edit view will take data from more than one model. I have set up the controller to load the model as follows:

controller1.php
Code:
function edit($key = null, $urlVar = null)
   {
      $view = $this->getView('RegUser', 'html', 'MyComponentView');
      
      // Get the RegUser model and make it the default model
      $view->setModel($this->getModel('Reguser'), 'True');
      
      // Get the Mileage model and include it in the view
      $view->setModel($this->getModel('Mileage'));
      
      $status = parent::edit($key, $urlVar);
      
   }


This loads the models fine but when I call the parent::edit($key, $urlVar); when the view loads it forgets that I have loaded in the second model as there is only one model in the models list.

Any ideas why it forgets this?

Cheers,

Bogsey


Top
 Profile  
 
PostPosted: Mon Apr 30, 2012 4:11 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Oct 30, 2008 7:27 pm
Posts: 149
It's because JControllerForm::edit() (what's getting called when you used parent::edit()) gets only one model (when it calls JController::getModel()) which get's the model that has the same name as the view's name which I'm guessing is "Reguser" in your case.

You probably can't use parent::edit(). You could copy it's code (/libraries/joomla/application/component/controllerform.php) into your edit() function and then modify it as needed to work with both models (ie, having something like $models1 = $this->getModel('Reguser'); $models2 = $this->getModel('Mileage');).


Maybe someone else has a different solution.


Last edited by drewgg on Mon Apr 30, 2012 8:49 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Mon Apr 30, 2012 6:48 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Jan 06, 2010 11:58 am
Posts: 8
Location: London
I see. What I'm trying to achieve is to update more than one db table from the same edit item and was hoping this might be the way to go.

My other thought was to put the code into the relevant model to update a table that is not necessarily associated with that model, is this possible?


Top
 Profile  
 
PostPosted: Mon Apr 30, 2012 9:02 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Oct 30, 2008 7:27 pm
Posts: 149
Well, it sounds possible, but what you want to doesn't sound like something that Joomla was built to do specifically. You may have to study the framework classes for a bit and really pin down how far down the rabbit hole you need to go to achieve what you want. Maybe someone else has some better insight or suggestions; good luck!


Top
 Profile  
 
PostPosted: Tue May 01, 2012 8:24 am 
Joomla! Ace
Joomla! Ace

Joined: Sat Oct 01, 2011 7:06 pm
Posts: 1260
Extending the model is what you should do. That is the purpose of a model, nothing to do with Joomla. Plain OO MVC.


Top
 Profile  
 
PostPosted: Tue May 01, 2012 8:27 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Jan 06, 2010 11:58 am
Posts: 8
Location: London
Thanks for that, probably saved me a week or so of banging my head against a wall!!

Just to be sure that we're talking about the same thing, what I have currently is 3 tables in the db system, A, B and C. A and B contain the data and C is a link table associating A and B together, I'm trying to get the edit view to edit data from both A and B where they are linked by C.

Thanks for the replies.


Top
 Profile  
 
PostPosted: Tue May 01, 2012 9:22 am 
Joomla! Ace
Joomla! Ace

Joined: Sat Oct 01, 2011 7:06 pm
Posts: 1260
The model should allow for changes in A, B and C. Your application should allow for changes in A, B and C. You can combine A and C, B and C without problems. When combining A, B and C it becomes difficult to determine what was meant to change.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 



Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group