Merge branch 'maint/7.0'
[ninja.git] / src / op5 / ninja_sdk / orm / ORMObjectSetGenerator.php
blob2b67cd7c0df2612e0412c8d9077ef169ad62ef8a
1 <?php
3 abstract class ORMObjectSetGenerator extends class_generator {
4 public $name;
5 public $structure;
6 public $full_structure;
7 public $objectclass;
8 public $associations; /** an association is a way to get a one-to-many */
10 public function __construct( $name, $structure ) {
11 $this->name = $name;
12 $this->structure = $structure[$name];
13 $this->full_structure = $structure;
14 $this->objectclass = $this->structure['class'].self::$model_suffix;
15 $this->classname = 'Base'.$this->structure['class'].'Set';
17 $this->associations = array();
19 foreach( $structure as $table => $tbl_struct ) {
20 foreach( $tbl_struct['structure'] as $name => $type ) {
21 if( is_array( $type ) ) {
22 if( $type[0] == $this->structure['class'] ) {
23 $this->associations[] = array(
24 $table,
25 $tbl_struct['class'],
26 $name
33 $this->set_model();
36 public function generate($skip_generated_note = false) {
37 parent::generate($skip_generated_note);
38 $this->init_class( 'ObjectSet', array('abstract') );
39 $this->variable('table',$this->name,'protected');
41 if( isset($this->structure['default_sort']) )
42 $this->variable('default_sort',$this->structure['default_sort'],'protected');
44 $this->variable('class',$this->structure['class'].self::$model_suffix,'protected');
45 $this->variable('key_columns',$this->structure['key'],'protected');
47 $this->generate_backend_specific_functions();
49 $this->generate_apply_columns_rewrite();
50 $this->generate_get_all_columns_list();
52 /* External interface, backend specific */
53 $this->generate_stats();
54 $this->generate_count();
55 $this->generate_it();
57 /* Interface used by orm-related libraries (some visitors) */
58 $this->generate_map_name_to_backend();
60 foreach( $this->associations as $assoc ) {
61 $this->generate_association_get_set( $assoc[0], $assoc[1], $assoc[2] );
63 $this->finish_class();
66 public function generate_apply_columns_rewrite() {
67 $this->init_function('apply_columns_rewrite', array('columns', 'prefix'),array('static'),array('prefix'=>''));
68 $this->write( 'foreach('.$this->structure['class'].self::$model_suffix.'::$rewrite_columns as $column => $rewrites) {');
69 $this->write( 'if( in_array( $prefix.$column, $columns ) ) {' );
70 $this->write( 'foreach($rewrites as $rewrite) {' );
71 $this->write( '$columns[] = $prefix.$rewrite;' );
72 $this->write( '}' );
73 $this->write( '}' );
74 $this->write( '}' );
75 foreach( $this->structure['structure'] as $name => $type ) {
76 if(isset($this->structure['rename']) && isset($this->structure['rename'][$name])) {
77 $name = $this->structure['rename'][$name];
79 if(is_array($type)) {
80 $this->write('$columns = '.$type[0].'Set'.self::$model_suffix.'::apply_columns_rewrite($columns,%s);',$name.".");
83 $this->write('return $columns;');
84 $this->finish_function();
87 public function generate_get_all_columns_list() {
88 $columns = array();
89 $subobjs = array();
90 foreach ($this->structure['structure'] as $name => $type) {
91 if (is_array($type)) {
92 $subobjs[$name] = $type;
93 } else {
94 $columns[] = $name;
97 $this->init_function('get_all_columns_list', array('include_nested'), array('static'), array('include_nested'=>true));
98 $this->write('$raw_columns = %s;', $columns);
99 $this->write('$sub_columns = array();');
100 $this->write('if ($include_nested) {');
101 foreach ($subobjs as $name => $type) {
102 $this->write('$obj_cols = '.$type[0].'Set'.self::$model_suffix.'::get_all_columns_list(false);');
103 $this->write('foreach ($obj_cols as $name) {');
104 $this->write('$sub_columns[] = %s.$name;', $name.'.');
105 $this->write('}');
107 $this->write('}');
108 $this->write('$virtual_columns = array_keys('.$this->objectclass.'::$rewrite_columns);');
109 $this->write('return array_merge($sub_columns, $raw_columns, $virtual_columns);');
110 $this->finish_function();
113 public function generate_association_get_set($table, $class, $field) {
114 $this->init_function('get_'.$table);
115 $this->write('$result = '.$class.'Pool'.self::$model_suffix.'::all();');
116 $this->write('$result->filter = $this->filter->prefix(%s);', $field.'.');
117 $this->write('return $result;');
118 $this->finish_function();
121 abstract public function generate_it();
122 abstract public function generate_count();
123 abstract public function generate_stats();
124 abstract public function generate_map_name_to_backend();
125 abstract public function generate_backend_specific_functions();