v5:userguide:active_record
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
userguide:active_record [2015/12/04 20:03] – mnewnham | v5:userguide:active_record [2019/05/08 16:32] (current) – [Setting the Table Name] ADODB_ASSOC_CASE not $ADODB_ASSOC_CASE dregad | ||
---|---|---|---|
Line 15: | Line 15: | ||
* Works with PHP4 and PHP5 and provides equivalent functionality in both versions of PHP. | * Works with PHP4 and PHP5 and provides equivalent functionality in both versions of PHP. | ||
* ADOdb_Active_Record works when you are connected to multiple databases. Zend's only works when connected to a default database. | * ADOdb_Active_Record works when you are connected to multiple databases. Zend's only works when connected to a default database. | ||
- | * Support for [[reference: | + | * Support for [[v5:reference:adodb_assoc_case]]. The field names are upper-cased, |
* No field name conversion to camel-caps style, unlike Zend's implementation which will convert field names such as ' | * No field name conversion to camel-caps style, unlike Zend's implementation which will convert field names such as ' | ||
* NewADOConnection:: | * NewADOConnection:: | ||
Line 152: | Line 152: | ||
We provide two ways to define your own table: | We provide two ways to define your own table: | ||
- | 1. Use a constructor parameter to override the default table naming behaviour. | + | - Use a constructor parameter to override the default table naming behaviour. |
- | + | class person extends ADOdb_Active_Record {} | |
- | class person extends ADOdb_Active_Record{} | + | $person = new person(' |
- | $person = new person(' | + | </ |
- | 2. Define it in a class declaration: | + | - Define it in a class declaration: |
+ | class person extends ADOdb_Active_Record | ||
+ | { | ||
+ | var $_table = ' | ||
+ | } | ||
+ | $person = new person(); | ||
+ | </ | ||
- | class person extends ADOdb_Active_Record | + | ==== ADODB_ASSOC_CASE |
- | { | + | |
- | var $_table | + | |
- | } | + | |
- | $person | + | |
- | $ADODB_ASSOC_CASE | + | |
- | This allows you to control the case of field names and properties. For example, all field names in Oracle | + | This allows you to control the case of field names and properties. For example, |
- | 0: lower-case | + | For further details on valid values and usage, see [[v5:reference:adodb_assoc_case]] reference. |
- | 1: upper-case | + | |
- | 2: native-case | + | |
- | So to force all Oracle field names to lower-case, use | + | |
- | $ADODB_ASSOC_CASE = 0; | + | <code php> |
+ | // ADODB_ASSOC_CASE == ADODB_ASSOC_CASE_LOWER | ||
$person = new person(' | $person = new person(' | ||
$person-> | $person-> | ||
- | $ADODB_ASSOC_CASE = 2; | + | |
+ | // ADODB_ASSOC_CASE == ADODB_ASSOC_CASE_UPPER | ||
$person2 = new person(' | $person2 = new person(' | ||
$person2-> | $person2-> | ||
- | Also see $ADODB_ASSOC_CASE. | + | </ |
- | ADOdb_Active_Record:: | + | ==== ADOdb_Active_Record:: |
Saves a record by executing an INSERT or UPDATE SQL statement as appropriate. | Saves a record by executing an INSERT or UPDATE SQL statement as appropriate. | ||
Line 188: | Line 188: | ||
Returns 0 on failed UPDATE, and 1 on UPDATE if data has changed, and -1 if no data was changed, so no UPDATE statement was executed. | Returns 0 on failed UPDATE, and 1 on UPDATE if data has changed, and -1 if no data was changed, so no UPDATE statement was executed. | ||
- | ADOdb_Active_Record:: | + | ==== ADOdb_Active_Record:: |
ADOdb supports replace functionality, | ADOdb supports replace functionality, | ||
+ | <code php> | ||
$rec = new ADOdb_Active_Record(" | $rec = new ADOdb_Active_Record(" | ||
$rec-> | $rec-> | ||
Line 197: | Line 197: | ||
$ok = $rec-> | $ok = $rec-> | ||
ADOdb_Active_Record:: | ADOdb_Active_Record:: | ||
+ | </ | ||
Sometimes, we want to load a single record into an Active Record. We can do so using: | Sometimes, we want to load a single record into an Active Record. We can do so using: | ||
+ | <code php> | ||
$person-> | $person-> | ||
Line 206: | Line 208: | ||
$person-> | $person-> | ||
Returns false if an error occurs. | Returns false if an error occurs. | ||
+ | </ | ||
- | ADOdb_Active_Record:: | + | ==== ADOdb_Active_Record:: |
We want to retrieve an array of active records based on some search criteria. For example: | We want to retrieve an array of active records based on some search criteria. For example: | ||
+ | <code php> | ||
class person extends ADOdb_Active_Record { | class person extends ADOdb_Active_Record { | ||
- | var $_table = ' | + | |
} | } | ||
$person = new person(); | $person = new person(); | ||
$peopleArray = $person-> | $peopleArray = $person-> | ||
- | Quoting Identifiers | + | </ |
+ | |||
+ | ===== Quoting Identifiers | ||
You can force column names to be quoted in INSERT and UPDATE statements, typically because you are using reserved words as column names by setting | You can force column names to be quoted in INSERT and UPDATE statements, typically because you are using reserved words as column names by setting | ||
- | ADODB_Active_Record:: | + | |
+ | | ||
Default is false. | Default is false. | ||
- | Error Handling and Debugging | + | ===== Error Handling and Debugging |
In PHP5, if adodb-exceptions.inc.php is included, then errors are thrown. Otherwise errors are handled by returning a value. False by default means an error has occurred. You can get the last error message using the ErrorMsg() function. | In PHP5, if adodb-exceptions.inc.php is included, then errors are thrown. Otherwise errors are handled by returning a value. False by default means an error has occurred. You can get the last error message using the ErrorMsg() function. | ||
To check for errors in ADOdb_Active_Record, | To check for errors in ADOdb_Active_Record, | ||
+ | <code php> | ||
# right! | # right! | ||
$ok = $rec-> | $ok = $rec-> | ||
Line 237: | Line 243: | ||
$rec-> | $rec-> | ||
if ($rec-> | if ($rec-> | ||
+ | |||
+ | </ | ||
The ADOConnection:: | The ADOConnection:: | ||
Line 243: | Line 251: | ||
You can convert an array to an ADOdb_Active_Record using Set(). The array must be numerically indexed, and have all fields of the table defined in the array. The elements of the array must be in the table' | You can convert an array to an ADOdb_Active_Record using Set(). The array must be numerically indexed, and have all fields of the table defined in the array. The elements of the array must be in the table' | ||
+ | <code php> | ||
$row = $db-> | $row = $db-> | ||
Line 262: | Line 271: | ||
echo $e-> | echo $e-> | ||
} | } | ||
- | Primary Keys | + | </ |
+ | |||
+ | ===== Primary Keys ===== | ||
ADOdb_Active_Record does not require the table to have a primary key. You can insert records for such a table, but you will not be able to update nor delete. | ADOdb_Active_Record does not require the table to have a primary key. You can insert records for such a table, but you will not be able to update nor delete. | ||
Line 268: | Line 279: | ||
Sometimes you are retrieving data from a view or table that has no primary key, but has a unique index. You can dynamically set the primary key of a table through the constructor: | Sometimes you are retrieving data from a view or table that has no primary key, but has a unique index. You can dynamically set the primary key of a table through the constructor: | ||
- | $pkeys = array(' | + | <code php> |
+ | $pkeys = array(' | ||
- | // set primary key using constructor | + | // set primary key using constructor |
- | $rec = new ADOdb_Active_Record(' | + | $rec = new ADOdb_Active_Record(' |
+ | // or define a new class | ||
+ | class Product extends ADOdb_Active_Record { | ||
+ | function __construct() | ||
+ | { | ||
+ | parent:: | ||
+ | } | ||
+ | } | ||
- | // or define a new class | + | $rec = new Product(); |
- | class Product extends ADOdb_Active_Record { | + | </ |
- | function __construct() | + | ===== Retrieval of Auto-incrementing ID ===== |
- | { | + | |
- | parent:: | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | $rec = new Product(); | + | |
- | Retrieval of Auto-incrementing ID | + | |
When creating a new record, the retrieval of the last auto-incrementing ID is not reliable for databases that do not support the Insert_ID() function call (check $connection-> | When creating a new record, the retrieval of the last auto-incrementing ID is not reliable for databases that do not support the Insert_ID() function call (check $connection-> | ||
Line 288: | Line 300: | ||
Sometimes we want to load data from one database and insert it into another using ActiveRecords. This can be done using the optional parameter of the ADOdb_Active_Record constructor. In the following example, we read data from db.table1 and store it in db2.table2: | Sometimes we want to load data from one database and insert it into another using ActiveRecords. This can be done using the optional parameter of the ADOdb_Active_Record constructor. In the following example, we read data from db.table1 and store it in db2.table2: | ||
+ | <code php> | ||
$db = NewADOConnection(...); | $db = NewADOConnection(...); | ||
$db2 = NewADOConnection(...); | $db2 = NewADOConnection(...); | ||
Line 297: | Line 309: | ||
foreach($activeRecs as $rec) { | foreach($activeRecs as $rec) { | ||
- | $rec2 = new ADOdb_Active_Record(' | + | $rec2 = new ADOdb_Active_Record(' |
- | $rec2-> | + | |
- | $rec2-> | + | |
- | $rec2-> | + | |
} | } | ||
+ | </ | ||
If you have to pass in a primary key called " | If you have to pass in a primary key called " | ||
$rec = new ADOdb_Active_Record(" | $rec = new ADOdb_Active_Record(" | ||
- | You can now give a named label in SetDatabaseAdapter, | ||
+ | You can now give a named label in SetDatabaseAdapter, | ||
+ | <code php> | ||
$db1 = NewADOConnection(...); | $db1 = NewADOConnection(...); | ||
ADOdb_Active_Record:: | ADOdb_Active_Record:: | ||
Line 318: | Line 332: | ||
... | ... | ||
} | } | ||
- | $ADODB_ACTIVE_CACHESECS | + | |
+ | </ | ||
+ | |||
+ | ===== $ADODB_ACTIVE_CACHESECS | ||
You can cache the table metadata (field names, types, and other info such primary keys) in $ADODB_CACHE_DIR (which defaults to /tmp) by setting the global variable $ADODB_ACTIVE_CACHESECS to a value greater than 0. This will be the number of seconds to cache. You should set this to a value of 30 seconds or greater for optimal performance. | You can cache the table metadata (field names, types, and other info such primary keys) in $ADODB_CACHE_DIR (which defaults to /tmp) by setting the global variable $ADODB_ACTIVE_CACHESECS to a value greater than 0. This will be the number of seconds to cache. You should set this to a value of 30 seconds or greater for optimal performance. | ||
- | Active Record Considered Bad? | + | ===== Active Record Considered Bad? ===== |
Although the Active Record concept is useful, you have to be aware of some pitfalls when using Active Record. The level of granularity of Active Record is individual records. It encourages code like the following, used to increase the price of all furniture products by 10%: | Although the Active Record concept is useful, you have to be aware of some pitfalls when using Active Record. The level of granularity of Active Record is individual records. It encourages code like the following, used to increase the price of all furniture products by 10%: | ||
- | $recs = $db-> | + | <code php> |
- | | + | $recs = $db-> |
+ | foreach($recs as $rec) { | ||
$rec-> | $rec-> | ||
$rec-> | $rec-> | ||
- | } | + | } |
+ | </ | ||
Of course an UPDATE statement is superior because it's simpler and much more efficient (probably by a factor of x10 or more): | Of course an UPDATE statement is superior because it's simpler and much more efficient (probably by a factor of x10 or more): | ||
- | $db-> | + | <code php> |
+ | $db-> | ||
+ | </ | ||
For performance sensitive code, using direct SQL will always be faster than using Active Records due to overhead and the fact that all fields in a row are retrieved (rather than only the subset you need) whenever an Active Record is loaded. | For performance sensitive code, using direct SQL will always be faster than using Active Records due to overhead and the fact that all fields in a row are retrieved (rather than only the subset you need) whenever an Active Record is loaded. | ||
- | Transactions | + | ===== Transactions |
The default transaction mode in ADOdb is autocommit. So that is the default with active record too. The general rules for managing transactions still apply. Active Record to the database is a set of insert/ | The default transaction mode in ADOdb is autocommit. So that is the default with active record too. The general rules for managing transactions still apply. Active Record to the database is a set of insert/ | ||
Line 341: | Line 364: | ||
Smart transactions, | Smart transactions, | ||
+ | <code php> | ||
$conn-> | $conn-> | ||
$parent-> | $parent-> | ||
$child-> | $child-> | ||
$conn-> | $conn-> | ||
- | One to Many Relations | + | </ |
+ | |||
+ | ===== One to Many Relations | ||
Since ADOdb 5.06, we support parent child relationships. This is done using the ClassBelongsTo() and ClassHasMany() functions. | Since ADOdb 5.06, we support parent child relationships. This is done using the ClassBelongsTo() and ClassHasMany() functions. | ||
- | ClassHasMany | + | ===== ClassHasMany |
- | To globally define a one-to-many relationship we use the static function ADODB_Active_Record:: | + | To globally define a one-to-many relationship we use the static function ADODB_Active_Record:: |
- | class person extends ADOdb_Active_Record{} | + | <code php> |
- | ADODB_Active_Record:: | + | class person extends ADOdb_Active_Record{} |
+ | ADODB_Active_Record:: | ||
- | $person = new person(); | + | $person = new person(); |
- | $person-> | + | $person-> |
- | foreach($person-> | + | foreach($person-> |
- | echo " $c-> | + | echo " $c-> |
- | $c-> | + | $c-> |
- | $c-> | + | $c-> |
- | } | + | } |
+ | </ | ||
If no data is loaded, then children is set to an empty array: | If no data is loaded, then children is set to an empty array: | ||
- | $person2 = new person(); | + | <code php> |
- | $p = $person2-> | + | $person2 = new person(); |
- | By default, data returned by HasMany() is unsorted. To define an order by clause (or define a SELECT LIMIT window), see LoadRelations() below. Another point is that all children are loaded only when the child member is accessed (in __get), and not when the Load() function of the parent object is called. This helps to conserve memory. | + | $p = $person2-> |
+ | </ | ||
+ | |||
+ | By default, data returned by HasMany() is unsorted. To define an order by clause (or define a SELECT LIMIT window), see LoadRelations() below. Another point is that all children are loaded only when the child member is accessed (in < | ||
To create and save new parent and child records: | To create and save new parent and child records: | ||
+ | <code php> | ||
+ | class person extends ADOdb_Active_Record{} | ||
+ | class children extends ADOdb_Active_Record{} | ||
+ | ADODB_Active_Record:: | ||
- | class | + | $person |
- | class children extends ADOdb_Active_Record{} | + | |
- | ADODB_Active_Record:: | + | |
- | $person = new person(); | + | for ($i=0; $i<10; $i++) |
+ | | ||
- | for ($i=0; $i<10; $i++) | + | // modify fields of $person, then... |
- | $person-> | + | $person-> |
- | + | ||
- | // modify fields of $person, then... | + | |
- | $person-> | + | |
- | foreach($person-> | + | foreach($person-> |
- | // modify fields of $c then... | + | // modify fields of $c then... |
- | $c-> | + | $c-> |
- | } | + | } |
+ | </ | ||
You can have multiple relationships (warning: relations are case-sensitive, | You can have multiple relationships (warning: relations are case-sensitive, | ||
- | ADODB_Active_Record:: | + | <code php> |
- | ADODB_Active_Record:: | + | ADODB_Active_Record:: |
- | $person = new person(); | + | ADODB_Active_Record:: |
- | $person-> | + | $person = new person(); |
- | var_dump($person-> | + | $person-> |
- | var_dump($person-> | + | var_dump($person-> |
- | By default, the child class is ADOdb_Active_Record. Sometimes you might want the child class to be based on your own class which has additional functions. You can do so using the last parameter: | + | var_dump($person-> |
+ | </ | ||
- | class person extends ADOdb_Active_Record{} | + | By default, the child class is ADOdb_Active_Record. Sometimes you might want the child class to be based on your own class which has additional functions. You can do so using the last parameter: |
- | class child extends ADOdb_Active_Record { .... some modifications here ... } | + | <code php> |
- | ADODB_Active_Record:: | + | class person extends ADOdb_Active_Record{} |
- | Lastly some troubleshooting issues. We use the __get() method to set $p-> | + | class child extends ADOdb_Active_Record { .... some modifications here ... } |
- | + | ADODB_Active_Record:: | |
- | ADODB_Active_Record:: | + | </ |
- | $p = new person(); | + | Lastly some troubleshooting issues. We use the <code php>__get()</ |
- | $p-> | + | <code php> |
- | # $p-> | + | ADODB_Active_Record:: |
- | var_dump($p-> | + | $p = new person(); |
- | + | $p-> | |
- | $p-> | + | # $p-> |
- | # $p-> | + | var_dump($p-> |
- | var_dump($p-> | + | $p-> |
+ | # $p-> | ||
+ | var_dump($p-> | ||
+ | </ | ||
The solution to the above is to unset($p-> | The solution to the above is to unset($p-> | ||
- | TableHasMany | + | ===== TableHasMany |
For some classes, the mapping between class name and table name (which is the pluralised version) might not match. For example, the class name might be person, but the table name might be people. So we have 2 tables, people (parent table) and children (child table) linked by people.id = children.person_id. | For some classes, the mapping between class name and table name (which is the pluralised version) might not match. For example, the class name might be person, but the table name might be people. So we have 2 tables, people (parent table) and children (child table) linked by people.id = children.person_id. | ||
- | Then you use the following static function ADODB_Active_Record:: | + | Then you use the following static function ADODB_Active_Record:: |
ADODB_Active_Record:: | ADODB_Active_Record:: | ||
- | TableKeyHasMany | + | |
+ | ===== TableKeyHasMany | ||
For some classes, the mapping between class name and table name (which is the pluralised version) might not match or the primary key is not the default id. For example, the class name might be person, but the table name might be people. So we have 2 tables, people (parent table) and children (child table) linked by people.pid = children.person_id. | For some classes, the mapping between class name and table name (which is the pluralised version) might not match or the primary key is not the default id. For example, the class name might be person, but the table name might be people. So we have 2 tables, people (parent table) and children (child table) linked by people.pid = children.person_id. | ||
- | Then you use the following static function ADODB_Active_Record:: | + | |
+ | Then you use the following static function ADODB_Active_Record:: | ||
ADODB_Active_Record:: | ADODB_Active_Record:: | ||
Line 429: | Line 467: | ||
Here is sample usage using mysql: | Here is sample usage using mysql: | ||
+ | <code php> | ||
+ | include_once(' | ||
+ | include_once(' | ||
- | include_once('../adodb.inc.php'); | + | $db = NewADOConnection('mysql:// |
- | include_once(' | + | ADOdb_Active_Record:: |
- | $db = NewADOConnection('mysql:// | + | $db-> |
- | ADOdb_Active_Record:: | + | `id` int(10) unsigned NOT NULL auto_increment, |
+ | `name_first` varchar(100) NOT NULL default | ||
+ | `name_last` varchar(100) NOT NULL default '', | ||
+ | | ||
+ | PRIMARY KEY (`id`) | ||
+ | ) ENGINE=MyISAM; | ||
+ | " | ||
- | $db-> | + | $db-> |
- | | + | `id` int(10) unsigned NOT NULL auto_increment, |
- | | + | `person_id` int(10) unsigned NOT NULL, |
- | | + | `gender` varchar(10) default ' |
- | | + | |
- | | + | `name_last` varchar(100) NOT NULL default '', |
- | | + | `favorite_pet` varchar(100) NOT NULL default '', |
- | "); | + | PRIMARY KEY (`id`) |
+ | ) ENGINE=MyISAM; | ||
+ | | ||
- | $db-> | + | $db-> |
- | `id` int(10) unsigned NOT NULL auto_increment, | + | $db-> |
- | `person_id` int(10) unsigned NOT NULL, | + | $db-> |
- | `gender` varchar(10) default | + | |
- | `name_first` varchar(100) NOT NULL default | + | |
- | `name_last` varchar(100) NOT NULL default | + | |
- | `favorite_pet` varchar(100) NOT NULL default | + | |
- | PRIMARY KEY (`id`) | + | |
- | ) ENGINE=MyISAM; | + | |
- | "); | + | |
- | $db-> | + | class person extends ADOdb_Active_Record{} |
- | $db-> | + | ADODB_Active_Record:: |
- | $db-> | + | |
- | class | + | $person = new person(); |
- | ADODB_Active_Record:: | + | $person-> |
+ | $person-> | ||
+ | $person-> | ||
+ | $person-> | ||
- | $person | + | $person2 |
+ | $person2-> | ||
- | $person->name_first | + | $c = $person2->children; |
- | $person->name_last | + | if (is_array($c) && sizeof($c) == 3 && $c[0]->name_first=='Jill' |
- | $person->favorite_color | + | && |
- | $person->save(); // this save will perform an INSERT successfully | + | else { |
- | + | echo "Error loading hasMany should have 3 array elements Jill Joan Jamie< | |
- | $person2 = new person(); # no need to define HasMany() again, adodb remembers definition | + | } |
- | $person2-> | + | |
- | $c = $person2-> | + | ===== HasMany ===== |
- | if (is_array($c) && sizeof($c) | + | |
- | && | + | |
- | else { | + | |
- | echo "Error loading hasMany should have 3 array elements Jill Joan Jamie< | + | |
- | } | + | |
- | HasMany | + | |
This older method is deprecated and ClassHasMany/ | This older method is deprecated and ClassHasMany/ | ||
- | The older way to define a one-to-many relationship is to use $parentobj-> | + | The older way to define a one-to-many relationship is to use $parentobj-> |
+ | <code php> | ||
+ | class person extends ADOdb_Active_Record{} | ||
- | class | + | $person |
+ | $person-> | ||
+ | $person-> | ||
+ | foreach($person-> | ||
+ | echo " $c-> | ||
+ | $c-> | ||
+ | $c-> | ||
+ | } | ||
+ | </ | ||
- | $person = new person(); | ||
- | $person-> | ||
- | $person-> | ||
- | foreach($person-> | ||
- | echo " $c-> | ||
- | $c-> | ||
- | $c-> | ||
- | } | ||
This HasMany() definition is global for the current script. This means that you only need to define it once. In the following example, $person2 knows about children. | This HasMany() definition is global for the current script. This means that you only need to define it once. In the following example, $person2 knows about children. | ||
+ | <code php> | ||
+ | $person = new person(); | ||
+ | $person-> | ||
- | $person = new person(); | + | $person2 = new person(); |
- | $person-> | + | $person-> |
- | + | $p = $person2-> | |
- | $person2 = new person(); | + | </ |
- | $person-> | + | ===== ClassBelongsTo |
- | $p = $person2-> | + | |
- | ClassBelongsTo | + | |
You can define the parent of the current object using ADODB_Active_Record:: | You can define the parent of the current object using ADODB_Active_Record:: | ||
Line 518: | Line 559: | ||
Also if no data is loaded into the child instance, then $p will return null; | Also if no data is loaded into the child instance, then $p will return null; | ||
+ | <code php> | ||
+ | ADODB_Active_Record:: | ||
- | ADODB_Active_Record:: | + | $ch = new kid(); |
- | + | $p = $ch-> | |
- | $ch = new kid(); | + | </ |
- | $p = $ch-> | + | |
Another way to define the class of the parent (which otherwise defaults to ADODB_Active_Record) as follows: | Another way to define the class of the parent (which otherwise defaults to ADODB_Active_Record) as follows: | ||
+ | <code php> | ||
+ | class kid extends ADOdb_Active_Record{}; | ||
+ | class person extends ADOdb_Active_Record{... your modifications ... }; | ||
+ | ADODB_Active_Record:: | ||
+ | </ | ||
- | class kid extends ADOdb_Active_Record{}; | + | ===== TableBelongsTo |
- | class person extends ADOdb_Active_Record{... your modifications ... }; | + | |
- | ADODB_Active_Record:: | + | |
- | TableBelongsTo | + | |
If the child table differs from the convention that the child table name is the plural of the child class name, use this function: ADODB_Active_Record:: | If the child table differs from the convention that the child table name is the plural of the child class name, use this function: ADODB_Active_Record:: | ||
Line 535: | Line 579: | ||
E.g. the class is child, but the table name is children, and the link between the two tables is children.person_id = person.id: | E.g. the class is child, but the table name is children, and the link between the two tables is children.person_id = person.id: | ||
- | ADODB_Active_Record:: | + | ADODB_Active_Record:: |
- | TableKeyBelongsTo | + | |
+ | ===== TableKeyBelongsTo | ||
If the child table differs from the convention that the child table name is the plural of the child class name or the primary key is not ' | If the child table differs from the convention that the child table name is the plural of the child class name or the primary key is not ' | ||
Line 542: | Line 587: | ||
E.g. the class is child, but the table name is children and primary key is ch_id, and the link between the two tables is children.person_id = person.id: | E.g. the class is child, but the table name is children and primary key is ch_id, and the link between the two tables is children.person_id = person.id: | ||
- | ADODB_Active_Record:: | + | ADODB_Active_Record:: |
- | BelongsTo | + | |
+ | ===== BelongsTo | ||
The following is deprecated. Use ClassBelongsTo/ | The following is deprecated. Use ClassBelongsTo/ | ||
The older way to define the parent of the current object is using BelongsTo($relationName, | The older way to define the parent of the current object is using BelongsTo($relationName, | ||
- | + | < | |
- | class Child extends ADOdb_Active_Record{}; | + | class Child extends ADOdb_Active_Record{}; |
- | $ch = new Child(' | + | $ch = new Child(' |
- | $ch-> | + | $ch-> |
- | | + | ## as foreign key defaults to $table.' |
- | | + | ## parent pkey defaults to ' |
- | $ch-> | + | $ch-> |
- | $p = $ch-> | + | $p = $ch-> |
- | if (!$p || $p-> | + | if (!$p || $p-> |
- | else echo "OK loading BelongTo< | + | else echo "OK loading BelongTo< |
+ | </ | ||
You only need to define BelongsTo() once in a script as it is global for all instances. | You only need to define BelongsTo() once in a script as it is global for all instances. | ||
- | LoadRelations | + | ===== LoadRelations ===== |
- | + | ||
- | Sometimes you want to load only a subset of data in a relationship. For example, you could load all female children sorted by children.name using LoadRelations($relation, | + | |
- | # assume this has been called: | + | Sometimes you want to load only a subset of data in a relationship. For example, you could load all female children sorted by children.name using LoadRelations($relation, |
- | # | + | <code php> |
- | $person = new person(); | + | # assume this has been called: |
- | $person-> | + | # |
- | # Load doesn' | + | $person = new person(); |
- | $person-> | + | $person-> |
+ | # Load doesn' | ||
+ | $person-> | ||
+ | </ | ||
Lastly, if you have lots of child data, you can define a window of data of records to load. In the following example, we load a window of 100 records at a time: | Lastly, if you have lots of child data, you can define a window of data of records to load. In the following example, we load a window of 100 records at a time: | ||
+ | <code php> | ||
+ | # assume this has been called: | ||
+ | # ADODB_Active_Record:: | ||
+ | #acc = new Account(); | ||
+ | $acc-> | ||
- | # assume this has been called: | + | $start = 0; |
- | # ADODB_Active_Record:: | + | while(true) { |
- | $acc = new Account(); | + | $acc-> |
- | $acc-> | + | if (!$acc-> |
- | + | foreach ($acc-> | |
- | $start = 0; | + | ## process |
- | while(true) { | + | $trx-> |
- | $acc-> | + | $trx-> |
- | if (!$acc-> | + | } |
- | foreach ($acc-> | + | $start += 100; |
- | ## process | + | unset($acc-> |
- | $trx-> | + | |
- | $trx-> | + | |
- | } | + | |
- | $start += 100; | + | |
- | unset($acc-> | + | |
- | + | } | |
- | } | + | </ |
The $offset is 0-based, and $limit is the number of records to retrieve. The default is to ignore $offset (-1) and $limit (-1). | The $offset is 0-based, and $limit is the number of records to retrieve. The default is to ignore $offset (-1) and $limit (-1). | ||
- | Acknowledgements | + | ===== Acknowledgements |
Thanks to Chris Ravenscroft for original one-to-many code (chris# | Thanks to Chris Ravenscroft for original one-to-many code (chris# | ||
- | ADOConnection Supplement | + | ===== ADOConnection Supplement |
- | ADOConnection:: | + | ==== ADOConnection:: |
This allows you to retrieve an array of ADOdb_Active_Records. Returns false if an error occurs. | This allows you to retrieve an array of ADOdb_Active_Records. Returns false if an error occurs. | ||
+ | <code php> | ||
$table = ' | $table = ' | ||
$whereOrderBy = "name LIKE ' | $whereOrderBy = "name LIKE ' | ||
Line 611: | Line 660: | ||
$rec-> | $rec-> | ||
} | } | ||
+ | </ | ||
And to retrieve all records ordered by specific fields: | And to retrieve all records ordered by specific fields: | ||
+ | <code php> | ||
$whereOrderBy = "1=1 ORDER BY Name"; | $whereOrderBy = "1=1 ORDER BY Name"; | ||
$activeRecArr = $db-> | $activeRecArr = $db-> | ||
- | To use bind variables (assuming ? is the place-holder for your database): | + | //To use bind variables (assuming ? is the place-holder for your database): |
$activeRecArr = $db-> | $activeRecArr = $db-> | ||
Line 624: | Line 674: | ||
array(' | array(' | ||
ADOConnection:: | ADOConnection:: | ||
+ | </ | ||
This allows you to retrieve an array of objects derived from ADOdb_Active_Records. Returns false if an error occurs. | This allows you to retrieve an array of objects derived from ADOdb_Active_Records. Returns false if an error occurs. | ||
+ | <code php> | ||
class Product extends ADOdb_Active_Record{}; | class Product extends ADOdb_Active_Record{}; | ||
$table = ' | $table = ' | ||
Line 637: | Line 688: | ||
$rec-> | $rec-> | ||
} | } | ||
+ | </ | ||
To use bind variables (assuming ? is the place-holder for your database): | To use bind variables (assuming ? is the place-holder for your database): | ||
+ | <code php> | ||
$activeRecArr = $db-> | $activeRecArr = $db-> | ||
array(' | array(' | ||
Line 645: | Line 697: | ||
$activeRecArr = $db-> | $activeRecArr = $db-> | ||
array(' | array(' | ||
+ | |||
+ | </ | ||
ADOConnection:: | ADOConnection:: | ||
Line 655: | Line 709: | ||
===== ActiveRecord Code Sample ===== | ===== ActiveRecord Code Sample ===== | ||
- | <?php | + | <code php> |
include(' | include(' | ||
include(' | include(' | ||
Line 959: | Line 1013: | ||
PHP5 specific: Make GetActiveRecords*() return an Iterator. | PHP5 specific: Make GetActiveRecords*() return an Iterator. | ||
- | PHP5 specific: Change PHP5 implementation of Active Record to use __get() and __set() for better performance. | + | PHP5 specific: Change PHP5 implementation of Active Record to use <code php>__get() and __set()</ |
v5/userguide/active_record.1449255828.txt.gz · Last modified: 2017/04/21 11:39 (external edit)