v5:activerecord:active_record_tutorial
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
activerecord:active_record_tutorial [2015/12/08 05:03] – [Transactions] mnewnham | v5:activerecord:active_record_tutorial [2016/01/18 07:43] – ↷ Links adapted because of a move operation 66.249.75.217 | ||
---|---|---|---|
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 133: | Line 133: | ||
===== Using $ADODB_ASSOC_CASE ==== | ===== Using $ADODB_ASSOC_CASE ==== | ||
- | You can use [[reference: | + | You can use [[v5:reference:adodb_assoc_case]] to control the field names |
<code php> | <code php> | ||
Line 146: | Line 146: | ||
===== 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 162: | ||
<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 175: | ||
====== 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 187: | ||
===== 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 200: | ||
</ | </ | ||
- | 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 250: | ||
===== 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 280: | ||
$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 318: | ||
===== 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 337: | ||
===== 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 408: | ||
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 417: | ||
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 423: | ||
===== 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 444: | ||
$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 452: | Line 466: | ||
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 |
+ | | ||
+ | |||
+ | For example, we have 2 tables, persons (parent table) and children (child table) linked by persons.id = children.person_id. The variable | ||
<code php> | <code php> | ||
class person extends ADOdb_Active_Record{} | class person extends ADOdb_Active_Record{} | ||
Line 479: | Line 497: | ||
===== ClassBelongsTo ===== | ===== ClassBelongsTo ===== | ||
- | You can define the parent of the current object using ADODB_Active_Record::ClassBelongsTo($class, | + | You can define the parent of the current object using [[v5:activerecord:classbelongsto]]. In the example below, we have a child table kids, and a parent table person. We have a link kids.person_id = persons.id. We create a child first, then link it to the parent: |
<code php> | <code php> | ||
class kid extends ADOdb_Active_Record{}; | class kid extends ADOdb_Active_Record{}; | ||
Line 514: | Line 533: | ||
===== 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 522: | Line 541: | ||
===== 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 530: | Line 549: | ||
===== 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 537: | Line 556: | ||
$person = new person(); | $person = new person(); | ||
$person-> | $person-> | ||
- | # Load doesn' | + | /* |
+ | * | ||
+ | * is accessed or LoadRelations is called: | ||
+ | */ | ||
$person-> | $person-> | ||
</ | </ | ||
Line 551: | Line 573: | ||
$start = 0; | $start = 0; | ||
while(true) { | while(true) { | ||
- | $acc-> | + | $acc-> |
- | if (!$acc-> | + | |
- | foreach ($acc-> | + | |
+ | | ||
+ | | ||
+ | if (!$acc-> | ||
+ | | ||
+ | foreach ($acc-> | ||
+ | | ||
## process | ## process | ||
$trx-> | $trx-> | ||
Line 566: | Line 594: | ||
- | 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