PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » DB_DataObject » Bug #1315

$object->toArray not "arrayizing" all elements

Details

Submitted2004-04-29 17:43 UTC
Fromasnagy at webitecture dot org
StatusNo Feedback
PackageDB_DataObject
PHP Version4.3.4
OSlinux
Roadmaps(Not assigned)

Comments

[2004-04-29 17:43 UTC] asnagy at webitecture dot org

Description:
------------
My dataObject ocassionally will have a special query where it will include some addition fields from other tables. When calling $object->toArray() the additional fields get left out.

[2004-05-20 15:05 UTC] asnagy at php dot net

What about replacing the toArray method with the get_object_vars function? I am using this as my workaround:

while ($object->fetch()) {
//$objectList[] = $object->toArray();
$objectList[] = get_object_vars($object);
}

You could just change the code in the toArray method to just one line with the get_object_vars function. Probably be faster too since its a core function :)

[2004-11-16 20:20 UTC] asnagy at webitecture dot org

This works now; however it is turning the other table values into dataobjects.

Array
(
[id] => 1
[standard_number] => NULL
[callnumber] =>
[title] => ATLA Religion Database
[holding] =>
[status] =>
[format] =>
[department_id] =>
[cost] =>
[location] =>
[type_id] => 2
[type] => resourcetype Object
(
[_DB_DataObject_version] => 1.7.2
[__table] => resourcetype
[N] => 1
[_database_dsn] =>
[_database_dsn_md5] => eb8bf4877f5e951f4eb46d5f0efbf157
[_database] => libSGR2
[_DB_resultid] => 2
[_link_loaded] =>
[_join] =>
[_lastError] =>
[id] => 2
[title] => Database
[_query] => Array
(
[condition] =>
[group_by] =>
[order_by] =>
[having] =>
[limit] =>
[data_select] => *
)

)

)

Here is my code:
$resource = new Resource();
$sql = "SELECT resource.*, resourcetype.title as type FROM resource, resourcetype WHERE resource.type_id = resourcetype.id";
$resource->query($sql);
if ($resource->N) {
while ($resource->fetch()) {
print_r($resource->toArray());
}
}

What I would expect as a result from this is:

Array
(
[id] => 1
[standard_number] => NULL
[callnumber] =>
[title] => ATLA Religion Database
[holding] =>
[status] =>
[format] =>
[department_id] =>
[cost] =>
[location] =>
[type_id] => 2
[type] => Database
)

This may not be a bug, but is this what you meant for the toArray method to do? I am not using the resourcetype table as a dataobject, but from running the create table script I get the dataobject file created.

[2004-11-16 20:47 UTC] asnagy at webitecture dot org

Sorry for reopenning this, my last comment is bogus.

[2004-11-16 20:52 UTC] asnagy at webitecture dot org

Rough day! Ignore the last statement :)

Here is my before and after toArray();

resource Object
(
[_DB_DataObject_version] => 1.7.2
[__table] => resource
[N] => 27
[_database_dsn] =>
[_database_dsn_md5] => eb8bf4877f5e951f4eb46d5f0efbf157
[_database] => libSGR2
[_DB_resultid] => 1
[_link_loaded] =>
[_join] =>
[_lastError] =>
[id] => 1
[standard_number] => NULL
[callnumber] =>
[title] => ATLA Religion Database
[holding] =>
[status] =>
[format] => Electronic
[department_id] =>
[cost] =>
[location] =>
[type_id] => 2
[type] => Database
)
Array
(
[id] => 1
[standard_number] => NULL
[callnumber] =>
[title] => ATLA Religion Database
[holding] =>
[status] =>
[format] =>
[department_id] =>
[cost] =>
[location] =>
[type_id] => 2
[type] => resourcetype Object
(
[_DB_DataObject_version] => 1.7.2
[__table] => resourcetype
[N] => 1
[_database_dsn] =>
[_database_dsn_md5] => eb8bf4877f5e951f4eb46d5f0efbf157
[_database] => libSGR2
[_DB_resultid] => 2
[_link_loaded] =>
[_join] =>
[_lastError] =>
[id] => 2
[title] => Database
[_query] => Array
(
[condition] =>
[group_by] =>
[order_by] =>
[having] =>
[limit] =>
[data_select] => *
)

)

)

Upon calling toArray, the field is being turned into an object

Here is the code:

if ($resource->find()) {
while ($resource->fetch()) {
print_r($resource);
print_r($resource->toArray());
}
}

[2005-01-24 14:43 UTC] asnagy at webitecture dot org

I do not have a method called "getType" but since I have a field in my database by the name of "type" (which is probably not the best column name) I'm assuming the that get* method is being called. Could you rename your method?