ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:activerecord:classbelongsto

classBelongsTo

Syntax
void ADODB_Active_Record::classBelongsTo(
   string $childClass, 
   string $parentClass, 
   string $foreignKey, 
   optional string $parentPrimaryKey = 'id', 
   optional string $parentClass = 'ADODB_Active_Record'
   )

Description

You can define the parent of the current object using 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

Parameters

$childClass

The child table in the relationship

$parentClass

The parent table in the relationship

$foreignKey

If, due to database design constraints, the key field in the child table is not named the same as that of the parent table, specify it here.

$foreignClass

If the class name for the child table differs from that of the parent, for example there has been some extension of methods, specify it here

Usage

class kid extends ADOdb_Active_Record{};
ADODB_Active_Record::ClassBelongsTo('kid','person','person_id','id');
 
$ch = new kid(); // default tablename will be 'kids', with primary key 'id'
$ch->Load('id=1');
$p = $ch->person;
if (!$p || $p->name_first != 'John') echo "Error loading belongsTo<br>";
else echo "OK loading BelongTo<br>";
v5/activerecord/classbelongsto.txt · Last modified: 2019/01/01 01:31 by mnewnham