Framework Development (2)
| From: | Simon Biela | Date: | Fri, 14 Jun 2002 10:23:35 +0000 |
| Subject: | Framework Development (2) | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-7014@lists.php.net to get a copy of this message | ||
As I realised that sending attachments is not very intelligent, here's the code of the class:
<?
class KBDBObject
{
var $table;
var $fields;
var $keycolumn;
var $db;
var $allowedfields;
// sets key-value
function setKeyColumn($_name)
{
$this->keycolumn = $_name;
}
// sets the table-name in the database
function setTable($_name)
{
$this->table = $_name;
}
// Contructor setting the fields allowed (field in the database) and the db name
// and maybe calling read_from_database() if id is given.
function KBDBObject($_db, $id)
{
$this->db = $_db;
$this->allowedfields = '';
//$this->allowedfields = array();
//array_push($this->allowedfields, 'password');
}
function setFields($_fields)
{
foreach ($_fields as $key => $value)
{
$this->setField($key, $value);
}
}
function setField($_name, $_value)
{
if ($this->allowedfields != '')
{
if (in_array($_name, $this->allowedfields))
{
$this->fields[$_name] = $_value;
}
else
{
trigger_error("Invalid field added", E_USER_WARNING);
}
}
else
$this->fields[$_name] = $_value;
}
function unsetField($_name)
{
unset($this->fields[$_name]);
}
// get fields from the database
function get_fields() {
}
function get_field() {
}
// save the object in the database
// will basically call the update or insert function
function write_to_database() {
}
// read an object from the database
// will call the select() function
function read_from_database() {
}
// building the sql for insert and sending to adodb
function insert()
{
$sqlst = "INSERT INTO ".$this->table." (";
foreach ($this->fields AS $key => $value)
{
if ($key != $this->keycolumn)
{
if ($this->allowedfields != '')
{
if (in_array($key, $this->allowedfields))
{
$sqlst .= $key.', ';
}
}
else
$sqlst .= $key.', ';
}
}
$sqlst = substr($sqlst, 0, (strlen($sqlst) - 2)).') VALUES (';
foreach ($this->fields AS $key => $value)
{
if ($key != $this->keycolumn)
{
if ($this->allowedfields != '')
{
if (in_array($key, $this->allowedfields))
{
$sqlst .= "'".$value."', ";
}
}
else
$sqlst .= "'".$value."', ";
}
}
$sqlst = substr($sqlst, 0, (strlen($sqlst) - 2)).')';
echo $sqlst.'<br>';
/*
if ($this->db->Execute($sql) === false)
{
print 'error inserting: '.$conn->ErrorMsg().'<BR>';
}
*/
}
// building the sql for update and sending to adodb
function update($keyvalue)
{
$sqlst = "UPDATE ".$this->table." SET ";
foreach ($this->fields AS $key => $value)
{
if ($key != $this->keycolumn)
{
if ($this->allowedfields != '')
{
if (in_array($key, $this->allowedfields))
{
$sqlst .= $key." = '".$value."'".', ';
}
}
else
$sqlst .= $key." = '".$value."'".', ';
}
}
$sqlst = substr($sqlst, 0, (strlen($sqlst) - 2)).' WHERE '.$this->keycolumn." =
'".$keyvalue."'";
echo $sqlst.'<br>';
}
// building the sql for delete and sending to adodb
function delete($keyvalue)
{
$sqlst = "DELETE FROM ".$this->table." WHERE ".$this->keycolumn."
= '".$keyvalue."'";
echo $sqlst.'<br>';
}
// building the select sql and sending to adodb
function select() {
}
}
?>
mit freundlichen Gruessen
Simon Biela
Geschaeftsfuehrung
---
kernpunkt GmbH
cross communication
Krefelder Strasse 18
50670 Koeln
tel: +49 221 - 569 576 10
fax: +49 221 - 569 576 29
infoline: 0800 KERNPUNKT
s.biela@kernpunkt.de
http://www.kernpunkt.de