How to add multiple models to a JView?

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
danielperaza
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Thu Apr 24, 2008 8:01 pm

How to add multiple models to a JView?

Post by danielperaza » Sun Aug 17, 2008 12:07 am

Hi. I need to build a view that needs data from different tables in the Database. Right now, I have the methods I need to retrieve these data by using several models. The question is if it's possible to add the models I need, without previously instantiate them, to my view using setModel(). I have been trying to do this, but JApplication is unable to find the models.

I know that a reasonable solution for this would be to build a new model and name it as my view, so the MVC Pattern works, but this seems to force me to rewrite code unnecessarily.

I leave an example to be more illustrative:

Let's say that we're managing a database of vehicles, owners, accessories, location of the vehicles and images of the owners and the vehicles. I'm trying to build a view which details the data of the vehicles, this implies to retrieve their images, their owners, their locations and their accessories. All of these relationships are 1:N except by the locations (A vehicle can only be at one site at a time) and the owners (a vehicle can only have one owner).

And there's another situation: what if I need to retrieve the data from the accessories but I want to show some descriptive info about the vehicle they belongs to (an image for example)?.

What would be the best way to implement this?.
Last edited by danielperaza on Tue Aug 19, 2008 6:16 pm, edited 2 times in total.

User avatar
dam-man
Joomla! Exemplar
Joomla! Exemplar
Posts: 7961
Joined: Fri Sep 09, 2005 2:13 pm
Location: The Netherlands
Contact:

Re: How to add multiple models to a JView?

Post by dam-man » Tue Aug 19, 2008 6:04 am

You can write 1 query to receive data from more than 1 table.
Take a look at my component, this is a car manager who does this also.
Click on the link below my avatar
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards

User avatar
spiderglobe
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 151
Joined: Mon Apr 03, 2006 1:45 pm
Location: Netherlands

Re: How to add multiple models to a JView?

Post by spiderglobe » Tue Aug 19, 2008 6:53 am

You can add multiple models to a view.

For example you can add multiple models, for example within the controller:

Code: Select all

$model = $this->getModel ( 'person' );
$view  = $this->getView  ( 'person '  );
$view->setModel( $model, true );  // true is for the default model;

$company = $this->getModel ( 'company'' );
$company->_name = 'model_company';
$view->setModel( $company );
Within the view:

Code: Select all

$person = $this->get('person' );
$company = $this->get('company, 'model_company');
This is a much nicer solution then using the different queries within one model. Suppose you have to change to company object you will have to change this in only one model if you are using the VMC in the correct way.

A much nicer approach would be that you can pass parameters within the model (current the Joomla VMC framework doesn't support this, see my other post: http://forum.joomla.org/viewtopic.php?f=231&t=312585

It would be nice that you can retrieve the company profile of the user for example by:

Code: Select all

$person = $this->get('person' );
$company = $this->get('company, 'model_company', $person->company_id );
regards,

Richard

danielperaza
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Thu Apr 24, 2008 8:01 pm

Re: How to add multiple models to a JView?

Post by danielperaza » Tue Aug 19, 2008 6:29 pm

That seems to be exactly what I tried before, but for some reason I don't know Joomla! is unable to find the required models. I'll take a deeper look to my code, but it might be possible that this would be just a naming problem.

Right now I'm following the convention XModelY / XViewZ, where X is the name of my component, Y the name of the model and Z the name of the view. So far, I have seen the MVC pattern working in Joomla! in this way: a XModelY is attached to a XViewY through a JController (say XController). What I'm trying to do is to attach a XModelZ to a XViewY, using the same methods you suggested me in the last post, but this is where the problem seems to arise.

My models and view are in the backend, I think that it wouldn't be necessary to tell Joomla! where my models are, it should already knows about it.

Where do you think I should start looking for bugs?.

User avatar
spiderglobe
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 151
Joined: Mon Apr 03, 2006 1:45 pm
Location: Netherlands

Re: How to add multiple models to a JView?

Post by spiderglobe » Wed Aug 20, 2008 7:22 am

What kind of development environment are you using? Using a debugger (for example xdebug) with an IDE tool can help a lot.

Could be indeed a naming issues. My experience with the VMC framework of Joomla is that the naming convention is 'strange'. You can not influence / control the naming conventions in regards of the models, views and controllers. Thereby you get 'strange' view / models names.

Try it with a debugger and start debugging with the controller function and check the parameters in the debugger...

For example checkout:http://www.phpdevelopmentstudio.com

Good luck!

danielperaza
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Thu Apr 24, 2008 8:01 pm

Re: How to add multiple models to a JView?

Post by danielperaza » Thu Aug 21, 2008 6:23 pm

Well I appreciate a lot the help I received. I'm using JDump as debugging tool. I have noticed that when trying to set the models from within the controller, once my view is open, they just don't exist in the registered models of my view.

I'm using JController::getView(), JController::getModel() and then JView::setModel() to try to achieve my goal.

secteur
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 146
Joined: Tue Nov 08, 2005 9:32 am
Location: Malaysia

Re: How to add multiple models to a JView?

Post by secteur » Sun Aug 24, 2008 6:58 am

Hi,

add the models to the view first ( in the controller ) then use them in your view.
In the controller:

Code: Select all

// Create the view object. I'm inside a component called com_exams. The view is in views/student.html.php, Class: ExamsViewStudent
$view =& $this->getView( 'student', 'html' );
				
// Exams Marks model - this one is inside the same component. The class is ExamsModelMarks. The true parameter makes it the default model
$view->setModel( $this->getModel( 'marks', 'ExamsModel' ), true );
				
// Add the student model from the students component - Different component so add the path to search models. Class: StudentsModelStudent
$this->addModelPath( JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_students' . DS . 'models' );
$view->setModel( $this->getModel( 'student', 'StudentsModel' ) );

// Now display the view. Make sure you dont have a parent::display() anymore
$view->display();
In the view.html.php you can now access both models. Could be more, I've been needed up to 5 sometimes.

Code: Select all

// For example: Calls getMarks from the model ExamsModelMarks because it is the default model
$marks =& $this->get( 'marks' );

// Calls getData from the model StudentsModelStudent (specified by the second parameter)
$studentData =& $this->get( 'data', 'student' );

// You might need to call a method from the model which takes parameters, in this case:
// see spiderglobe's comment above about not passing parameters directly to $this->get( 'Something', 'student', $paramsYouNeed ) . The code below is currently the only way
// Note that this only works if you have attached the model to the view in the controller, it does not create the model
$model =& $this->getModel( 'student' ); 
$model->getSomething( $student->name, $marks );
Hope this helps

danielperaza
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Thu Apr 24, 2008 8:01 pm

Re: How to add multiple models to a JView?

Post by danielperaza » Sun Aug 24, 2008 4:46 pm

Code: Select all

$this->addModelPath( JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_students' . DS . 'models' );
Is it really needed to do this from within a back-end controller?, I thought that Joomla! Framework would do that for me instead.

secteur
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 146
Joined: Tue Nov 08, 2005 9:32 am
Location: Malaysia

Re: How to add multiple models to a JView?

Post by secteur » Mon Aug 25, 2008 1:59 am

Hi,

yes it's needed if you are not in the same component as the model you require.
By default the getModel only searches in the same component models folder.

Code: Select all

// This is called from the controller inside a component called com_exams (not the same as the model I need)
// Model class: StudentgroupsModelStudentgroups
var_dump( $this->getModel( 'studentgroups', 'StudentgroupsModel' ) ); // Outputs bool(false)

// Add the path where my model is found
$this->addModelPath( JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_studentgroups' . DS . 'models' );

var_dump( $this->getModel( 'studentgroups', 'StudentgroupsModel' ) ); // Outputs the model object

soup a loignon y crouton
Joomla! Apprentice
Joomla! Apprentice
Posts: 40
Joined: Tue Aug 21, 2007 12:38 am

Re: How to add multiple models to a JView?

Post by soup a loignon y crouton » Mon Aug 25, 2008 3:26 am

a follow-up question on this

how would you save the data from the view if for example you hae data in a form and some data is from one model , and other fields from another model?


do you call in the controller one model at a time and do the store function?

i can t seem to be able to bind the row .

for example :

table 1 primary key id is "id"
linked to table 2 on the field "userid " and the primary key id

thus from what i understand the bind will be done on the primary key

i can t seem to be able to bind the data from the 2nd model to the second table aparently it is looking to bind to the primary key id but the relation between model 1 and model to is between id and userid

not sure what i am doing wrong
note to self : OMG FH UY and IYF

tampe125
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Fri Sep 11, 2009 10:20 am

Re: How to add multiple models to a JView?

Post by tampe125 » Wed Mar 03, 2010 9:25 pm

ok, little bumb up.

i need to add a model to the view, but i don't know how to do this.

can you help me step by step?
i need to add the model to the controller and then to the view, but how?

thanks in advance

chamilsanjeewa
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Fri Dec 11, 2009 9:17 am

Re: How to add multiple models to a JView?

Post by chamilsanjeewa » Tue Mar 23, 2010 12:18 pm

I directly include the model to view.html.php
I call the function

<?php
/**
* @version $Id$
* @package clienti
* @subpackage _ECR_SUBPACKAGE_
* @author EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
* @author chamil {@link http://www.easy-joomla.org}
* @author Created on 11-Nov-2009
*/

//-- No direct access
defined('_JEXEC') or die('=;)');

jimport( 'joomla.application.component.view');
require_once(JPATH_SITE.DS.'components'.DS.'com_clienti'.DS.'models'.DS.'clienti.php');
/**
* HTML View class for the clienti Component
*
* @package clienti
*/

class clientiViewactivity extends JView
{
function display($tpl = null)
{

$alltask=ClientiModelClienti::getAllTask();
$this->assignRef('alltask',$alltask);

parent::display($tpl);
}
}

secteur
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 146
Joined: Tue Nov 08, 2005 9:32 am
Location: Malaysia

Re: How to add multiple models to a JView?

Post by secteur » Tue May 04, 2010 12:22 pm

@tampe125

See posts above:
In the controller you load the model

Code: Select all

$view = $this->getView( 'nameofview', 'typeofview(html,raw,etc)');
# Note that next line works only if nameofcomponent is the current component, otherwise you need to use $this->addModelPath(... as seen in previous posts. But the next line will not return any error even if the model you call doesnt exist.
$model= $this->getModel('nameofmodel', 'nameofcomponentModel');
# Then add to the view - this is where the error will be thrown (see comment below):
$view->setModel( $model );
# Or $view->setModel( $model, true ) if this is your default model
Let me know if it works for you. If you get a rather cryptic error in JView, something with attempt to assign property on a non-object, it probably means that your model does not exist or that it is located in a different component. You can check that by doing a var_dump( $model ) see if it shows NULL.

chamilsanjeewa's answer surely works although I don't think it's the "proper" Joomla way.

Mat

bolgi
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sun Jun 20, 2010 2:08 pm

Re: How to add multiple models to a JView?

Post by bolgi » Thu Jul 08, 2010 8:54 am

SOLUTION:

$model = $this->getModel ( 'person' );
$view = $this->getView ( 'person ' , 'html' );
$view->setModel( $model, false ); // false since we are adding extra models..

--

the 'html' is the dealbreaker. I had to dig into the source for a while this morning, if you don't include the type of the view (html) joomla looks for view.php instead of view.html.php.

Doesn't make sense from our perspective but..

cheers.

dreamadi
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Sep 06, 2012 8:59 am

Re: How to add multiple models to a JView?

Post by dreamadi » Thu Sep 06, 2012 9:19 am

Hey guys,

Sorry for the begginer's question:

where do I need to put the lines inside the controler here:
For example you can add multiple models, for example within the controller:

Code:
$model = $this->getModel ( 'person' );
$view = $this->getView ( 'person ' );
$view->setModel( $model, true ); // true is for the default model;

$company = $this->getModel ( 'company'' );
$company->_name = 'model_company';
$view->setModel( $company );
is it in the construct method before/after the parent::__construct(); line?

Thanks a lot,

Adi


Locked

Return to “Joomla! 1.5 Coding”