Automatic Table Linking --
Automatic Table Linking - ::getLink(), ::getLinks()
Automating the collection of related data
When designing a database, often some tables are related to others - a membership table would contain
a reference to a person's id and the group id that they are a member of. Using the Link methods, you
can automatically fetch objects into the parents variables.
The Automated method of doing this is heavily dependent on naming columns relating to databases, for example in the
membership described above, you would have to have a column called person_id, group_id for the automated Link
methods to understand which table to fetch the data from. The principle is TABLENAME_ANYTHING, and it
will always attempt to find primary keys that match the value of field, the getLinks() methods will auto load
the related object into $object->_tablename_anything
The other way of using getLink() is to use it with the second parameter which is the table name of the related
table, it will then query the primary key on that table to see if it matches the value of the requested row.
In this method however the getLinks() method will not work, as it will not know the related table names.
Using link ini files for table links
DB_DataObject Version 0.3 introduced the ability to create link ini files so you can map rows to other database
columns using an ini file this ini file should have the name databasename.links.ini, and be placed in the
same folder as the databasename.ini file that is created automatically by createTables.php
The Ini file contains a section for each table, then the row that is linked equal to the table and row
that it should locate the row from. If you use a 'full stop' in the key (link from column), getLinks() will
look up in the table with the row name matching the string to the left of the 'full stop', and replace the
'full stop' with and underscore and assign the object variable to that name.
Beispiel 1. An example databasename.links.ini file ; for table person
[person]
; link value of eycolor to table colors, match name column
eyecolor = colors:name
; link value of owner to table grp, match id column
owner = grp:id
; link value of picture to table attachments, match id column
picture = attachments:id
; for a sales example with multiple links of a single column
[sales]
; for autoloading the car object into $sales->_car_id
car_id = car:id
; for autoloading the part number object into $sales->_car_id_partnum
car_id.partnum = part_numbers:car_id |
|
::getLink()
Gets the Related Row Object from the Related Table
Gets the related Object
Beispiel 2. Getting the related object $person = new DataObjects_Person;
$person->get(12);
$group = $person->getLink('group_owner');
echo $group->name;
$group = $person->getLink('colourid','hair'); |
|
This would execute the following SQL statements, and fetch the data
::getLinks()
Loads all the related Objects
Loads the all the related objects into the main object, based on the naming of columns using the tablename_xxx
naming method, and sets the main objects variables with the row name prefixed with a _
Beispiel 4. Two Example Tables Person
+---------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+----------------+
| id | mediumint(9) | | PRI | 0 | auto_increment |
| first_name | varchar(80) | YES | | NULL | |
| last_name | varchar(80) | | MUL | | |
| middle_name | varchar(80) | YES | | NULL | |
| badge_number | smallint(6) | YES | | NULL | |
| street | varchar(80) | YES | | NULL | |
| city | varchar(80) | YES | | NULL | |
| state | varchar(80) | YES | | NULL | |
| zip | varchar(15) | YES | | NULL | |
| phone | varchar(15) | YES | | NULL | |
| reg_type | varchar(80) | YES | | NULL | |
| judge | varchar(10) | YES | | NULL | |
| staff | varchar(10) | YES | | NULL | |
| volunteer | varchar(10) | YES | | NULL | |
| rpga_number | mediumint(9) | YES | | NULL | |
| total_fee | float(10,2) | YES | | NULL | |
| email_address | varchar(80) | YES | | NULL | |
| country | varchar(30) | YES | | NULL | |
| convention_id | int(11) | | | 0 | |
| last_modified | timestamp(14) | YES | | NULL | |
+---------------+---------------+------+-----+---------+----------------+
Convention
+----------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------+------+-----+---------+----------------+
| id | int(11) | | PRI | 0 | auto_increment |
| name | varchar(50) | | | | |
| sponsor_organization | varchar(50) | | | | |
| rpga_convention_code | varchar(20) | | | | |
| web_site_url | varchar(200) | | | | |
| last_modified | timestamp(14) | YES | | NULL | |
+----------------------+---------------+------+-----+---------+----------------+ |
|
This would execute the following SQL statement, and fetch the data
And output the following data
Beispiel 7. Resulting Output Object:dataobjects_person Object
(
[_DB_DataObject_version] => 1.0
[__table] => person
[_database_dsn] =>
[_database_dsn_md5] => 3974043abbccdd6412fb156a1d10b98377
[_database] => testing
[_condition] =>
[_group_by] =>
[_order_by] =>
[_limit] =>
[_data_select] => *
[_link_loaded] => 1
[_lastError] => pear_error Object
(
[error_message_prefix] =>
[mode] => 1
[level] => 1024
[code] => -3
[message] => getLink:Could not find class for row last_modified, table last
[userinfo] =>
[callback] =>
)
[id] => 1079
[N] => 1
[_DB_resultid] => 2
[first_name] => Tim
[last_name] => White
[middle_name] =>
[badge_number] => 123
[street] => 334411 N Washington
[city] => Texas
[state] => CO
[zip] => 12345
[phone] => 343412323232
[reg_type] => Staff
[judge] =>
[staff] => CHECKED
[volunteer] =>
[rpga_number] => 1232323
[total_fee] => 0.00
[email_address] => tim@example.com
[country] => USA
[convention_id] => 1
[last_modified] => 20020711084539
[_first_name] =>
[_last_name] =>
[_middle_name] =>
[_badge_number] =>
[_reg_type] =>
[_rpga_number] =>
[_total_fee] =>
[_email_address] =>
[_convention_id] => dataobjects_convention Object
(
[_DB_DataObject_version] => 1.0
[__table] => convention
[_database_dsn] =>
[_database_dsn_md5] => 3974043abbcc86412fb156a1d10b98377
[_database] => testing
[_condition] =>
[_group_by] =>
[_order_by] =>
[_limit] =>
[_data_select] => *
[_link_loaded] =>
[_lastError] =>
[id] => 1
[N] => 1
[_DB_resultid] => 3
[name] => ABCD XYZ
[sponsor_organization] => some sponser
[rpga_convention_code] => ABCD_XYZ
[web_site_url] => http://example.com
[last_modified] => 20020703143828
)
[_last_modified] =>
) |
|
In the example above, alot of the columns have '_' underscores in and do not relate to tables,
DB_DataObject will only flag it as an error in $object->_lastError.