ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:activerecord:active_record_tutorial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
activerecord:active_record_tutorial [2015/12/08 05:09] – [LoadRelations] mnewnhamactiverecord:active_record_tutorial [2015/12/08 05:37] – [ClassHasMany] mnewnham
Line 78: Line 78:
   1048: Column 'favorite_color' cannot be null   1048: Column 'favorite_color' cannot be null
    
-This error occurred because MySQL rejected the INSERT query that was generated by ADOdb_Active_Record.+This error occurred because MySQL rejected the INSERT statement that was generated.
  
 To insert a new ADOdb_Active_Record in the database, populate all of ADOdb_Active_Record's properties so that they satisfy the constraints of the database table, and then call the [[activerecod:save()]] method: To insert a new ADOdb_Active_Record in the database, populate all of ADOdb_Active_Record's properties so that they satisfy the constraints of the database table, and then call the [[activerecod:save()]] method:
Line 337: Line 337:
 ===== ClassHasMany ===== ===== ClassHasMany =====
  
-To globally define a one-to-many relationship we use the static function ADODB_Active_Record::ClassHasMany($class, $relation, $foreignKey = ' ', $foreignClass = 'ADODB_Active_Record'). For example, we have 2 tables, persons (parent table) and children (child table) linked by persons.id = children.person_id. The variable $person->children is an array that holds the children. To define this relationship:+To globally define a one-to-many relationship we use the static function [[activerecord:classHasMany()]]. For example, we have 2 tables, persons (parent table) and children (child table) linked by persons.id = children.person_id. The variable $person->children is an array that holds the children. To define this relationship:
  
 <code php> <code php>
Line 408: Line 408:
 var_dump($p->children); var_dump($p->children);
 </code> </code>
-The solution to the above is to unset($p->children) before $p->Load('id=2').+The solution to the above is to  
 +  unset($p->children)  
 +before  
 +  $p->Load('id=2').
  
 ===== TableHasMany ===== ===== TableHasMany =====
Line 425: Line 428:
 <code php> <code php>
  
-$db->Execute("insert into children (person_id,name_first,name_last) values (1,'Jill','Lim')"); +$db->Execute("insert into children (person_id,name_first,name_last)  
-$db->Execute("insert into children (person_id,name_first,name_last) values (1,'Joan','Lim')"); +              values (1,'Jill','Lim')"); 
-$db->Execute("insert into children (person_id,name_first,name_last) values (1,'JAMIE','Lim')");+$db->Execute("insert into children (person_id,name_first,name_last)  
 +               values (1,'Joan','Lim')"); 
 +$db->Execute("insert into children (person_id,name_first,name_last) 
 +               values (1,'JAMIE','Lim')");
  
 class person extends ADOdb_Active_Record{} class person extends ADOdb_Active_Record{}
Line 438: Line 444:
 $person->save(); // this save will perform an INSERT successfully $person->save(); // this save will perform an INSERT successfully
  
-$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->Load('id=1'); $person2->Load('id=1');
  
 $c = $person2->children; $c = $person2->children;
-if (is_array($c) && sizeof($c) == 3 && $c[0]->name_first=='Jill' && $c[1]->name_first=='Joan' +if (is_array($c)  
- && $c[2]->name_first == 'JAMIE') echo "OK Loaded HasMany<br>";+&& sizeof($c) == 3  
 +&& $c[0]->name_first=='Jill'  
 +&& $c[1]->name_first=='Joan' 
 +&& $c[2]->name_first == 'JAMIE' 
 +    echo "OK Loaded HasMany<br>";
 else { else {
-    echo "Error loading hasMany should have 3 array elements Jill Joan Jamie<br>";+    echo "Error loading hasMany should have  
 +          3 array elements Jill Joan Jamie<br>";
 } }
 </code>  </code> 
Line 483: Line 497:
 ===== ClassBelongsTo ===== ===== ClassBelongsTo =====
  
-You can define the parent of the current object using ADODB_Active_Record::ClassBelongsTo($class, $relationName, $foreignKey, $parentPrimaryKey = 'id', $parentClass = 'ADODB_Active_Record'). 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:+You can define the parent of the current object using [[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{};
v5/activerecord/active_record_tutorial.txt · Last modified: 2022/06/09 18:37 by 3.83.52.90