Req #45276 [Opn->Wfx]: feature request: mysql_fetch_object_dynamic
| From: | johannes@php.net | Date: | Mon, 18 Apr 2011 12:10:07 +0000 |
| Subject: | Req #45276 [Opn->Wfx]: feature request: mysql_fetch_object_dynamic | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-160249@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=45276&edit=1
ID: 45276
Updated by: johannes@php.net
Reported by: darrel dot opry at gmail dot com
Summary: feature request: mysql_fetch_object_dynamic
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: MySQL related
PHP Version: 5.3CVS-2008-06-15 (CVS)
Block user comment: N
Private report: N
New Comment:
It is hard to do properly in a generic way which works for most cases.
Which we have to do at the level. You can build an iterator or such
doing what you need quite efficiently.
Previous Comments:
------------------------------------------------------------------------
[2008-06-15 19:39:45] darrel dot opry at gmail dot com
Description:
------------
Current you can specify a class name for mysql_fetch_object to
instantiate a class in the request. For some of my applications
I do not know the proper class at the point I'm querying the
database. Normally this happens when working with child classes
that have identical database data requirements as their parents.
Currently I use factory functions that query the database and
instantiate the proper class based on the row object, and pass the
row object into the constructor to initialized the object.
It would be nice to be able to specify a table column that contains the
class name for an object. Something along the lines of:
/**
* @param mysql_result $result
* @param string $class_column to column of the row containing
* the class name that should be initilized.
* @param array $ctor_args arguments to pass to the class constructor.
*/
mysql_fetch_object_dynamic($result, $class_column, $ctor_args)
Example Use Case:
Loading child classes from with identical data storage.
Given:
table strings(id, class, value);
class string {
$id = 0;
$value = '';
function set_value($value) {
if (!$this->validate($value)) return false;
$this->value = $value;
return true;
}
function validate($value) {
return is_string($value);
}
function print() {
print $this->value();
}
function save() {
if ($this->id) {
mysql_query("UPDATE strings
SET value='$this->value'
WHERE id=$this->id");
}
else {
mysql_query('INSERT INTO {strings}
($this->is, __CLASS__, $this->value));
}
}
}
class email extends string {
function validate($value) {
return is_email($value);
}
function print() {
print "<a href='mailto:
$this->value'>$this->value</a>\";
}
}
$result = mysql_query('SELECT * FROM strings');
while($string = mysql_fetch_object_dynamic($result, 'class')) {
$string->print();
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=45276&edit=1