3 abstract class ORMObjectSetGenerator
extends class_generator
{
6 public $full_structure;
8 public $associations; /** an association is a way to get a one-to-many */
10 public function __construct( $name, $structure ) {
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(
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();
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;' );
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];
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() {
90 foreach ($this->structure
['structure'] as $name => $type) {
91 if (is_array($type)) {
92 $subobjs[$name] = $type;
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.'.');
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();