Overview

Namespaces

  • Alchemy
    • core
      • query
      • schema
    • dialect
    • engine
    • orm
    • tests
    • util
      • promise
  • PHP

Classes

  • DataMapper
  • DDL
  • ManyToOne
  • OneToMany
  • OneToOne
  • ORMQuery
  • ORMTable
  • ORMTableRef
  • RelatedSet
  • Relationship
  • Session
  • SessionSelect
  • WorkQueue
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: 
 3: namespace Alchemy\orm;
 4: use Alchemy\core\query\Query;
 5: 
 6: 
 7: /**
 8:  * Controller for performing DDL operations on the database
 9:  */
10: class DDL {
11:     private $session;
12: 
13:     /**
14:      * Object constructor
15:      *
16:      * @param Session $session
17:      */
18:     public function __construct(Session $session) {
19:         $this->session = $session;
20:     }
21: 
22: 
23:     /**
24:      * CREATE the table for the given DataMapper class
25:      *
26:      * @param string $cls Class Name of DataMapper child
27:      */
28:     public function create($cls) {
29:         $create = Query::Create($cls::schema());
30:         $this->session->engine()->query($create);
31:     }
32: 
33: 
34:     /**
35:      * Find all subclasses of DataMapper and run {@see DDL::create()} on each
36:      * of them.
37:      */
38:     public function createAll() {
39:         $mappers = DataMapper::list_mappers();
40:         $created = array();
41: 
42:         while (count($mappers) > 0) {
43:             $mapper = array_pop($mappers);
44:             $table = $mapper::schema();
45:             $dependancies = $table->listDependancies();
46:             $dependancies = array_diff($dependancies, $created);
47: 
48:             if (count($dependancies) > 0) {
49:                 array_unshift($mappers, $mapper);
50:             } else {
51:                 $this->create($mapper);
52:                 $created[] = $table->getName();
53:             }
54:         }
55:     }
56: 
57: 
58:     /**
59:      * DROP the table for the given DataMapper class
60:      */
61:     public function drop($cls) {
62:         $drop = Query::Drop($cls::schema());
63:         $this->session->engine()->query($drop);
64:     }
65: 
66: 
67:     /**
68:      * Find all subclasses of DataMapper and run {DDL::drop()} on each
69:      * of them.
70:      */
71:     public function dropAll() {
72:         $mappers = DataMapper::list_mappers();
73:         $dropped = array();
74: 
75:         while (count($mappers) > 0) {
76:             $mapper = array_pop($mappers);
77:             $table = $mapper::schema();
78:             $dependants = $table->listDependants();
79:             $dependants = array_diff($dependants, $dropped);
80: 
81:             if (count($dependants) > 0) {
82:                 array_unshift($mappers, $mapper);
83:             } else {
84:                 $this->drop($mapper);
85:                 $dropped[] = $table->getName();
86:             }
87:         }
88:     }
89: }
90: 
API documentation generated by ApiGen 2.8.0