v5:activerecord:active_record_tutorial
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
activerecord:active_record_tutorial [2015/12/08 05:12] – [ClassBelongsTo] mnewnham | v5:activerecord:active_record_tutorial [2019/05/08 16:39] – ADODB_ASSOC_CASE not $ADODB_ASSOC_CASE dregad | ||
---|---|---|---|
Line 6: | Line 6: | ||
</ | </ | ||
===== Overview ===== | ===== Overview ===== | ||
- | This tutorial shows basic usage of the ADODB Active Record Feature. It relies on a MySQL database, and each script needs to include the code shown [[activerecord: | + | This tutorial shows basic usage of the ADODB Active Record Feature. It relies on a MySQL database, and each script needs to include the code shown [[v5:activerecord: |
Line 34: | Line 34: | ||
" | " | ||
| | ||
- | Each of these fields is now a property of the $person object. To see all these properties, use the [[activerecord: | + | Each of these fields is now a property of the $person object. To see all these properties, use the [[v5:activerecord: |
<code php> | <code php> | ||
Line 58: | Line 58: | ||
<WRAP right box round> | <WRAP right box round> | ||
== See Also == | == See Also == | ||
- | [[userguide: | + | [[v5:userguide: |
</ | </ | ||
===== Inserting and Updating a Record ===== | ===== Inserting and Updating a Record ===== | ||
Line 64: | Line 64: | ||
An ADOdb_Active_Record object is a representation of a single table row. However, when our $person object is instantiated, | An ADOdb_Active_Record object is a representation of a single table row. However, when our $person object is instantiated, | ||
- | To insert a new record into the database, change the object' | + | To insert a new record into the database, change the object' |
<code php> | <code php> | ||
Line 78: | Line 78: | ||
1048: Column ' | 1048: Column ' | ||
- | This error occurred because MySQL rejected the INSERT | + | This error occurred because MySQL rejected the INSERT |
To insert a new ADOdb_Active_Record in the database, populate all of ADOdb_Active_Record' | To insert a new ADOdb_Active_Record in the database, populate all of ADOdb_Active_Record' | ||
Line 105: | Line 105: | ||
</ | </ | ||
- | From this point on, updating it is simply a matter of changing the object' | + | From this point on, updating it is simply a matter of changing the object' |
<code php> | <code php> | ||
Line 132: | Line 132: | ||
$person = new person(); | $person = new person(); | ||
- | ===== Using $ADODB_ASSOC_CASE ==== | + | ===== Using ADODB_ASSOC_CASE ==== |
- | You can use [[reference: | + | |
+ | You can define the [[v5:reference:adodb_assoc_case]] constant | ||
<code php> | <code php> | ||
- | $ADODB_ASSOC_CASE | + | define(' |
+ | require " | ||
$person = new person(' | $person = new person(' | ||
$person-> | $person-> | ||
- | $ADODB_ASSOC_CASE | + | |
+ | define(' | ||
+ | require " | ||
$person2 = new person(' | $person2 = new person(' | ||
$person2-> | $person2-> | ||
Line 146: | Line 150: | ||
===== Saving Records ==== | ===== Saving Records ==== | ||
- | The method [[activerecord: | + | The method [[v5:activerecord: |
==== Replacing Records ==== | ==== Replacing Records ==== | ||
- | The method [[activerecord: | + | The method [[v5:activerecord: |
<code php> | <code php> | ||
$rec = new ADOdb_Active_Record(" | $rec = new ADOdb_Active_Record(" | ||
Line 162: | Line 166: | ||
<WRAP right box 200px> | <WRAP right box 200px> | ||
== More Information == | == More Information == | ||
- | For more information on bind variables, see [[reference: | + | For more information on bind variables, see [[v5:reference:connection:execute]] |
</ | </ | ||
===== Loading Individual Records ===== | ===== Loading Individual Records ===== | ||
- | Sometimes, we want to load a single record into an Active Record using the [[activerecord: | + | Sometimes, we want to load a single record into an Active Record using the [[v5:activerecord: |
<code php> | <code php> | ||
Line 175: | Line 179: | ||
====== Loading Record Sets ====== | ====== Loading Record Sets ====== | ||
- | An array of active records based on some search criteria can be loaded using the [[activerecord: | + | An array of active records based on some search criteria can be loaded using the [[v5:activerecord: |
<code php> | <code php> | ||
Line 187: | Line 191: | ||
===== Quoting Identifiers ===== | ===== Quoting Identifiers ===== | ||
- | You can force column names to be quoted by use of the [[activerecord: | + | You can force column names to be quoted by use of the [[v5:activerecord: |
===== Error Handling and Debugging ===== | ===== Error Handling and Debugging ===== | ||
- | The ADOdb Active Record can be used in conjunction with [[userguide: | + | The ADOdb Active Record can be used in conjunction with [[v5:userguide: |
<code php> | <code php> | ||
Line 200: | Line 204: | ||
</ | </ | ||
- | The ADOConnection:: | + | The ADOConnection:: |
===== Converting a recordset to an Active Record object ===== | ===== Converting a recordset to an Active Record object ===== | ||
- | You can convert an array to an ADOdb_Active_Record using [[activerecord: | + | You can convert an array to an ADOdb_Active_Record using [[v5:activerecord: |
<code php> | <code php> | ||
Line 250: | Line 254: | ||
===== Retrieval of Auto-incrementing ID ===== | ===== 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 [[reference: | + | When creating a new record, the retrieval of the last auto-incrementing ID is not reliable for databases that do not support the [[v5:reference:connection: |
SELECT MAX($primarykey) FROM $table | SELECT MAX($primarykey) FROM $table | ||
| | ||
- | which will not work reliably in a multi-user environment. You can override the [[activerecord: | + | which will not work reliably in a multi-user environment. You can override the [[v5:activerecord: |
===== Dealing with Multiple Databases ===== | ===== Dealing with Multiple Databases ===== | ||
Line 280: | Line 284: | ||
$rec = new ADOdb_Active_Record(" | $rec = new ADOdb_Active_Record(" | ||
- | You can now give a named label in [[activerecord: | + | You can now give a named label in [[v5:activerecord: |
<code php> | <code php> | ||
Line 318: | Line 322: | ||
===== Transactions ===== | ===== Transactions ===== | ||
<WRAP right box 200px> | <WRAP right box 200px> | ||
- | More about [[reference: | + | More about [[v5:reference:reference_index# |
</ | </ | ||
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 337: | Line 341: | ||
===== ClassHasMany ===== | ===== ClassHasMany ===== | ||
- | To globally define a one-to-many relationship we use the static function | + | To globally define a one-to-many relationship we use the static function |
<code php> | <code php> | ||
Line 408: | Line 412: | ||
var_dump($p-> | var_dump($p-> | ||
</ | </ | ||
- | The solution to the above is to unset($p-> | + | The solution to the above is to |
+ | | ||
+ | before | ||
+ | | ||
===== TableHasMany ===== | ===== TableHasMany ===== | ||
Line 414: | Line 421: | ||
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 [[activerecord: | + | Then you use the following static function [[v5:activerecord: |
ADODB_Active_Record:: | ADODB_Active_Record:: | ||
Line 420: | Line 427: | ||
===== 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. This issue can be managed with the [[activerecord: | + | 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. This issue can be managed with the [[v5:activerecord: |
Here is sample usage using mysql: | Here is sample usage using mysql: | ||
<code php> | <code php> | ||
- | $db-> | + | $db-> |
- | $db-> | + | |
- | $db-> | + | $db-> |
+ | values (1,' | ||
+ | $db-> | ||
+ | values (1,' | ||
class person extends ADOdb_Active_Record{} | class person extends ADOdb_Active_Record{} | ||
Line 438: | Line 448: | ||
$person-> | $person-> | ||
- | $person2 = new person(); # no need to define HasMany() again, adodb remembers definition | + | /* |
+ | * no need to define HasMany() again, adodb remembers definition | ||
+ | */ | ||
+ | $person2 = new person(); | ||
$person2-> | $person2-> | ||
$c = $person2-> | $c = $person2-> | ||
- | if (is_array($c) && sizeof($c) == 3 && $c[0]-> | + | if (is_array($c) |
- | && $c[2]-> | + | && sizeof($c) == 3 |
+ | && $c[0]-> | ||
+ | && $c[1]-> | ||
+ | && $c[2]-> | ||
+ | | ||
else { | else { | ||
- | echo "Error loading hasMany should have 3 array elements Jill Joan Jamie< | + | echo "Error loading hasMany should have |
+ | | ||
} | } | ||
</ | </ | ||
Line 483: | Line 501: | ||
===== ClassBelongsTo ===== | ===== ClassBelongsTo ===== | ||
- | You can define the parent of the current object using [[activerecord: | + | You can define the parent of the current object using [[v5:activerecord: |
<code php> | <code php> | ||
Line 519: | Line 537: | ||
===== TableBelongsTo ===== | ===== 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: [[activerecord: | + | If the child table differs from the convention that the child table name is the plural of the child class name, use this function: [[v5:activerecord: |
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: | ||
Line 527: | Line 545: | ||
===== 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 ' |
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: | ||
Line 535: | Line 553: | ||
===== 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 [[activerecord: | + | 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 [[v5:activerecord: |
<code php> | <code php> | ||
Line 580: | Line 598: | ||
- | All of the code snippets above have been pulled together into a single procedure [[activerecord: | + | All of the code snippets above have been pulled together into a single procedure [[v5:activerecord: |
====== Extended Active Record ====== | ====== Extended Active Record ====== | ||
- | See the [[activerecord: | + | See the [[v5:activerecord: |
v5/activerecord/active_record_tutorial.txt · Last modified: 2022/06/09 18:37 by 3.83.52.90