eZ components PHP Cross Reference Developer Tools

Source: /Tree/docs/tutorial_example_persistent_object.php - 49 lines - 1684 bytes - Summary - Text - Print

   1  <?php
   2  require_once  'tutorial_autoload.php';
   3  
   4  // Setup the database tables
   5  $dbh = ezcDbFactory::create( 'sqlite://:memory:' );
   6  $dbh->exec( <<<ENDSQL
   7      CREATE TABLE nested_set (
   8          'id' varchar(255) NOT NULL,
   9          'parent_id' varchar(255),
  10          'lft' integer NOT NULL,
  11          'rgt' integer NOT NULL
  12      );
  13      CREATE UNIQUE INDEX 'nested_set_pri' on 'nested_set' ( 'id' );
  14      CREATE INDEX 'nested_set_left' on 'nested_set' ( 'lft' );
  15      CREATE INDEX 'nested_set_right' on 'nested_set' ( 'rgt' );
  16  
  17      CREATE TABLE data (
  18          'node_id' varchar(255) NOT NULL,
  19          'melting_temp_k' float,
  20          'boiling_temp_k' float
  21      );
  22      CREATE UNIQUE INDEX 'data_pri' on 'data' ( 'node_id' );
  23  ENDSQL
  24  );
  25  
  26  // Create the example Persistent Object definition files and stub classes
  27  $dbSchema = ezcDbSchema::createFromDb( $dbh );
  28  $writer1 = new ezcDbSchemaPersistentWriter( true );
  29  $writer2 = new ezcDbSchemaPersistentClassWriter( true );
  30  $writer1->saveToFile( 'files/po_defs', $dbSchema );
  31  $writer2->saveToFile( 'files/classes', $dbSchema );
  32  require  'files/classes/data.php';
  33  
  34  // Setup the store and tree
  35  $session = new ezcPersistentSession( $dbh, new ezcPersistentCodeManager( "files/po_defs" ) );
  36  $store = new ezcTreePersistentObjectDataStore( $session, 'data', 'node_id' );
  37  $tree = new ezcTreeDbNestedSet( $dbh, 'nested_set', $store );
  38  
  39  // Insert data
  40  $metal = new data();
  41  $tree->setRootNode( $root = $tree->createNode( 'Metals', $metal ) );
  42  $iron = new data();
  43  $iron->setState( array( 'melting_temp_k' => 1811, 'boiling_temp_k' => 3134 ) );
  44  $root->addChild( $tree->createNode( 'Fe', $iron ) );
  45  
  46  // Fetch data
  47  $fe = $tree->fetchNodeById( 'Fe' )->data;
  48  var_dump( $fe );
  49  ?>

title

Description

title

Description

title

Description

title

title

Body