Re: Get model from a view

1,620 views
Skip to first unread message

Viper

unread,
May 20, 2013, 2:33:05 PM5/20/13
to joomla-de...@googlegroups.com
[view]->get() can accept two parameters. First this is name of the called method and second this is model name(of course if you extend JViewLegacy). Look at the get() method in "libraries/legacy/view/legacy.php"

On Monday, May 20, 2013 3:56:41 PM UTC+3, lsri8088 wrote:
Hello,
I'm starting with Joomla (2.5) and want to create a component.
I have two tables: googleaccounts and imports
Each table is managed by a view, but the view ViewImports, I also need to use the model "googleaccounts".

The $this->getModel returns data only if I send parameters or if you use the model of the same view.
If you use another model, then I get this error:
Notice: Undefined index: googleaccounts in /Users/lsri8088/Sites/joomla/joomla/libraries/joomla/application/component/view.php on line 413

I created the component with an application that generates all files but only allows you to use a table. Then I created the same component, with a different table, and then I merged the two folders, files merging. Did I do something wrong, any configuration files?

Thanks in advance, and greetings.

Viper

unread,
May 20, 2013, 2:36:06 PM5/20/13
to joomla-de...@googlegroups.com
Sorry this is for Joomla 3+.
For J2.5 the same method in "libraries/joomla/application/component/view.php"

Adam Rifat

unread,
May 21, 2013, 4:00:40 AM5/21/13
to joomla-de...@googlegroups.com
Not sure if I understand exactly what you are trying to do, but if you are trying to use a different model in your view.html.php then you will need to do something like...

// Set the default model MyModelProperty
$this->setModel(JModelLegacy::getInstance('Property', 'MyModel'));

That is, the first argument of setModel accepts a model instance. You can then change the model that the view is using by doing

$this->getModel('Property');

All calls like:

$this->get('Form')

are now called in the Property model and not the default view model.

So you can, in theory, have any number of models available in one view. If you just call getModel without first calling setModel then you get the error you mentioned in your post.

lsri8088

unread,
May 21, 2013, 3:21:36 PM5/21/13
to joomla-de...@googlegroups.com
I run in my view.html.php this:
$this->setModel(JModelLegacy::getInstance('googleaccounts'), false);

And I get this error:
Fatal error: Call to a member function getName() on a non-object in /Users/lsri8088/Sites/joomla/joomla/libraries/joomla/application/component/view.php on line 481 

because JModelLegacy::getInstance('googleaccount') is returning false :( 

What are the first two parameters of JModelLegacy::getInstance function?

I understand that the first parameter is the name of the model ... ¿ok?


The name of the model class is LsricalendarModelgoogleaccounts.

I searched on Google, but I did not clarify, and API documentation does not say much more.


Thanks!

lsri8088

unread,
May 22, 2013, 1:36:50 PM5/22/13
to joomla-de...@googlegroups.com
Finally!
Thank you very much, with this I have resolved.

Thanks :)

El martes, 21 de mayo de 2013 22:58:00 UTC+2, Aleksander Linkov escribió:
The first parameter is "The model type to instantiate", googleaccounts in your case.
Second parameter is "Prefix for the model class name", LsricalendarModel.

For this to work I believe your class name should be LsricalendarModelGoogleaccounts, and this class should be in file googleaccounts.php.
Then you can do something like this:
$this->setModel(JModelLegacy::getInstance('Googleaccounts', 'LsricalendarModel'));
Reply all
Reply to author
Forward
0 new messages