count [line 1449]
int count(
[bool|string
$countWhat = false], [bool
$whereAddOnly = false])
|
|
Find the number of results from a simple query
for example
$object = new mytable(); $object->name = "fred"; echo $object->count(); echo $object->count(true); // dont use object vars. echo $object->count('distinct mycol'); count distinct mycol. echo $object->count('distinct mycol',true); // dont use object vars. echo $object->count('distinct'); // count distinct id (eg. the primary key)
Parameters:
database [line 1780]
string database(
string
0)
|
|
Return or assign the name of the current database
Parameters:
databaseStructure [line 1635]
true databaseStructure(
optional
0, optional
1, optional
2)
|
|
Autoload or manually load the table definitions
usage : DB_DataObject::databaseStructure( 'databasename', parse_ini_file('mydb.ini',true), parse_ini_file('mydb.link.ini',true));
obviously you dont have to use ini files.. (just return array similar to ini files..)
It should append to the table structure array
Parameters:
debug [line 3658]
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
Parameters:
debugLevel [line 3697]
none debugLevel(
[int
$v = null])
|
|
sets and returns debug level eg. DB_DataObject::debugLevel(4);
Parameters:
delete [line 1298]
mixed delete(
[bool
$useWhere = 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->limit(1); $object->orderBy('age DESC'); $object->delete(true); // dont use object vars, use the conditions, limit and order.
Parameters:
escape [line 1534]
string escape(
string
$string)
|
|
an escape wrapper around DB->escapeSimple() can be used when adding manual queries or clauses eg.
$object->query("select * from xyz where abc like '". $object->escape($_GET['name']) . "'");
Parameters:
fetch [line 488]
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; }
fetchRow [line 1374]
boolean fetchRow(
[int
$row = null])
|
|
fetches a specific row into this object variables
Not recommended - better to use fetch()
Returens true on success
Parameters:
find [line 386]
mixed 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
Parameters:
free [line 3792]
Free global arrays associated with this object.
Note: as we now store resultfields in a global, it is never freed, if you do alot of calls to find(), memory will grow gradually.
fromValue [line 3476]
true| fromValue(
string
$col, mixed
$value)
|
|
standard set* implementation.
takes data and uses it to set dates/strings etc. normally called from __call..
Current supports date = using (standard time format, or unixtimestamp).... so you could create a method : function setLastread($string) { $this->fromValue('lastread',strtotime($string)); }
time = using strtotime datetime = using same as date - accepts iso standard or unixtimestamp. string = typecast only..
TODO: add formater:: eg. d/m/Y for date! ???
Parameters:
get [line 282]
int get(
[string
$k = null], [string
$v = null])
|
|
Get a 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 keys() to obtain the key.
Parameters:
getDatabaseConnection [line 3329]
object The &getDatabaseConnection(
)
|
|
Gets the DB object related to an object - so you can use funky peardb stuf with it :)
getDatabaseResult [line 3351]
object The &getDatabaseResult(
)
|
|
Gets the DB result object related to the objects active query
- so you can use funky pear stuff with it - like pager for example.. :)
getLink [line 2680]
mixed &getLink(
string
$row, [string
$table = null], [string
$link = false])
|
|
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' :)
NOTE: the naming convention is depreciated!!! - use links.ini
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
you can also use $this->getLink('thisColumnName','otherTable','otherTableColumnName')
Parameters:
getLinkArray [line 2758]
array &getLinkArray(
$row, [
$table = null])
|
|
IS THIS SUPPORTED/USED ANYMORE???? return a list of options for a linked table
This 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
Parameters:
getLinks [line 2602]
boolean getLinks(
[string
$format = '_%s'])
|
|
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 you can change what object vars the links are stored in by changeing the format parameter
Parameters:
groupBy [line 643]
none|PEAR::Error groupBy(
[string
$group = false])
|
|
Adds a group by condition
$object->groupBy(); //reset the grouping $object->groupBy("ID DESC"); $object->groupBy("ID,age");
Parameters:
having [line 678]
none|PEAR::Error having(
[string
$having = false])
|
|
Adds a having clause
$object->having(); //reset the grouping $object->having("sum(value) > 0 ");
Parameters:
insert [line 856]
Insert the current objects variables into the database
Returns the ID of the inserted element (if auto increment or sequences are used.)
for example
Designed to be extended
$object = new mytable(); $object->name = "fred"; echo $object->insert();
joinAdd [line 2860]
none joinAdd(
[optional
$obj = false], [optional
$joinType = 'INNER'], [optional
$joinAs = false], [optional
$joinCol = false])
|
|
joinAdd - adds another dataobject to this, building a joined query.
example (requires links.ini to be set up correctly) // get all the images for product 24 $i = new DataObject_Image(); $pi = new DataObjects_Product_image(); $pi->product_id = 24; // set the product id to 24 $i->joinAdd($pi); // add the product_image connectoin $i->find(); while ($i->fetch()) { // do stuff } // an example with 2 joins // get all the images linked with products or productgroups $i = new DataObject_Image(); $pi = new DataObject_Product_image(); $pgi = new DataObject_Productgroup_image(); $i->joinAdd($pi); $i->joinAdd($pgi); $i->find(); while ($i->fetch()) { // do stuff }
Parameters:
limit [line 719]
none|PEAR::Error limit(
[string
$a = null], [string
$b = null])
|
|
Sets the Limit
$boject->limit(); // clear limit $object->limit(12); $object->limit(12,10);
Note this will emit an error on databases other than mysql/postgress as there is no 'clean way' to implement it. - you should consider refering to your database manual to decide how you want to implement it.
Parameters:
links [line 2559]
Get the links associate array as defined by the links.ini file.
Experimental... - Should look a bit like [local_col_name] => "related_tablename:related_col_name"
orderBy [line 608]
none|PEAR::Error orderBy(
[string
$order = false])
|
|
Adds a order by condition
$object->orderBy(); //clears order by $object->orderBy("ID"); $object->orderBy("ID,age");
Parameters:
query [line 1518]
void query(
string
$string)
|
|
sends raw query to database
Since _query has to be a private 'non overwriteable method', this is a relay
Parameters:
raiseError [line 3732]
error raiseError(
int
$message, [int
$type = null], [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!
Parameters:
selectAdd [line 762]
mixed selectAdd(
[string
$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");
to prepend distict: $object->selectAdd('distinct ' . $object->selectAdd());
Parameters:
selectAs [line 802]
void selectAs(
[array|object|null
$from = null], [string
$format = '%s'], [string
$tableName = false])
|
|
Adds multiple Columns or objects to select with formating.
$object->selectAs(null); // adds "table.colnameA as colnameA,table.colnameB as colnameB,......" // note with null it will also clear the '*' default select $object->selectAs(array('a','b'),'%s_x'); // adds "a as a_x, b as b_x" $object->selectAs(array('a','b'),'ddd_%s','ccc'); // adds "ccc.a as ddd_a, ccc.b as ddd_b" $object->selectAdd($object,'prefix_%s'); // calls $object->get_table and adds it all as objectTableName.colnameA as prefix_colnameA
Parameters:
setFrom [line 3128]
true setFrom(
array
&$from, [string
$format = '%s'], [
$checkEmpty = false])
|
|
Copies items that are in the table definitions from an array or object into the current object will not override key values.
Parameters:
staticGet [line 328]
object &staticGet(
string
$class, string
$k, [string
$v = null])
|
|
An 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); }
Parameters:
table [line 1796]
get/set an associative array of table columns
Parameters:
tableName [line 1764]
string tableName(
string
0)
|
|
Return or assign the name of the current table
Parameters:
toArray [line 3211]
array toArray(
[string
$format = '%s'], [bool
$hideEmpty = false])
|
|
Returns an associative array from the current data (kind of oblivates the idea behind DataObjects, but is usefull if you use it with things like QuickForms.
you can use the format to return things like user[key] by sending it $object->toArray('user[%s]')
will also return links converted to arrays.
Parameters:
toValue [line 3576]
true toValue(
string
$col, [format
$format = null])
|
|
standard get* implementation.
with formaters.. supported formaters: date/time : %d/%m/%Y (eg. php strftime) or pear::Date numbers : %02d (eg. sprintf) NOTE you will get unexpected results with times like 0000-00-00 !!!
Parameters:
update [line 1104]
int update(
[object dataobject
$dataObject = false])
|
|
Updates current objects variables into the database uses the keys() to decide how to update Returns the true on success
for example
$object = DB_DataObject::factory('mytable'); $object->get("ID",234); $object->email="testing@test.com"; if(!$object->update()) echo "UPDATE FAILED";
to only update changed items : $dataobject->get(132); $original = $dataobject; // clone/copy it.. $dataobject->setFrom($_POST); if ($dataobject->validate()) { $dataobject->update($original); } // otherwise an error...
performing global updates: $object = DB_DataObject::factory('mytable'); $object->status = "dead"; $object->whereAdd('age > 150'); $object->update(DB_DATAOBJECT_WHEREADD_ONLY);
Parameters:
validate [line 3259]
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.
This should really be in a extenal class - eg. DB_DataObject_Validate.
whereAdd [line 571]
string|PEAR::Error 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");
Parameters:
_call [line 3385]
true? _call(
$method,
$params,
&$return)
|
|
Overload Extension support
- enables setCOLNAME/getCOLNAME
if you define a set/get method for the item it will be called.
otherwise it will just return/set the value. NOTE this currently means that a few Names are NO-NO's eg. links,link,linksarray, from, Databaseconnection,databaseresult
note
- get is automatically called by toArray()
setters return true on success. = strings on failure getters return the value!
this fires off trigger_error - if any problems.. pear_error, has problems with 4.3.2RC2 here
Parameters:
_loadConfig [line 3774]
Define the global $_DB_DATAOBJECT['CONFIG'] as an alias to PEAR::getStaticProperty('DB_DataObject','options');
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. as all calls to debug are wrapped with direct variable queries rather than actually calling the funciton THIS STILL NEEDS FURTHER INVESTIGATION