JModel _getList()

239 views
Skip to first unread message

Matti Ritala

unread,
Jan 29, 2011, 9:37:54 AM1/29/11
to Joomla! General Development
Hello!

I'm having problems with JModel function _getList(). The documentation
tells that it should return an array but when you check it with
is_array() it always returns false. Does it return an array on success
or in failure also?

I'm trying to do a simple database query to determine if any rows are
returned but I'm stuck how to use this function. Here's some of my
code:

$query = 'SELECT * FROM ' . $prefix . 'medialibrarytags WHERE tag =
"$tag"';
$rows = $this->_getList($query);

if(is_array($rows))
{
$this->tag_exists = TRUE;
}
else
{
$this->tag_exists = FALSE;
}

There must be something that I haven't noticed. H

Binh Le

unread,
Jan 29, 2011, 9:43:09 AM1/29/11
to joomla-de...@googlegroups.com
THis is a private method, you dont call it directly from the model subclass

Matti Ritala

unread,
Jan 29, 2011, 10:00:19 AM1/29/11
to Joomla! General Development
According to the documentation it is protected, so it should be
legimate to able to call it from a subclass.

Ian MacLennan

unread,
Jan 29, 2011, 10:07:48 AM1/29/11
to joomla-de...@googlegroups.com
Are you sure the query works?  Maybe something more like:
$query = 'SELECT * FROM  #__medialibrarytags WHERE tag = '.$this->_db->Quote($tag);

Ian

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.


Matti Ritala

unread,
Jan 29, 2011, 10:31:48 AM1/29/11
to Joomla! General Development
I think the query is fine. When you echo it out it doesn't have any
problems.

I'm trying to just to see whether there are rows in which the value of
tag-field is $tag. The $tag is given as a function parameter.
> > joomla-dev-gene...@googlegroups.com<joomla-dev-general%2Bunsubs cr...@googlegroups.com>
> > .

Binh Le

unread,
Jan 29, 2011, 10:45:45 AM1/29/11
to joomla-de...@googlegroups.com
what s your full source code for this?
where do u place it?

Ian MacLennan

unread,
Jan 29, 2011, 10:57:34 AM1/29/11
to joomla-de...@googlegroups.com
So when you echo it our and run it in phpmyadmin it gives you the expected number of rows?

Ian

To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.

Matti Ritala

unread,
Jan 29, 2011, 11:07:49 AM1/29/11
to Joomla! General Development
Yes, it gives the expected number of rows.

On 29 tammi, 17:57, Ian MacLennan <ian.maclen...@joomla.org> wrote:
> So when you echo it our and run it in phpmyadmin it gives you the expected
> number of rows?
>
> Ian
>
> > > > joomla-dev-gene...@googlegroups.com<joomla-dev-general%2Bunsu...@googlegroups.com><joomla-dev-general%2Bunsubs
> > cr...@googlegroups.com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/joomla-dev-general?hl=en-GB.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Joomla! General Development" group.
> > To post to this group, send an email to
> > joomla-de...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > joomla-dev-gene...@googlegroups.com<joomla-dev-general%2Bunsu...@googlegroups.com>

rich friedel

unread,
Jan 29, 2011, 3:20:04 PM1/29/11
to joomla-de...@googlegroups.com

you have your double quotes inside a single quote thus it is looking for "$tag" literally...

it is better to use a query in joomla the way mr. ian wrote it anyway so as to avoid quote issues.

> To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.

Ian MacLennan

unread,
Jan 29, 2011, 3:47:07 PM1/29/11
to joomla-de...@googlegroups.com
Hmm, that seems odd, because he said that when he echoed the query itself and put it in phpmyadmin, it worked.  My guess would have otherwise been the fact that he hadn't put quotes around tag, but when I specifically asked him and he hold me the query worked right I wasn't sure where to go next.

Now that you say it though, I'm confused as to how the query that he echoed could have worked in phpmyadmin.

Interesting.

Ian

Rob Stocker

unread,
Jan 29, 2011, 4:17:16 PM1/29/11
to joomla-de...@googlegroups.com
Hi Ian,
If $tag is a php variable then Matti must have exchanged it for a value before putting it into phpmyadmin, since phpmyadmin does not accept php variables.

I think.


Rob

rich friedel

unread,
Jan 29, 2011, 4:24:39 PM1/29/11
to joomla-de...@googlegroups.com

yeah I'm with ya... I'm wondering if he is putting it in phpmyadmin verbatim.

either way tho your example is definitely my preferred method. using the hash prefix and quotes method makes for much cleaner, readable code.

On Jan 29, 2011 3:47 PM, "Ian MacLennan" <ian.ma...@joomla.org> wrote:

Matti Ritala

unread,
Jan 29, 2011, 5:14:12 PM1/29/11
to Joomla! General Development
Yes, I had a problem with the quotes and the variable $tag earlier and
posted wrong stuff. I got it sorted it out. Sorry...

Now the problem is how do I check whether the result arrayof
_getList() does contain rows or not, and to translate this to boolean
so I can use it in a If-structure. Any tips?
> On Jan 29, 2011 3:47 PM, "Ian MacLennan" <ian.maclen...@joomla.org> wrote:

Nick Weavers

unread,
Jan 30, 2011, 7:51:07 AM1/30/11
to Joomla! General Development
_getList() is a method in class JModel which is found here libraries/
joomla/application/component/model.php

The code is below and can be seen to use loadObjectList() to get the
result (resultset in db parlance) of the query.

function &_getList( $query, $limitstart=0, $limit=0 )
{
$this->_db->setQuery( $query, $limitstart, $limit );
$result = $this->_db->loadObjectList();

return $result;
}

To see what $result can be, we can look in libraries/joomla/database/
database/(mysqli.php | mysql.php) for the loadObjectList() method in
the JDatabaseMySQLi or JDatabaseMySQL class (depending on what you
chose to use during setup).

/**
* Load a list of database objects
*
* If <var>key</var> is not empty then the returned array is indexed
by the value
* the database key. Returns <var>null</var> if the query fails.
*
* @access public
* @param string The field name of a primary key
* @return array If <var>key</var> is empty as sequential list of
returned records.
*/
function loadObjectList( $key='' )
{
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysqli_fetch_object( $cur )) {
if ($key) {
$array[$row->$key] = $row;
} else {
$array[] = $row;
}
}
mysqli_free_result( $cur );
return $array;
}

So, it returns either a null or an array.

Your test could be

if ($result && count($result) {
echo "result has " . count($result) . " rows<br>";
} else {
echo "result is null or empty. <br>";
}

If $result is null, the first part of the && (and) expression is false
and the second part doesn't get evaluated and the else is performed
and prints "result is null or empty".
If $result isn't null but the array is empty, count($result) will be
zero so the second part of the expression yields false and the else is
performed and prints "result is null or empty".
If result is an array with one or more elements, the if expressions
tests true and prints prints "result n rows" where n is the number of
array elements count() counted.

I hope all of this is correct and helpful. (I read it through but
could still have made a mistake)

Nick


Matti Ritala

unread,
Jan 30, 2011, 12:40:41 PM1/30/11
to Joomla! General Development
Thanks for you effort Nick, but still the result is somehow always
false or null even if the query should return rows.
Strange thing is that even var_dump seem not print out any info. Why
this?

Debreczeni András

unread,
Jan 30, 2011, 2:52:19 PM1/30/11
to Joomla! General Development
var_dump( $this->_db ) ;
could provide accurate info on exactly what the query was that was
submitted to the database.
Look for the [_sql] property!

Also in case of errors, this class dump should contain additional info
under - I think [_error] property.

Make sure you dump it right after the $this->_getList($query );

Matti Ritala

unread,
Jan 30, 2011, 4:12:09 PM1/30/11
to Joomla! General Development
This is so mystical. This var_dump( $this->_db ) ; seems not to
return anything. When I put it in print_r() it returns value 1. I'm so
confused.

On 30 tammi, 21:52, Debreczeni András <debreczeniand...@gmail.com>
wrote:

Andy Nagai

unread,
Jan 30, 2011, 6:36:23 PM1/30/11
to joomla-de...@googlegroups.com
btw, _getList() does not return an array. Your array check will not work.

It returns an object list.

Ian MacLennan

unread,
Jan 30, 2011, 6:57:14 PM1/30/11
to joomla-de...@googlegroups.com

Well, an object list is an array of objects.

Ian

On 2011-01-30 6:36 PM, "Andy Nagai" <andy....@gmail.com> wrote:

btw, _getList() does not return an array. Your array check will not work.

It returns an object list.


On Sun, Jan 30, 2011 at 1:12 PM, Matti Ritala <m.ri...@gmail.com> wrote:

> This is so mystical. Th...

Matti Ritala

unread,
Jan 31, 2011, 6:29:01 AM1/31/11
to Joomla! General Development
So, how do I handle this array of objects? Why does handling these
objects in a view template work through get() and assignRef() in
view.html.php work but not internally in the model class?

On 31 tammi, 01:57, Ian MacLennan <ian.maclen...@joomla.org> wrote:
> Well, an object list is an array of objects.
>
> Ian
>

Matti Ritala

unread,
Jan 31, 2011, 6:56:35 AM1/31/11
to Joomla! General Development
All right, now I solved the problem. I found out that the returned
object list array must be assigned as a variable by reference, not by
value. That's what the assignRef() does.

Thanks a lot for your help and effort.

Nick Weavers

unread,
Feb 1, 2011, 3:23:33 PM2/1/11
to Joomla! General Development
function &_getList( $query, $limitstart=0, $limit=0 )

I guess the clue is the & before the method name :)

http://www.php.net/manual/en/language.references.return.php
Reply all
Reply to author
Forward
0 new messages