Database query returns unexpected keys

Bug #689717 reported by jsherk
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
PHPDevShell
Invalid
Undecided
Unassigned

Bug Description

Ok, try the following:

Given this database table:
CREATE TABLE pds_test (
  id int(11) NOT NULL AUTO_INCREMENT,
  `name` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO pds_test (id, `name`) VALUES
(2, 'Bob'),
(4, 'Jeff');

And this controller test.php:
<?php
class test extends PHPDS_controller
{
public function execute()
 {
 $test = $this->db->invokeQuery('myPlugin_test');
 echo '<pre>';
 print_r($test);
 echo '</pre>';
 }
}
return 'test';

And this model:
<?php
class myPlugin_test extends PHPDS_query
{
protected $sql = "
 SELECT *
 FROM _db_test
";
}

This returns the following results:
Array
(
    [2] => Array
        (
            [id] => 2
            [name] => Bob
        )

    [4] => Array
        (
            [id] => 4
            [name] => Jeff
        )

)

I would have expected the results of the main key to start with an index of 0 (see below) and not with the [id] field:
Array
(
    [0] => Array
        (
            [id] => 2
            [name] => Bob
        )

    [1] => Array
        (
            [id] => 4
            [name] => Jeff
        )

)

Revision history for this message
jsherk (jeff-forerunnertv) wrote :

Ok, after looking into this further, I am seeing that setting the data query switch to $keyField=false will cause the result keys to returned as a numeric array starting at index[0].

In my opinion, this should be the default behavior.

So I would suggest that default behavior, when not specified, is $keyField=false and not $keyField='__auto__'

Revision history for this message
Greg (gregfr) wrote :

The whole idea of this system is to be intuitive and easy to use. And believe me, having a table already hashed by key field is really handy. You can still iterate by index using array_keys(). And in the rare case you need linear indexing, just add the flag to the query, as you did.

Example:

SELECT name, phone FROM phonebook => $numbers

echo $numbers['jason'];

Revision history for this message
jsherk (jeff-forerunnertv) wrote :

I just needed to understand what it was doing and why is was doing it! Your example above makes perfect sense! Thanks

Changed in phpdevshell:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.