Helix Installation   Getting Started   Reference Documentation   Global Functions   Quick Start
ModulesClassesFunctions
 

Class Reference

class mysql_table extends obj implements dbi_table

Hide Inherited MembersHelix Core
Description
No description.
 
Constants
constantSTANDARD [1][ mysql_table ]
constantRELATIONSHIP [2][ mysql_table ]
constantCUSTOM [3][ mysql_table ]
constantLOOKUP [4][ mysql_table ]
constantSTANDARD_TYPE [5][ mysql_table ]
constantRELATIONSHIP_TYPE [6][ mysql_table ]
constantCOLUMN_EXTENSION [7][ mysql_table ]
constantRESOURCE [8][ mysql_table ]
constantMULTIPLE_RELATIONSHIP [9][ mysql_table ]
 
Properties
protected_class[ mysql_table ]
publiccolumns[ mysql_table ]
protected_database[ mysql_table ]
protected_dirty[ obj ]
publicfk_columns[ mysql_table ]
protected_has_type[ mysql_table ]
protected_is_child[ mysql_table ]
protected_is_col_ext[ mysql_table ]
protected_is_ordered[ mysql_table ]
protected_is_parent[ mysql_table ]
protected_is_resource[ mysql_table ]
protected_is_self_referential[ mysql_table ]
protected_is_type[ mysql_table ]
publiclinked_columns[ mysql_table ]
publiclinked_relationship_tables[ mysql_table ]
publiclinked_tables[ mysql_table ]
publiclookups[ mysql_table ]
protected_name[ mysql_table ]
protected_num_parts[ mysql_table ]
protected_object[ mysql_table ]
protected_object1[ mysql_table ]
protected_object2[ mysql_table ]
protected_parent[ mysql_table ]
protected_parts[ mysql_table ]
protected_prefix[ mysql_table ]
protected_primary_key[ mysql_table ]
publicprimary_keys[ mysql_table ]
publicproperty_objects_array[ obj ]
publicreferenced_columns[ mysql_table ]
publicreferenced_tables[ mysql_table ]
protected_type[ mysql_table ]
publicunique_keys[ mysql_table ]
 
Methods
public__construct ( tablename , dbname ["h266209_helix"] )[ mysql_table ]
Parameters
requiredtablename
optionaldbname ["h266209_helix"]
Code
045  public function __construct($__tablename, $__dbname=DB_NAME)
046  {
047      $this->_database = null;
048      $this->_name = null;
049      $this->_class = null;
050      $this->_is_col_ext = false;
051      $this->_has_type = false;
052      $this->_is_type = false;
053      $this->_type = null;
054      $this->_parts = null;
055      $this->_num_parts = 0;
056      $this->_prefix = null;
057      $this->_object = null;
058      $this->_object1 = null;
059      $this->_object2 = null;
060      $this->_is_self_referential = false;
061      $this->_is_ordered = false;
062      $this->_parent = null;
063      $this->_primary_key = null;
064      $this->_is_child = false;
065      $this->_is_parent = false;
066      $this->_is_resource = false;
067      $this->columns = array();
068      $this->primary_keys = array();
069      $this->unique_keys = array();
070      $this->lookups = array();
071      $this->linked_tables = array();
072      $this->linked_columns = array();
073      $this->linked_relationship_tables = array();
074      $this->referenced_tables = array();
075      $this->referenced_columns = array();
076      $this->fk_columns = array();
077 
078      $db = new db();
079 
080      $q = "
081      SELECT *
082      FROM information_schema.TABLES
083      WHERE TABLE_NAME = '$__tablename'
084      AND TABLE_SCHEMA = '$__dbname'
085      ";
086      $db->query($q);
087      if ($db->next_record() && $db->num_rows() == 1)
088      {
089          $this->_database=$db->f("TABLE_SCHEMA");
090          $this->_name=$db->f("TABLE_NAME");
091          $this->_class = substr($this->_name,strpos($this->_name,"_")+1);
092      }
093 
094      $q = "
095      SELECT *
096      FROM information_schema.TABLES
097      WHERE TABLE_NAME = '${__tablename}_type'
098      AND TABLE_SCHEMA = '$__dbname'
099      ";
100      $db->query($q);
101      if ($db->next_record() && $db->num_rows() == 1)
102      {
103          $this->_has_type = true;
104      }
105 
106      $q = "
107      SELECT *
108      FROM information_schema.COLUMNS
109      WHERE TABLE_NAME = '$__tablename'
110      AND TABLE_SCHEMA = '$__dbname'
111      AND COLUMN_KEY='PRI'
112      ORDER BY ORDINAL_POSITION
113      ";
114      $db->query($q);
115      while ($db->next_record())
116      {
117          $this->primary_keys[] = $db->f("COLUMN_NAME");
118      }
119      $this->primary_key = implode(",",$this->primary_keys);
120 
121      $q = "
122      SELECT *
123      FROM information_schema.COLUMNS
124      WHERE TABLE_NAME = '$__tablename'
125      AND TABLE_SCHEMA = '$__dbname'
126      AND COLUMN_KEY='UNI'
127      ORDER BY ORDINAL_POSITION
128      ";
129      $db->query($q);
130      while ($db->next_record())
131      {
132          $this->unique_keys[] = $db->f("COLUMN_NAME");
133      }
134 
135      $q = "
136      SELECT *
137      FROM information_schema.COLUMNS
138      WHERE TABLE_NAME = '$__tablename'
139      AND TABLE_SCHEMA = '$__dbname'
140      ORDER BY ORDINAL_POSITION
141      ";
142      $db->query($q);
143      while ($db->next_record())
144      {
145          $this->columns[$db->f("COLUMN_NAME")] = new db_column($this->_name, $db->f("COLUMN_NAME"), $this->_database);
146          if (preg_match('/.+_id$/i',$db->f("COLUMN_NAME")) && !in_array($db->f("COLUMN_NAME"),$this->primary_keys))
147          {
148              $this->lookups[] = $db->f("COLUMN_NAME");
149          }
150      }
151 
152      $this->_is_ordered = in_array("order",array_keys($this->columns));
153      $this->_parts = preg_split('/_/i',$this->_name);
154      $this->_num_parts = count($this->_parts);
155      $this->_prefix = $this->_parts[0];
156     
157      // DO NOT TREAT RESOURCES DIFFERENTLY THAN DB CLASSES RIGHT NOW
158      // MAKE RESOURCE SPECIFIC DIFFERENCES IN THE EXTENDED CLASS
159       $this->_is_resource = preg_match('/_resource$/i',$this->name);
160  //        $this->_is_resource = false;
161     
162     
163      if ($this->_num_parts === 2)
164      {
165          $col_ext_cols = array("id","value","mdate","cdate","deleted");
166          if (count(array_diff(array_keys($this->columns), $col_ext_cols)) == 0)
167          {
168              $this->_type = self::COLUMN_EXTENSION;
169              $this->_is_col_ext = true;
170          }
171          else if ($this->is_resource)
172          {
173              $this->_type = self::RESOURCE;
174          }
175          else
176          {
177              $this->_type = self::STANDARD;
178          }
179          $this->_object = $this->_parts[1];
180      }
181      else if ($this->_num_parts === 3)
182      {
183          if (preg_match('/_type$/i',$this->_name))
184          {
185              $this->_type = self::STANDARD_TYPE;
186              $this->_is_type = true;
187              $this->_object = $this->_parts[1] . "_" . $this->_parts[2];
188          }
189          else if (preg_match('/_(ext|cus)$/i',$this->_name))
190          {
191              $this->_type = self::CUSTOM;
192              $this->_object = $this->_parts[1] . "_" . $this->_parts[2];
193          }
194          else
195          {
196              $this->_type = self::RELATIONSHIP;
197              $this->_object = $this->_parts[1] . "_" . $this->_parts[2];
198              if ($this->_parts[1] == $this->_parts[2])
199              {
200                  $this->_is_self_referential = true;
201                  $this->_object1 = "child";
202                  $this->_object2 = "parent";
203              }
204              else
205              {
206                  $this->_object1 = $this->_parts[1];
207                  $this->_object2 = $this->_parts[2];
208              }
209          }
210      }
211      else if ($this->_num_parts === 4)
212      {
213          if (preg_match('/_type$/i',$this->_name))
214          {
215              $this->_type = self::RELATIONSHIP_TYPE;
216              $this->_is_type = true;
217              $this->_object = $this->_parts[1] . "_" . $this->_parts[2] . "_" . $this->_parts[3];
218          }
219          else if (preg_match('/_(ext|cus)$/i',$this->_name))
220          {
221              $this->_type = self::CUSTOM;
222              $this->_object = $this->_parts[1] . "_" . $this->_parts[2] . "_" . $this->_parts[3];
223          }
224      }
225 
226      $q  = "
227      SELECT TABLE_NAME
228      FROM information_schema.KEY_COLUMN_USAGE
229      WHERE CONSTRAINT_SCHEMA = '$this->_database'
230      AND REFERENCED_TABLE_NAME = '$this->_name'
231      AND TABLE_NAME REGEXP '^[a-z]+_[a-z]+_[a-z]+$'
232      ORDER BY TABLE_NAME
233      ";
234      $db->query($q);
235      $linked_tablenames = array();
236      while ($db->next_record())
237      {
238          $linked_tablenames[] = $db->f("TABLE_NAME");
239      }
240 
241      $q  = "
242      SELECT REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME, COLUMN_NAME
243      FROM information_schema.KEY_COLUMN_USAGE
244      WHERE CONSTRAINT_SCHEMA = '$this->_database'
245      AND TABLE_NAME = '$this->_name'
246      AND REFERENCED_TABLE_NAME IS NOT NULL
247      ORDER BY TABLE_NAME
248      ";
249      $db->query($q);
250      while ($db->next_record())
251      {
252          $this->referenced_tables[] = $db->f("REFERENCED_TABLE_NAME");
253          $this->referenced_columns[] = $db->f("REFERENCED_COLUMN_NAME");
254          $this->fk_columns[] = $db->f("COLUMN_NAME");
255      }
256 
257      foreach ($linked_tablenames as $tablename)
258      {
259          $q  = "
260          SELECT REFERENCED_TABLE_NAME, COLUMN_NAME
261          FROM information_schema.KEY_COLUMN_USAGE
262          WHERE CONSTRAINT_SCHEMA = '$this->_database'
263          AND TABLE_NAME = '$tablename'
264          ";
265         
266          if (!$this->is_self_referential)
267          {
268              $q .= "AND REFERENCED_TABLE_NAME <> '$this->_name'";
269          }
270         
271          $q .= "
272          AND REFERENCED_TABLE_NAME NOT REGEXP '_type$'
273          ORDER BY TABLE_NAME
274          ";
275          $db->query($q);
276          if ($db->next_record() && $db->num_rows() == 1)
277          {
278              $this->linked_relationship_tables[] = $tablename;
279              $this->linked_tables[] = $db->f("REFERENCED_TABLE_NAME");
280              $this->linked_columns[] = $db->f("COLUMN_NAME");
281          }
282      }
283 
284  }
public__get ( p )[ obj ]
Parameters
requiredp
Code
007  public function __get($p)
008  {
009      $methods = get_class_methods($this);
010      $properties = get_object_vars($this);
011      if (in_array("get_$p",$methods))
012      {
013          return $this->{"get_$p"}();
014      }
015      else if (array_key_exists("_$p",$properties))
016      {
017          return $this->{"_$p"};
018      }
019      else if (in_array("get_" . substr($p,strpos($p,"_")+1), $methods))
020      {
021          return $this->{"get_" . substr($p,strpos($p,"_")+1)}(substr($p,0,strpos($p,"_")));
022      }
023      else
024      {
025          trigger_error("This object [" . get_class($this) . ":$this->id] has no private property '_$p' for the requested public property '$p'",E_USER_WARNING);
026          return null;
027      }
028  }
publicget_array ( )[ obj ]
Parameters
No parameters for this method
Code
096  public function get_array()
097  {
098      return get_object_vars($this);
099  }
publicget_classname ( )[ obj ]
Parameters
No parameters for this method
Code
086  public function get_classname()
087  {
088      return get_class($this);
089  }
publicget_collapsed_columns ( )[ mysql_table ]
Parameters
No parameters for this method
Code
316  public function get_collapsed_columns()
317  {
318      $columns = $this->columns;
319      $table = $this->parent;
320      while (!is_null($table))
321      {
322          $columns = array_merge($table->columns,$columns);
323          $table = $table->parent;
324      }
325      return $columns;
326  }
publicget_collapsed_unique_keys ( )[ mysql_table ]
Parameters
No parameters for this method
Code
304  public function get_collapsed_unique_keys()
305  {
306      $keys = $this->unique_keys;
307      $table = $this->parent;
308      while (!is_null($table))
309      {
310          $keys = array_merge($this->parent->unique_keys,$keys);
311          $table = $table->parent;
312      }
313      return $keys;
314  }
publicget_dirty ( )[ obj ]
Parameters
No parameters for this method
Code
063  public function get_dirty()
064  {
065      if ($this->id === 0)
066      {
067          $this->_dirty = true;
068      }
069      else
070      {
071          foreach (get_object_vars($this) as $k=>$v)
072          {
073              if ($this->_dirty)
074              {
075                  break;
076              }
077              if (preg_match('/_dirty$/',$k))
078              {
079                  $this->_dirty = $this->_dirty || $v;
080              }
081          }
082      }
083      return $this->_dirty;
084  }
publicget_is_child ( )[ mysql_table ]
Parameters
No parameters for this method
Code
299  public function get_is_child()
300  {
301      return in_array("_id",array_keys($this->columns));
302  }
publicget_json ( )[ obj ]
Parameters
No parameters for this method
Code
101  public function get_json()
102  {
103      $class = new ReflectionClass($this->classname);
104      $methods = $class->getMethods();
105      usort($methods,"sort_reflection_objects");
106      $accessors = array();
107      foreach ($methods as $index=>$method)
108      {
109          $declarer = $method->getDeclaringClass()->name;
110          if (preg_match('/^(get|set|)_(.+)$/',$method->name,$matches))
111          {
112              $accessor = $matches[2];
113              $getset = $matches[1];
114              eval("\$accessors[\$accessor] = \$this->".$accessor.";");
115          }
116      }
117      //return json_encode($this->get_array());
118      return json_encode($accessors);
119  }
publicget_parent ( )[ mysql_table ]
Parameters
No parameters for this method
Code
286  public function get_parent()
287  {
288      if (is_null($this->_parent) && $this->is_child)
289      {
290          $this->_parent = new db_table($this->referenced_tables[array_search($this->primary_key,$this->fk_columns)],$this->database);
291          return $this->_parent;
292      }
293      else
294      {
295          return $this->_parent;
296      }
297  }
publicget_properties ( )[ obj ]
Parameters
No parameters for this method
Code
121  public function get_properties()
122  {
123      return array_keys($this->array);
124  }
protectedget_string ( )[ obj ]
Parameters
No parameters for this method
Code
091  protected function get_string()
092  {
093      return method_exists($this,"__toString") ? $this->__toString() : null;
094  }
public__set ( p , v )[ obj ]
Parameters
requiredp
requiredv
Code
030  public function __set($p,$v)
031  {
032      $methods = get_class_methods($this);
033      $properties = get_object_vars($this);
034      if (in_array("set_$p",$methods))
035      {
036          $this->{"set_$p"}($v);
037      }
038      else if (array_key_exists("_$p",$properties))
039      {
040          if (!preg_match('/_dirty$/',$p) && property_exists($this,"_{$p}_dirty"))
041          {
042              if (is_object($this->{"_$p"}))
043              {
044                  $this->{"_$p"}->dirty = true;
045              }
046              else
047              {
048                  $this->{"_{$p}_dirty"} = $this->{$p}==$v ? false : true;
049              }
050          }
051          $this->{"_$p"} = $v;
052      }
053      else if (in_array("set_" . substr($p,strpos($p,"_")+1), $methods))
054      {
055          $this->{"set_" . substr($p,strpos($p,"_")+1)}($v,substr($p,0,strpos($p,"_")));
056      }
057      else
058      {
059          trigger_error("This object [" . get_class($this) . ":$this->id] has no private property '_$p' for the requested public property '$p'",E_USER_WARNING);
060      }
061  }
 
Accessors
publicarray[ obj ]
Code
096  public function get_array()
097  {
098      return get_object_vars($this);
099  }
publicclassname[ obj ]
Code
086  public function get_classname()
087  {
088      return get_class($this);
089  }
publiccollapsed_columns[ mysql_table ]
Code
316  public function get_collapsed_columns()
317  {
318      $columns = $this->columns;
319      $table = $this->parent;
320      while (!is_null($table))
321      {
322          $columns = array_merge($table->columns,$columns);
323          $table = $table->parent;
324      }
325      return $columns;
326  }
publiccollapsed_unique_keys[ mysql_table ]
Code
304  public function get_collapsed_unique_keys()
305  {
306      $keys = $this->unique_keys;
307      $table = $this->parent;
308      while (!is_null($table))
309      {
310          $keys = array_merge($this->parent->unique_keys,$keys);
311          $table = $table->parent;
312      }
313      return $keys;
314  }
publicdirty[ obj ]
Code
063  public function get_dirty()
064  {
065      if ($this->id === 0)
066      {
067          $this->_dirty = true;
068      }
069      else
070      {
071          foreach (get_object_vars($this) as $k=>$v)
072          {
073              if ($this->_dirty)
074              {
075                  break;
076              }
077              if (preg_match('/_dirty$/',$k))
078              {
079                  $this->_dirty = $this->_dirty || $v;
080              }
081          }
082      }
083      return $this->_dirty;
084  }
publicis_child[ mysql_table ]
Code
299  public function get_is_child()
300  {
301      return in_array("_id",array_keys($this->columns));
302  }
publicjson[ obj ]
Code
101  public function get_json()
102  {
103      $class = new ReflectionClass($this->classname);
104      $methods = $class->getMethods();
105      usort($methods,"sort_reflection_objects");
106      $accessors = array();
107      foreach ($methods as $index=>$method)
108      {
109          $declarer = $method->getDeclaringClass()->name;
110          if (preg_match('/^(get|set|)_(.+)$/',$method->name,$matches))
111          {
112              $accessor = $matches[2];
113              $getset = $matches[1];
114              eval("\$accessors[\$accessor] = \$this->".$accessor.";");
115          }
116      }
117      //return json_encode($this->get_array());
118      return json_encode($accessors);
119  }
publicparent[ mysql_table ]
Code
286  public function get_parent()
287  {
288      if (is_null($this->_parent) && $this->is_child)
289      {
290          $this->_parent = new db_table($this->referenced_tables[array_search($this->primary_key,$this->fk_columns)],$this->database);
291          return $this->_parent;
292      }
293      else
294      {
295          return $this->_parent;
296      }
297  }
publicproperties[ obj ]
Code
121  public function get_properties()
122  {
123      return array_keys($this->array);
124  }
publicstring[ obj ]
Code
091  protected function get_string()
092  {
093      return method_exists($this,"__toString") ? $this->__toString() : null;
094  }
Get Helix PHP Framework at SourceForge.net. Fast, secure and Free Open Source software downloads