ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:activerecord:getactiverecords

Differences

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

Link to this comparison view

Next revision
Previous revision
activerecord:getactiverecords [2015/12/04 22:35] – created mnewnhamv5:activerecord:getactiverecords [2018/07/02 14:19] (current) – prefer functions start lowercase (#430) peterdd
Line 2: Line 2:
 ~~NOTOC~~ ~~NOTOC~~
 <WRAP right box> <WRAP right box>
-== syntax == +== Syntax == 
-   mixed getActiveRecords( +  bool getActiveRecords( 
- string $tableName, +    string $tableName, 
- optional string  $where=false, +    optional string  $where=false, 
- optional mixed[] $bindarr=false, +    optional mixed[] $bindarr=false, 
- optional mixed[] $primaryKeyArray=false +    optional mixed[] $primaryKeyArray=false 
- )+  )
  
 </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 method retrieves an array of ADOdb_Active_Records.
 +===== Parameters ===== 
  
 +==== $tableName ====
 +Table used by the active record object
 +==== $where ====
 + Where clause
 +==== $bindarr ====
 +For more information on the use of ''Bind'' variables, see [[v5:reference:connection:execute]].
 +==== $primaryKeyArray ====
 + 
 ==== Usage ==== ==== Usage ====
 <code php> <code php>
Line 51: Line 32:
 $db->connect('','user','password','employees'); $db->connect('','user','password','employees');
  
-$activeRecs = $db->GetActiveRecords('departments'); +$table        = 'departments'; 
-           +$whereOrderBy = "name LIKE 'A%' ORDER BY Name"; 
 +$primaryKeys  = array('id'); 
 +$activeRecArr = $db->getActiveRecords($table, $whereOrderBy,$primaryKeys); 
 +foreach($activeRecArr as $rec)  
 +
 +    $rec->id = rand(); 
 +    $rec->save(); 
 +}            
 +</code> 
 + 
 +And to retrieve all records ordered by specific fields: 
 +<code php> 
 + 
 +$whereOrderBy = "1=1 ORDER BY Name"; 
 +$activeRecArr = $db->getActiveRecords($table); 
 +/* 
 +* To use bind variables (assuming ? is the place-holder for your database): 
 +*/ 
 + 
 +$activeRecArr = $db->getActiveRecords($tableName, 'name LIKE ?', 
 + array('A%')); 
 + 
 +/* 
 +Define the primary key field[s] of the table by passing an array of field names: 
 +*/ 
 +$activeRecArr = $db->getActiveRecords($tableName, 'name LIKE ?', 
 + array('A%'), array('id'));
 </code> </code>
 {{tag>[Active_Record]}}        {{tag>[Active_Record]}}       
v5/activerecord/getactiverecords.1449264917.txt.gz · Last modified: 2017/04/21 11:21 (external edit)