example
1 <?php 2 /* 3 * there is a HOWTO for this example: Bs_SimpleObjPersister.howTo.pdf 4 */ 5 6 class Foo { 7 var $ID = 0; // Following attribute will be our ID (primary key) 8 9 var $attr_1 = 'A string to persist'; // persist (string) 10 var $attr_2 = 0; // persist (integer) 11 12 var $dummy = ''; // don't persist 13 14 function bs_sop_getHints() { 15 static $hint_hash = array ( 16 'primary' => array ( 17 'ID' => array('name'=>'id', 'type'=>'auto_increment'), 18 ), 19 'fields' => array ( 20 'attr_1' => array('name'=>'str', 'metaType'=>'string', 'size'=>40 ), //of course it would make sense to use 21 'attr_2' => array('name'=>'num', 'metaType'=>'integer'), //the var names as field names too. 22 ) 23 ); 24 return $hint_hash; 25 } 26 } 27 28 29 //----------------------------------------------------------------------------------- 30 // A) First thing we do here, is to include the DB-Factory that will return us a 31 // DB-Agent. The Bs_SimpleObjPersister needs a DB-Agent to 'talk' to the 32 // underlying DB. 33 require_once($_SERVER['DOCUMENT_ROOT'] . '../global.conf.php'); 34 require_once($GLOBALS['APP']['path']['core'] . 'db/Bs_Db.class.php'); 35 36 //----------------------------------------------------------------------------------- 37 // B) Then setup the connection parameters, that we want to pass to the DB-Factory. 38 // In this case it's going to return a mySQL DB-Agent 39 $dsn = array ('name'=>'test', 'host'=>'localhost', 'port'=>'3306', 'socket'=>'', 40 'user'=>'root', 'pass'=>'', 41 'syntax'=>'mysql', 'type'=>'mysql'); 42 43 //----------------------------------------------------------------------------------- 44 // C) Get the DB-Agent. If the return is a Bs_Exception echo error and die. 45 if (isEx($dbAgent =& getDbObject($dsn))) { 46 $dbAgent->stackDump('echo'); 47 die(); 48 } 49 50 //----------------------------------------------------------------------------------- 51 // D) Now create the Bs_SimpleObjPersister and pass the DB-Agent. It's now ready 52 // to be used ... 53 require_once($GLOBALS['APP']['path']['core'] . 'storage/objectpersister/Bs_SimpleObjPersister.class.php'); 54 55 $objPersister = new Bs_SimpleObjPersister(); 56 $objPersister->setDbObject($dbAgent); 57 58 //let's store a new one 59 $myFoo = new Foo(); 60 $myFoo->attr_1 = 'This is new!'; 61 $objPersister->store($myFoo); 62 $myNewId = $myFoo->ID; 63 64 //and now let's load it 65 $myFoo = new Foo(); 66 $myFoo->ID = $myNewId; 67 if ($objPersister->load($myFoo)) { 68 echo $myFoo->attr_1 . ' ' . $myFoo->ID; // Will print 'This is new!' (plus the ID); 69 } 70 ?>
|