ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:activerecord:getactiverecordsclass

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
v5:activerecord:getactiverecordsclass [2015/12/04 22:35]
mnewnham created
v5:activerecord:getactiverecordsclass [2018/07/02 14:17] (current)
peterdd prefer functions start lowercase (#430)
Line 1: Line 1:
-====== ​getActiveRecords ​======+====== ​getActiveRecordsClass ​======
 ~~NOTOC~~ ~~NOTOC~~
 <WRAP right box> <WRAP right box>
-== syntax ​== +== Syntax ​== 
-   mixed getActiveRecords+  void getActiveRecordsClass
- string $tableName,​ +   string $class, 
- optional string ​ $where=false,​ +   string $tableName,​ 
- optional mixed[] $bindarr=false,​ +   ​optional string ​ $where=false,​ 
- optional mixed[] $primaryKeyArray=false +   ​optional mixed[] $bindarr=false,​ 
- )+   ​optional mixed[] $primaryKeyArray=false, 
 +   optional mixed[] $queryExtras=array(),​ 
 +   ​optional mixed[] $relationsData=array() 
 +  ​)
  
 </​WRAP>​ </​WRAP>​
-/** +
- * GetActiveRecordsClass Performs an '​ALL'​ query  +
- *  +
- * @param mixed $class This string represents the class of the current active record +
- * @param mixed $table Table used by the active record object +
- * @param mixed $whereOrderBy Where, order, by clauses +
- * @param mixed $bindarr  +
- * @param mixed $primkeyArr  +
- * @param array $extra Query extras: limit, offset... +
- * @param mixed $relations Associative array: table'​s foreign name, "​hasMany",​ "​belongsTo"​ +
- * @access public +
- * @return void +
- */ +
- function GetActiveRecordsClass( +
- $class, $table,​$whereOrderBy=false,​$bindarr=false,​ $primkeyArr=false,​ +
- $extra=array(),​ +
- $relations=array()) +
-+
- global $_ADODB_ACTIVE_DBS;​ +
- ## reduce overhead of adodb.inc.php -- moved to adodb-active-record.inc.php +
- ## if adodb-active-recordx is loaded -- should be no issue as they will probably use Find() +
- if (!isset($_ADODB_ACTIVE_DBS))include_once(ADODB_DIR.'/​adodb-active-record.inc.php'​);​ +
- return adodb_GetActiveRecordsClass($this,​ $class, $table, $whereOrderBy,​ $bindarr, $primkeyArr,​ $extra, $relations);​ +
-+
-  +
- function GetActiveRecords($table,​$where=false,​$bindarr=false,​$primkeyArr=false) +
-+
- $arr = $this->​GetActiveRecordsClass('​ADODB_Active_Record',​ $table, $where, $bindarr, $primkeyArr);​ +
- return $arr; +
- }+
 ===== Description ===== ===== Description =====
 +This allows you to retrieve an array of objects derived from ADOdb_Active_Records. Returns false if an error occurs.
 +
 +===== Parameters ===== 
 +====$class ====
 +This string represents the class of the current active record
 +==== $table ====
 +Table used by the active record object
 +==== $whereOrderBy====
 + ​Where,​ order, by clause
 +==== $bindarr ====
 +For more information on the use of ''​Bind''​ variables, see [[v5:​reference:​connection:​execute]]
 +==== $primaryKeyArray ====
 + 
 +==== $queryExtras ====
 +Query extras: limit, offset...
 +==== $relationsData ====
  
 ==== Usage ==== ==== Usage ====
 <code php> <code php>
-include ​'adodb/​adodb.inc.php'; +class Product extends ADOdb_Active_Record{};​ 
-include ​'adodb/adodb-activerecord.inc.php';+$table = 'products'; 
 +$whereOrderBy = "name LIKE 'A%' ORDER BY Name";​ 
 +$activeRecArr = $db->​getActiveRecordsClass('Product',​ 
 +                                            $table, 
 +                                            $whereOrderBy);
  
-$db = NewADOConnection('mysqli'); +/* 
-$db->connect('','​user','​password','​employees');+* the objects in $activeRecArr are of class 'Product' 
 +*/ 
 +foreach($activeRecArr as $rec) { 
 + $rec->​id = rand(); 
 + $rec->​save();​ 
 +
 +</​code>​ 
 + 
 +To use bind variables (assuming ? is the place-holder for your database):​ 
 +<code php> 
 + 
 +$activeRecArr = $db->getActiveRecordsClass($className,​$tableName, ​'name LIKE ?', 
 + array('A%')); 
 +/* 
 +* You can also define the primary keys of the table by passing an array of field names: 
 +*/ 
 + 
 +$activeRecArr = $db->​getActiveRecordsClass($className,​$tableName, 'name LIKE ?', 
 + array('A%'), array('​id'​));
  
-$activeRecs = $db->​GetActiveRecords('​departments'​);​ 
-            
 </​code>​ </​code>​
 +
 {{tag>​[Active_Record]}} ​       {{tag>​[Active_Record]}} ​      
v5/activerecord/getactiverecordsclass.1449264934.txt.gz · Last modified: 2017/04/21 11:21 (external edit)