MVC: Calling a model from a view

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

MVC: Calling a model from a view

Post by uweD » Thu Feb 21, 2008 12:14 am

Hi,

What I trying to do: I created a login view which the user access by clicking on a link on my navigation bar. The link I created using the joomla administration site (see attached image). Joomla creates a link which points directly to the view and NOT to the controller. The below code now tries to oull the data model 'language' into the view - without any success.

Code: Select all

class xyzViewLogin extends JView { 
		
		function display($tpl = null)	{
		
                $model_language =  &$this->getModel('language');
		$model_login = &$this->getModel('login');
				 
		$this->assignRef('label',$model_language->labels);
		$this->assignRef('userName', $model_login->userName);			
		parent::display($tpl);
		}
Just to test it I created an external link to my component using the 'option' and 'task' parameter.

Code: Select all

index.php?option=com_ehive&task=login
With that approach the controller does the job (see code below):

Code: Select all

function login() {
	
			$session 	= JFactory::getSession();
			$document 	=& JFactory::getDocument(); 
	
			$viewName = JRequest::getVar('view','login');
			$model_language = &$this->getModel('language');
			
			$viewType = $document->getType(); 
			
			$view = &$this->getView($viewName, $viewType); 
		
				if (!JError::isError( $model_language )) { 
						$view->setModel( $model_language, true ); 
			
				}
							
				$view->setLayout('login'); 
			$view->display(); 
			
		}
This approach does the trick, the model is being pulled in/assigned to the view. As I obviously want to be able to use the nifty backend of joomla properly, I need to know how to assign a model to a view in the view itself.

Thanks,

Uwe

What am I missing?

Thanks a lot,
Uwe
Attachments
CreateMenuItem.png
CreateMenuItem.png (9.34 KiB) Viewed 347 times
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: MVC: Calling a model from a view

Post by uweD » Thu Feb 21, 2008 2:37 am

...hmmm. Well, I got it going, but it simply feels wrong :-(

I went back to the controller and added the model that I needed in my login view to the 'display'(!) function. Even though that does not seem to me to be logical it does the trick. argh - lots and lots of confusion at the end of a long day... Can anyone help?


Thanks,
Uwe
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: MVC: Calling a model from a view

Post by uweD » Thu Feb 21, 2008 3:29 am

At the end of (my) day I found a (temporary) solution which keeps me going until someone explains to me how to do it better: Instead of messing around with the default function I know got my controller executing the right function. I did this with the following peace of code in my main component file:

Instead of that

Code: Select all

		$controller = new eHiveController();
		$controller->execute(JRequest::getVar('task');
		$controller->redirect();

I use know

Code: Select all

		$controller = new eHiveController();
		$controller->execute(JRequest::getVar('task',JRequest::getVar('view')));
		$controller->redirect();
When I don't have a task, I default the task parameter to the value of the view parameter.... The is at least a little bit simplier.

Cheers,

Uwe
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga


Post Reply