PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/include/adodb/tests/test-active-record.php

https://github.com/radicaldesigns/amp
PHP | 82 lines | 48 code | 19 blank | 15 comment | 2 complexity | c7ef92274ade6c6db74e20c8796219f1 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. include_once('../adodb.inc.php');
  3. include_once('../adodb-active-record.inc.php');
  4. // uncomment the following if you want to test exceptions
  5. if (@$_GET['except']) {
  6. if (PHP_VERSION >= 5) {
  7. include('../adodb-exceptions.inc.php');
  8. echo "<h3>Exceptions included</h3>";
  9. }
  10. }
  11. $db = NewADOConnection('mysql://root@localhost/northwind');
  12. $db->debug=1;
  13. ADOdb_Active_Record::SetDatabaseAdapter($db);
  14. $db->Execute("CREATE TEMPORARY TABLE `persons` (
  15. `id` int(10) unsigned NOT NULL auto_increment,
  16. `name_first` varchar(100) NOT NULL default '',
  17. `name_last` varchar(100) NOT NULL default '',
  18. `favorite_color` varchar(100) NOT NULL default '',
  19. PRIMARY KEY (`id`)
  20. ) ENGINE=MyISAM;
  21. ");
  22. class Person extends ADOdb_Active_Record{}
  23. $person = new Person();
  24. echo "<p>Output of getAttributeNames: ";
  25. var_dump($person->getAttributeNames());
  26. /**
  27. * Outputs the following:
  28. * array(4) {
  29. * [0]=>
  30. * string(2) "id"
  31. * [1]=>
  32. * string(9) "name_first"
  33. * [2]=>
  34. * string(8) "name_last"
  35. * [3]=>
  36. * string(13) "favorite_color"
  37. * }
  38. */
  39. $person = new Person();
  40. $person->name_first = 'Andi';
  41. $person->name_last = 'Gutmans';
  42. $person->save(); // this save() will fail on INSERT as favorite_color is a must fill...
  43. $person = new Person();
  44. $person->name_first = 'Andi';
  45. $person->name_last = 'Gutmans';
  46. $person->favorite_color = 'blue';
  47. $person->save(); // this save will perform an INSERT successfully
  48. echo "<p>The Insert ID generated:"; print_r($person->id);
  49. $person->favorite_color = 'red';
  50. $person->save(); // this save() will perform an UPDATE
  51. $person = new Person();
  52. $person->name_first = 'John';
  53. $person->name_last = 'Lim';
  54. $person->favorite_color = 'lavender';
  55. $person->save(); // this save will perform an INSERT successfully
  56. // load record where id=2 into a new ADOdb_Active_Record
  57. $person2 = new Person();
  58. $person2->Load('id=2');
  59. var_dump($person2);
  60. $activeArr = $db->GetActiveRecordsClass($class = "Person",$table = "persons","id=".$db->Param(0),array(2));
  61. $person2 = $activeArr[0];
  62. echo "<p>Name (should be John): ",$person->name_first, " <br> Class (should be Person): ",get_class($person2);
  63. ?>