User tests: Successful: Unsuccessful:
$key
is a numeric index of a field in $row array not a field/column name. $row
variable is an array indexed starting from zero. So $key
is unique . Using column name in this param can result in unexpected results.
Here is a test (put it for example in template's index.php):
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title,id')->from('#__menu');
$db->setQuery($query);
$results = $db->loadRowList('id');
var_dump($results);die;
This test should return all menu items indexed by item id. But what it does is returning first item in rows list.
The correct code should be a follows:
Here is test:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title,id')->from('#__menu');
$db->setQuery($query);
$results = $db->loadRowList(1);
var_dump($results);die;
So as you can see description is wrong.
Status | New | ⇒ | Pending |
Labels |
Added:
?
|
Title |
|
Title |
|
||||||
Labels |
Added:
?
|
Category | ⇒ | Code style Libraries |
But what it does is returning first item in rows list.
Wrong! It returns last item in rows list (every subsequent item overwrites previous item) with an empty index and PHP Notice.
Notice: Undefined index: id in /Users/Izhar/Sites/staging/libraries/joomla/database/driver.php on line 1809
Array
(
[] => Array // <== see the empty index created from $row['id']
(
[0] => com_patchtester
[1] => 102
)
)
I'd suggest also rename the parameter $key
to $index
that hints to be integer.
and change the code inside something like:
while ($row = $this->fetchArray($cursor))
{
// Ignore if the specified offset does not exist
if (!isset($index))
{
$array[] = $row;
}
elseif (isset($row[$index]))
{
$array[$row[$index]] = $row;
}
}
Hello @artur-stepien
Thank you for your contribution.
The last comment here was on June 29th. Can you follow up on the feedback?
Thanks for understanding!
I stopped supporting this repository long a go as you can see, thats why didn't respond faster. The description on this PR is still right. Test is still working on 3.6.4 so $key is basically a numeric index of a column in the single result row. The description of this $key parameter did changed in the core but is still unclear so as the one from my PR. Its possible that PR should be recreated and new better description prepared.
any Comment on this PR by Maintainers?
IMO, The note should stay, and yes the parameter type should be integer. String type applies for fetchAssoc
method.
This query works ok in all databases:
$query->clear()->select($db->quoteName('id', '1'))->select($db->quoteName('title', '1'))->from('#__menu');
echo count($db->setQuery($query)->loadRowList(0)); // 129 - testing data
But this returns less rows:
$query->clear()->select($db->quoteName('title', '1'))->select($db->quoteName('id', '1'))->from('#__menu');
echo count($db->setQuery($query)->loadRowList(0)); // 121 - testing data
IMO Note
mentions about unique values in $key
column. Not unique name of column.
I just checked this and in 3.6.5 and it STILL has wrong description (I also used the test method provided in the ticket). It doesn't matter what this function returns when you provide wrong parameter. Only matter what SHOULD be provided as a parameter and how it is described in the function comment. As for now the parameter is a numeric index of a column in a single result. Not a KEY (that suggests a column name). I have no idea why it takes you so long to change one description and how is that even possible that no one tried to check if I'm right.
About @param
you are right. You may also change variable name $key
to $index
if you wish.
About Note
for me it should stay as I explained it above.
I will mark it as a successful test if you revert Note
.
@artur-stepien any Response on @csthomas' suggestion?
Updated. Can we finally close it? It's over 2 years for closing a simple description change ...
I have tested this item
Code review
I have tested this item
Status | Pending | ⇒ | Ready to Commit |
RTC, finally
@artur-stepien thanks for your patience on this one
Status | Ready to Commit | ⇒ | Fixed in Code Base |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2017-04-10 15:02:04 |
Closed_By | ⇒ | rdeutz | |
Labels |
Added:
?
|
Here is a test (put it for example in template's index.php):
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title,id')->from('#__menu');
$db->setQuery($query);
$results = $db->loadRowList('id');
var_dump($results);die;
This test should return all menu items indexed by item id. But what it does is returning first item in rows list.
The correct code should be a follows:
Here is test:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title,id')->from('#__menu');
$db->setQuery($query);
$results = $db->loadRowList(1);
var_dump($results);die;
So as you can see description is wrong.
This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/7239.