Source for file dbrow.php
Documentation is available at dbrow.php
* Database row base class
* @author Stuart Prescott
* @copyright Copyright Stuart Prescott
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @subpackage FormsLibrary
/** Load ancillary functions */
require_once 'inc/typeinfo.php';
require_once 'dbobject.php';
/** status codes for success/failure of database actions */
require_once 'inc/statuscodes.php';
* Object representing a database row (and extensible to represent joined rows)
* #set database connection parameters
* $obj = new DBRow("users", 14, "userid");
* #set the fields required and their attributes
* $obj->addElement(....);
* #connect to the database
* #check to see if user data changes some values
* #synchronise with database
* @subpackage FormsLibrary
/** @var boolean this is a new object the form for which has not yet been shown to the user */
/** @var boolean this row should be inserted into the db */
/** @var boolean include all fields in the SQL statement not just ones that have changed or have values */
/** @var boolean automatically number new objects (i.e. the database will do it for us) */
/** @var string a restriction that is included in the WHERE statement for all queries */
/** @var string number of the start record number for a LIMIT statement */
/** @var string number of the stop record number for a LIMIT statement */
/** @var string do a two-step synchronisation routine whereby the record is created first then updated second */
/** @var array additional rows to be included at the end of the display table */
/** @var boolean row is marked as deleted in the table (but not actually deleted) */
/** @var string this object can be deleted from the table (using DELETE); otherwise set the delete column to 1 for delete */
* Create a new database row object
* @param string $table name of the table to be used
* @param integer $id row id number in the table (-1 for new object)
* @param string $idfield (optional) the column in the table for the primary key (id)
function DBRow($table, $id, $idfield=
'id') {
$this->DBO($table, $id, $idfield);
#$this->fields = array();
* Set the value of the primary key (id) for this object
* @param integer $newId the id value to use
$this->log('DBRow: setting new id'.
$newId);
if ($this->fields[$k]->notifyIdChange) {
$this->fields[$k]->idChange($newId);
* Update the object with the user-submitted data
* update the value of each of the objects fields according to the user
* input data, and validate the data if appropriate
* @param array user supplied data (field => $value)
* @return boolean data is valid
$this->log('DBRow:'.
$this->namebase.
' Looking for updates:');
// First, check to see if this record is new
// We're a new object, but has the user filled the form in, or is the
// user about to fill the form in?
$this->log('I AM NOT NEW '.
$k.
':changed');
// check each field in turn to allow it to update its data
$this->log("Check $k ov:".
$this->fields[$k]->value
$this->log('nv:'.
$this->fields[$k]->value.
' '.
($this->changed ?
'changed' :
'not changed'));
* check the validity of the data
* @return boolean data is valid
// check each field in turn to allow it to update its data
// if this object has not been filled in by the user, then
$this->log('Checking valid '.
$this->fields[$k]->namebase .
$k);
if (! $this->fields[$k]->isValid()) {
.
'('.
$this->fields[$k]->name.
')'
.
' = "'.
$this->fields[$k]->getValue() .
'"<br />';
.
T_('Some values entered into the form are not valid and should be highlighted in the form below. Please check your data entry and try again.');
* Synchronise this object's fields with the database
* If the object is new, then INSERT the data, if the object is pre-existing
* then UPDATE the data. Fancier fields that are only pretending to
* do be simple fields (such as JOINed data) should perform their updates
* during the _sqlvals() call
* @return integer from statuscodes
// If the input isn't valid then bail out straight away
$this->log('not syncing: changed='.
$this->changed);
$this->log('starting two-step sync');
//obtain the *clean* parameter='value' data that has been SQL-cleansed
//this will also trip any complex fields to sync
//echo "changed with vals=$vals<br/>";
//it's an existing record, so update
$q =
'UPDATE '.
$TABLEPREFIX.
$this->table
//it's a new record, insert it
$q =
'INSERT '.
$TABLEPREFIX.
$this->table.
' SET '.
$vals;
# FIXME: do we need to check that this was successful in here?
//the record number can now be copied into the object's data.
//echo "sql=$sql_result, oob=$this->oob_status\n";
* An alternative way of synchronising this object's fields with the database.
* Using this approach, we:
* - If the object is new, then INSERT a temp row first.
* - Then, trip the sqlvals() calls.
* - Then, UPDATE the data.
* Here, we to the 'create temp row' part.
foreach ($this->fields as $field) {
if ($field->requiredTwoStage) {
$row->addElement(clone($field));
//the record number can now be copied into the object's data.
$this->log('Created temp row for locking, id='.
$this->id.
')');
* Delete this object's row from the database.
* @param mixed (optional) string or array of column => value added to the UPDATE statement objects are only to be marked as deleted not actually deleted.
* @return integer from statuscodes
function delete($extraUpdates=
NULL) {
$this->log('$id == -1, so nothing to do');
$this->log('Object not deletable by rule.');
$this->errorMessage =
T_('Cannot delete this item. Permission denied.');
$q =
'DELETE FROM '.
$TABLEPREFIX.
$this->table
} elseif ($extraUpdates !==
NULL) {
$updates[] =
$extraUpdates;
// toggle the deleted state
$updates[] =
'deleted='.
($this->isDeleted?
0:
1); // old MySQL cannot handle true, use 0,1 instead
$q =
'UPDATE '.
$TABLEPREFIX.
$this->table
.
' SET '.
join($updates, ', ')
* Generate name='value' data for the SQL statement
* @param boolean $force (optional) force all fields to be included
* @return string of data statements
if ($this->fields[$k]->changed ||
$force) {
//obtain a string of the form "name='Stuart'" from the field.
//Complex fields can use this as a JIT syncing point, and may
//choose to return nothing here, in which case their entry is
//not added to the return list for the row
$this->log('Getting SQL string for '.
$this->fields[$k]->name, 8);
$sqlval =
$this->fields[$k]->sqlSetStr('', $force);
#echo "$k,oob = '".$this->fields[$k]->oob_status."' ";
#echo "SQLUpdate: '$sqlval' <br />";
#$vals[] = "$k=" . qw($v->value);
#echo "<pre>"; print_r($vals); echo "</pre>";
* Add a new field to the row
* Add an element into the fields[] array. The element must conform
* to the Fields class (or at least its interface!) as that will be
* assumed elsewhere in this object.
* Inheritable attributes are also set here.
* @param Field $el the field to add
$this->fields[$el->name] =
$el;
if ($this->fields[$el->name]->editable == -
1) {
if (! isset
($this->fields[$el->name]->namebase)) {
#echo "Altered field $el->name to $this->namebase\n";
if ($this->fields[$el->name]->suppressValidation == -
1) {
#echo "Altered field $el->name to $this->namebase\n";
#echo "foo:".$this->fields[$el->name]->name.":bar";
* Add multiple new fields to the row
* Adds multiple elements into the fields[] array.
* @param array $els array of Field objects
* Perform the SQL lookup to fill the object with the current data
* Fill this object (i.e. its fields) from the SQL query
* @global string prefix for table names
//echo "foo:$this->id:bar";
if (($this->id !==
NULL &&
$this->id !==
'' &&
$this->id != -
1) ||
$this->ignoreId) {
//FIXME: can we do this using quickSQLSelect()?
.
' WHERE '.
join($where, ' AND ')
if (! $this->fields[$k]->sqlHidden) {
//we have to have an id present otherwise we're in trouble next time
#echo "Filling with defaults";
#echo get_class($this->fields[$k]);
#echo '"'.$this->fields[$k]->defaultValue.'"';
* Quick and dirty dump of fields (values only, not a full print_r
foreach ($this->fields as $v) {
$t .=
"\t".
$v->text_dump();
* Display the row as a form in a table
* @param integer $j (optional) number of columns in the table (will pad as necessary)
* @return string html table
$t =
'<table class="tabularobject">';
foreach ($this->fields as $v) {
$t .=
$v->displayInTable($j);
* PHP5 clone statement will perform only a shallow copy of the object. Any subobjects must also be cloned
Documentation generated on Tue, 06 Mar 2007 10:01:22 +0000 by phpDocumentor 1.3.0