// this save will perform an INSERT successfully $person2 = new person(); $person2->Load('id=1'); $c = $person2->children; if (is_array($c) && sizeof($c) == 3 && $c[0]->name_first == 'Jill' && $c[1]->name_first == 'Joan' && $c[2]->name_first == 'JAMIE') { echo "OK Loaded HasMany</br>"; } else { var_dump($c); echo "error loading hasMany should have 3 array elements Jill Joan Jamie<br>"; } class child extends ADOdb_Active_Record { } ADODB_Active_Record::TableBelongsTo('children', 'person', 'person_id', 'id'); $ch = new Child('children', array('id')); $ch->Load('id=1'); if ($ch->name_first !== 'Jill') { echo "error in Loading Child<br>"; } $p = $ch->person; if (!$p || $p->name_first != 'John') { echo "Error loading belongsTo<br>"; } else { echo "OK loading BelongTo<br>"; } if ($p) { #$p->HasMany('children','person_id'); ## this is affects all other instances of Person $p->LoadRelations('children', 'order by id', 1, 2); if (sizeof($p->children) == 2 && $p->children[1]->name_first == 'JAMIE') { echo "OK LoadRelations<br>"; } else {
ar_echo("Load() always uses the join method since it returns only one row\n"); ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); $person = new Person(); // Under the hood, Load(), since it returns only one row, always perform a join // Therefore we need to clarify which id we are talking about. $person->Load('people.id=1'); ar_echo(ar_assert(found($person, "'name_first' => 'John'")) ? "[OK] Found John\n" : "[!!] Find failed\n"); ar_echo(ar_assert(found($person, "'favorite_pet' => 'tortoise'")) ? "[OK] Found relation: child\n" : "[!!] Missing relation: child\n"); ar_echo(ar_assert(found($person, "'name_first' => 'Joan'")) ? "[OK] Found Joan\n" : "[!!] Find failed\n"); ar_echo(ar_assert(found($person, "'name_first' => 'JAMIE'")) ? "[OK] Found JAMIE\n" : "[!!] Find failed\n"); ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); ar_echo("child->Load('children.id=1') [Join Method]\n"); ar_echo("We are now loading from the 'children' table, not from 'people'\n"); ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); $child = new Child(); $child->Load('children.id=1'); ar_echo(ar_assert(found($child, "'name_first' => 'Jill'")) ? "[OK] Found Jill\n" : "[!!] Find failed\n"); ar_echo(ar_assert(found($child, "'favorite_color' => 'lavender'")) ? "[OK] Found relation: person\n" : "[!!] Missing relation: person\n"); ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); ar_echo("child->Find('children.id=1' ... ADODB_WORK_AR) [Worker Method]\n"); ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); $child = new Child(); $children = $child->Find('id=1', false, false, array('loading' => ADODB_WORK_AR)); ar_echo(ar_assert(found($children, "'name_first' => 'Jill'")) ? "[OK] Found Jill\n" : "[!!] Find failed\n"); ar_echo(ar_assert(found($children, "'favorite_color' => 'lavender'")) ? "[OK] Found relation: person\n" : "[!!] Missing relation: person\n"); ar_echo(ar_assert(notfound($children, "'name_first' => 'Joan'")) ? "[OK] No Joan relation\n" : "[!!] Find failed\n"); ar_echo(ar_assert(notfound($children, "'name_first' => 'JAMIE'")) ? "[OK] No JAMIE relation\n" : "[!!] Find failed\n"); ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); ar_echo("kid->Find('children.id=1' ... ADODB_WORK_AR) [Worker Method]\n"); ar_echo("Where we see that kid shares relationships with child because they are stored\n"); ar_echo("in the common table's metadata structure.\n");