Return an array of errors from model using setError() ?

296 views
Skip to first unread message

Viper

unread,
Apr 20, 2015, 2:46:03 PM4/20/15
to joomla-de...@googlegroups.com
Hi!

I'm stack on the question in topic title.

A have a custom 'save' method in model. But in some situations I need to return more than one error. Something like this:

public function save($data) {
    $app
= JFactory::getApplication();
    $this
->setError('1');
    $this
->setError('2');
   
return false;
}

And when display messages in controller. But it return only the last message. I know that the JError is deprecated. Should I use enqueueMessage() instead of crutches?

PS! Or maybe exists an other way to fill the error property in the model and get it in the controller?

Walt Sorensen

unread,
Apr 20, 2015, 7:10:45 PM4/20/15
to joomla-de...@googlegroups.com
What version of Joomla are you working with?
setError and JError was deprecated in platform 12.1
According to the API you should be using "PHP Exception" now

https://api.joomla.org/cms-3/classes/JModelForm.html

Viper

unread,
Apr 21, 2015, 2:56:11 AM4/21/15
to joomla-de...@googlegroups.com
Joomla 3, but it doesn't matter. I see this code in Joomla every time in https://github.com/joomla/joomla-cms/blob/staging/administrator/components/com_content/models/article.php

And you said that I must to use Exceptions, but try/catch for this is not a solution, because I need to return error into controller.

Rob Clayburn

unread,
Apr 21, 2015, 5:19:07 AM4/21/15
to joomla-de...@googlegroups.com
There's a lot of old code in Joomla that should be updated, error handling is one of those areas. All that JError stuff was written before exceptions were really used in the PHP world.

As Walt says, your model should throw an exception, and then you should catch it in the controller and handle it there. It takes a bit of getting used to but it really does increase the readability of the code

Walt Sorensen

unread,
Apr 28, 2015, 8:38:14 PM4/28/15
to joomla-de...@googlegroups.com
You also still see stuff like JViewLegacy, JControllerLegacy, JModelItemLegacy, JModelFormLegacy in joomla 3 rather than the new the base abstract classes like JModelBase, JViewBase and JControllerBase

(use of the new base abstract classes requires some re-factoring to implement, here is an example component using the new MVC http://lendr.websparkinc.com/)

as Rob said, there is a lot of old code that should be updated. 

Hannes Papenberg

unread,
Apr 29, 2015, 5:26:49 AM4/29/15
to joomla-de...@googlegroups.com
Since there is no new equivalent to those "Legacy" classes, you will
also see them for a LOT longer.

Hannes
> <https://github.com/joomla/joomla-cms/blob/staging/administrator/components/com_content/models/article.php>
>
> --
> 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
> <mailto:joomla-dev-gene...@googlegroups.com>.
> To post to this group, send email to
> joomla-de...@googlegroups.com
> <mailto:joomla-de...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/joomla-dev-general.
> For more options, visit https://groups.google.com/d/optout.

Walt Sorensen

unread,
Apr 29, 2015, 12:10:50 PM4/29/15
to joomla-de...@googlegroups.com
Very true Hannes.

In some ways the lack of a true "new equivalent to those 'Legacy' classes," has me questioning the platform's choice to implement the classes as "legacy" when it was. The current implementation has resulted in legacy classes becoming the default standard practice used rather than a depreciated method to assist developers in transition. But that is a topic for a different discussion.

My point was that there are new base classes which follow the new MVC methods for the platform, and that even the Joomla CMS hasn't updated all of the old code (it's a volunteer community after all).

~Walt
Reply all
Reply to author
Forward
0 new messages