DB_DataObject
[ class tree: DB_DataObject ] [ index: DB_DataObject ] [ all elements ]

Class: DB_DataObject

Source Location: /DB_DataObject-0.3/DataObject.php

Class Overview


The main "DB_DataObject" class is really a base class for your own tables classes


Author(s):

Variables

Methods


Child classes:

DB_DataObject_Generator
The main "DB_DataObject" class is really a base class for your own tables classes

Inherited Variables

Inherited Methods


Class Details

[line 109]
The main "DB_DataObject" class is really a base class for your own tables classes

// Set up the class by creating an ini file (refer to the manual for more details [DB_DataObject] database = mysql:/username:password@host/database schema_location = /home/myapplication/database class_location = /home/myapplication/DBTables/ clase_prefix = DBTables_

//Start and initialize...................... - dont forget the & $config = parse_ini_file('example.ini',TRUE); $options = &PEAR::setStaticProperty('DB_DataObject','options'); $options = $config['DB_DataObject'];

// example of a class (that does not use the 'auto generated tables data') class mytable extends DB_DataObject { // mandatory - set the table var $_database_dsn = "mysql://username:password@localhost/database"; var $__table = "mytable"; function _get_table() { return array( 'id' => 1, // integer or number 'name' => 2, // string ); } function _get_keys() { return array('id'); } }

// use in the application

Simple get one row

$instance = new mytable; $instance->get("id",12); echo $instance->somedata;

Get multiple rows

$instance = new mytable; $instance->whereAdd("ID > 12"); $instance->whereAdd("ID < 14"); $instance->find(); while ($instance->fetch()) { echo $instance->somedata; }



[ Top ]


Class Variables

$N =  0

[line 135]

The Number of rows returned from a query
  • Access: public

Type:   int


[ Top ]

$_lastError = FALSE

[line 1366]

Last Error that has occured
  • use $this->_lastError or
$last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
  • Var: (or FALSE)
  • Access: public

Type:   object PEAR_Error


[ Top ]



Method Detail

count   [line 697]

int count( )

find the number of results from a simple query

for example

$object = new mytable(); $object->name = "fred"; echo $object->count();

  • Access: public

[ Top ]

debug   [line 1321]

none debug( string $message, [string $logtype = 0], [string $level = 1])

Debugger. - use this in your extended classes to output debugging information.

Uses DB_DataObject::DebugLevel(x) to turn it on

  • Access: public

Parameters:

string   $message     message - message to output
string   $logtype     logtype - bold at start
string   $level     level - output level

[ Top ]

debugLevel   [line 1346]

none debugLevel( [int $v = NULL])

sets and returns debug level eg. DB_DataObject::debugLevel(4);
  • Access: public

Parameters:

int   $v     level

[ Top ]

delete   [line 610]

boolean delete( [boolean $use_where = FALSE])

Deletes items from table which match current objects variables

Returns the TRUE on success

for example

Designed to be extended

$object = new mytable(); $object->ID=123; echo $object->delete(); // builds a conditon $object = new mytable(); $object->whereAdd('age > 12'); $object->delete(TRUE); // use the condition

  • Return: TRUE on success
  • Access: public

Parameters:

boolean   $use_where     use the whereAdd conditions (default = no - use current values.)

[ Top ]

fetch   [line 324]

boolean fetch( )

fetches next row into this objects var's

returns 1 on success 0 on failure

Example $object = new mytable(); $object->name = "fred"; $object->find(); $store = array(); while ($object->fetch()) { echo $this->ID; $store[] = $object; // builds an array of object lines. }

to add features to a fetch function fetch () { $ret = parent::fetch(); $this->date_formated = date('dmY',$this->date); return $ret; }

  • Return: on success
  • Access: public

[ Top ]

fetchRow   [line 638]

boolean fetchRow( [int $row = NULL])

fetches a specific row into this object variables

Not recommended - better to use fetch()

Returens TRUE on success

  • Return: TRUE on success
  • Access: public

Parameters:

int   $row     row

[ Top ]

find   [line 260]

int find( [boolean $n = FALSE])

find results, either normal or crosstable

for example

$object = new mytable(); $object->ID = 1; $object->find();

will set $object->N to number of rows, and expects next command to fetch rows will return $object->N

  • Access: public

Parameters:

boolean   $n     Fetch first result

[ Top ]

get   [line 163]

int get( string $k, [string $v = NULL])

get an result using key, value

for example $object->get("ID",1234); Returns Number of rows located (usually 1) for success, and puts all the table columns into this classes variables

see the fetch example on how to extend this.

if no value is entered, it is assumed that $key is a value and get will then use the first key in _get_keys to obtain the key.

  • Return: No. of rows
  • Access: public

Parameters:

string   $k     key column
string   $v     key value

[ Top ]

getLink   [line 1151]

mixed &getLink( mixed $row, [mixed $table = NULL])

return name from related object

There are two ways to use this, one is to set up a <dbname>.links.ini file into a static property named <dbname>.links and specifies the table joins, the other is highly dependant on naming columns 'correctly' :) using colname = xxxxx_yyyyyy xxxxxx = related table; (yyyyy = user defined..) looks up table xxxxx, for value id=$this->xxxxx stores it in $this->_xxxxx_yyyyy


[ Top ]

getLinkArray   [line 1197]

void &getLinkArray( mixed $row, [mixed $table = NULL])


[ Top ]

getLinks   [line 1113]

boolean getLinks( )

load related objects

There are two ways to use this, one is to set up a <dbname>.links.ini file into a static property named <dbname>.links and specifies the table joins, the other highly dependent on naming columns 'correctly' :) using colname = xxxxx_yyyyyy xxxxxx = related table; (yyyyy = user defined..) looks up table xxxxx, for value id=$this->xxxxx stores it in $this->_xxxxx_yyyyy


[ Top ]

groupBy   [line 421]

none groupBy( [string $group = FALSE])

Adds a group by condition

$object->groupBy(); //reset the grouping $object->groupBy("ID DESC"); $object->groupBy("ID,age");

  • Access: public

Parameters:

string   $group     Grouping

[ Top ]

insert   [line 501]

int insert( )

Insert the current objects variables into the database

Returns the ID of the inserted element - mysql specific = fixme?

for example

Designed to be extended

$object = new mytable(); $object->name = "fred"; echo $object->insert();

  • Access: public

[ Top ]

limit   [line 446]

none limit( [string $a = NULL], [string $b = NULL])

Sets the Limit

$boject->limit(); // clear limit $object->limit(12); $object->limit(12,10);

  • Access: public

Parameters:

string   $a     limit start (or number), or blank to reset
string   $b     number

[ Top ]

orderBy   [line 398]

none orderBy( [string $order = FALSE])

Adds a order by condition

$object->orderBy(); //clears order by $object->orderBy("ID"); $object->orderBy("ID,age");

  • Access: public

Parameters:

string   $order     Order

[ Top ]

query   [line 733]

none query( string $string)

sends raw query to database

Since _query has to be a private 'non overwriteable method', this is a relay

  • Access: public

Parameters:

string   $string     SQL Query

[ Top ]

raiseError   [line 1379]

error raiseError( int $message, int $type, [int $behaviour = NULL])

Default error handling is to create a pear error, but never return it.

if you need to handle errors you should look at setting the PEAR_Error callback this is due to the fact it would wreck havoc on the internal methods!

  • Return: object
  • Access: public

Parameters:

int   $message     message
int   $type     type
int   $behaviour     behaviour (die or contine!);

[ Top ]

selectAdd   [line 473]

none selectAdd( [mixed $k = NULL])

Adds a select columns

$object->selectAdd(); // resets select to nothing! $object->selectAdd("*"); // default select $object->selectAdd("unixtime(DATE) as udate"); $object->selectAdd("DATE");

  • Access: public

[ Top ]

setFrom   [line 1235]

boolean setFrom( array &$from)

Copies items that are in the table definitions from an array or object into the current object will not override key values.
  • Return: , TRUE on success
  • Access: public

Parameters:

array   &$from     | object

[ Top ]

staticGet   [line 202]

object &staticGet( string $class, string $k, [string $v = NULL])

a autoloading, caching static get method using key, value (based on get)

Usage: $object = DB_DataObject::staticGet("DbTable_mytable",12); or $object = DB_DataObject::staticGet("DbTable_mytable","name","fred");

or write it into your extended class: function &staticGet($k,$v=NULL) { return DB_DataObject::staticGet("This_Class",$k,$v); }

  • Access: public

Parameters:

string   $class     class name
string   $k     key column (or value if using _get_keys)
string   $v     key value (optional)

[ Top ]

staticInitialize   [line 1400]

error staticInitialize( )

Define the $GLOBALS['_DB_DATAOBJECT_PRODUCTION'] variable

After Profiling DB_DataObject, I discoved that the debug calls where taking considerable time (well 0.1 ms), so this should stop those calls happening. by setting production =1 in the config, you disable all debug calls..

  • Return: object
  • Access: public

[ Top ]

update   [line 558]

boolean update( )

Updates current objects variables into the database uses the _get_keys() to decide how to update Returns the TRUE on success

for example

$object = new mytable(); $object->get("ID",234); $object->email="testing@test.com"; if(!$object->update()) echo "UPDATE FAILED";

  • Return: TRUE = success
  • Access: public

[ Top ]

validate   [line 1278]

array validate( )

validate - override this to set up your validation rules

validate the current objects values either just testing strings/numbers or using the user defined validate{Row name}() methods. will attempt to call $this->validate{column_name}() - expects TRUE = ok FALSE = ERROR you can the use the validate Class from your own methods.

  • Return: of validation results or TRUE
  • Access: public

[ Top ]

whereAdd   [line 373]

none whereAdd( [string $cond = FALSE], [string $logic = "AND"])

Adds a condition to the WHERE statement, defaults to AND

$object->whereAdd(); //reset or cleaer ewhwer $object->whereAdd("ID > 20"); $object->whereAdd("age > 20","OR");

  • Access: public

Parameters:

string   $cond     condition
string   $logic     optional logic "OR" (defaults to "AND")

[ Top ]


Documentation generated on Fri, 30 Apr 2004 21:33:21 +0200 by phpDocumentor 1.2.3. PEAR Logo Copyright © PHP Group 2004.