Re: Contributing / DB_Object
| From: | fabien dot marty at free dot fr | Date: | Mon, 29 Apr 2002 14:20:54 +0000 |
| Subject: | Re: Contributing / DB_Object | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-5886@lists.php.net to get a copy of this message | ||
(Mika speaking)
> I've been playing with a bit similar idea, but just the other
> way: (...)
to generate your get and set methods, what about using :
function genericGet($field)
{
return $this->{$field};
}
and
function genericSet($field, $value)
{
$this->{$field} = $value;
}
I you don't like this solution (there are some reasons),
the only way (i think) is to make a little PHP script which
generate the php code you want. Then, you make a copy/paste
to add these trivial methods to the rest of your class.
(to make the generation script, you may use
DB_Object::_fieldsInfo() to get the names of the fields)
For the save() method, I use a global variable
$db_connection (the DB connection) and my save() method
is for example :
// to save changes
function save()
{
global $db_connection;
DB_Object::update($db_connection, "foo", "id=$this->id", $this,
"id");
// Save in the "foo" table the current object
// on the record (specified with WHERE id=$this->id)
// without recording the 'id' field
}
// to insert (new record) into database
function insert()
{
global $db_connection;
DB_Object::insert($db_connection, "foo", $this);
// Insert in the "foo" table the current object
// NB : if 'id' field is AUTOINCREMENT, you
// have to add the param 'id' at the end of the
// insert call (not to record 'id' initial value)
}
Fabien
(fabien.marty@free.fr)