3 * @desc The Abstract Data Objects series
5 * @author Marius Orcsik <marius.orcsik@gmail.com>
20 protected $id, // public to allow debugging outside the class
26 * @desc function to implement get_ | set_ virtual methods
28 * @param string $method
32 * @see http://www.ibm.com/developerworks/xml/library/os-php-flexobj/ by Jack Herrington <jherr@pobox.com>
34 function __call ( $method, $args) {
35 $diff = $this->get_members();
36 $all = get_object_vars($this);
38 if ( preg_match( '/set_(.*)/', $method, $found ) ) {
39 // check for fields with $found[1] name
40 if ( array_key_exists( $found[1], $diff) ) {
41 $this->fields
[$found[1]]->setValue($args[0]);
43 // check for obj members with $found[1] name
44 } elseif (array_key_exists( $found[1], $all)){
45 $this->$found[1] = $args[0];
48 } else if ( preg_match( '/get_(.*)/', $method, $found ) ) {
49 if ( array_key_exists( $found[1], $diff ) ) {
50 return $this->fields
[$found[1]]->getValue();
51 } elseif (array_key_exists( $found[1], $all)){
52 return $this->$found[1];
59 * @desc A function to implement a virtual getter member of the class
64 * @see http://www.ibm.com/developerworks/xml/library/os-php-flexobj/ by Jack Herrington <jherr@pobox.com>
66 function __get ( $key ) {
67 return $this->fields
[$key];
71 * @desc A function to implement a virtual setter member for this class
77 * @see http://www.ibm.com/developerworks/xml/library/os-php-flexobj/ by Jack Herrington <jherr@pobox.com>
79 function __set ( $key, $value ) {
81 array_key_exists ($key, $this->get_members()) /*&&
82 $this->isValidMember($this->fields[$key])*/
84 $this->fields
[$key]->set_value ($value);
87 $this->fields
[$key] = new tdoAbstractField ($key, $this->alias
);
88 $this->fields
[$key]->set_value ($value);
92 public function __construct(&$db) {
96 $this->instantiateMembers();
99 public function __destruct() {}
102 * gets the members we consider table fields
104 * @return array ('fieldName' => tdoAbstractField)
106 public function &get_members () {
107 return $this->fields
;
111 * checks if a field is a valid member of the current object
113 * @param tdoAbstractField $incMember
116 public function isValidMember ($incMember) {
118 ! ($incMember instanceof tdoAbstractField
) ||
119 // this prevents an error in php > 5.2 object comparison
120 ! in_array ($incMember, $this->get_members() /**/, true/**/)
128 * instantiating the object's members
130 public function instantiateMembers () {
131 foreach ($this->get_members() as $key => $field) {
132 if ( is_int ($key) && is_string ($field) ) {
133 $this->fields
[$field] = new tdoAbstractField ($field, $this->alias
);
135 // FIXME: this is so bad :D - it implies that the primary key must be the first field containing _id :D
136 if (stristr ($field,'_id') && !isset ($this->id
) && $this->isValidMember ($this->fields
[$field])) {
137 $this->id
= &$this->fields
[$field];
139 unset ($this->fields
[$key]);
143 $this->wheres
= array();
145 // $this->orders = '';
146 $this->refers
= array();
149 * setting the table's index field
151 * @param tdoAbstractField $obj
153 public function set_index (&$obj) {
154 if ($this->isValidMember($obj)) {
156 $this->id
->flags
= PRIMARY
;
161 * setting an alias for the table in case of joins.
162 * this method also sets the alias on all the fields of the current object
164 * @param string $alias
166 public function set_alias ($alias) {
167 foreach ($this->get_members() as $field){
168 if ($field->table
== $this->alias
) {
169 $field->table
= 't'.$alias;
172 $this->alias
= 't'.$alias;
176 * For adding custom sql functions for certain fields
178 * @param string $modif
179 * @param tdoAbstractField $field
181 public function set_fieldModifier ($modif, &$field) {
182 if ($this->isValidMember ($field) && stristr ($modif, '%'))
183 $field->set_modifier ($modif);
187 * method used in join cases to add the joined object's fields
190 * @param array ('fieldName' => tdoAbstractField) $incArr
192 public function addFields (&$incArr) {
193 if (is_array($incArr)) {
194 foreach ($incArr as $fieldName => $field) {
195 $this->fields
[$fieldName] = $field;
201 * Based on field values, we get the _first_ row in the table that matches
203 * TODO: maybe we can have a function that returns _all_ the rows that match
207 public function buildObj () {
208 $sql = $this->buildSql(1);
209 $this->db
->query( $sql );
211 $arr = $this->db
->getAssoc();
212 if (is_array($arr)) {
213 foreach ($arr as $field => $var) {
214 $this->fields
[$field]->set_value ($var);
222 * Gets the row in the table for $id
227 public function get ($id) {
228 $id = $this->db
->escape($id);
230 $this->instantiateMembers();
232 $this->id
->set_value ($id);
236 * Returns the last id of the table
237 * OBS: this assumes that we didn't delete any rows from the table.
241 public function getLastInsertedId () {
242 $sql = $this->db
->_SELECT ($this->id
->name
)
243 .$this->db
->_FROM ($this->name
) . $this->db
->_AS ($this->alias
);
245 $this->addOrder ($this->id
, false);
247 $sql .= $this->db
->_ORDER ($this->outputOrders ());
249 $sql .= $this->db
->_LIMIT (1);
251 $this->db
->query ($sql);
252 return $this->db
->getScalar ();
256 * encapsulating the $this->db->escape() method
258 * @param mixed $value
261 public function escape ($value) {
262 if (is_numeric ($value)) {
265 // this behavior is broken for PostgreSQL as it encloses values in apostrophes
266 return '"' . $this->db
->escape ($value) . '"';
270 * inserting into the database
271 * TODO: multiple inserts to use with loadFromArray
275 public function insert () {
276 $sql = $this->db
->_INSERT ($this->name
);//`'INSERT INTO '.$this->name;
281 $f = $this->get_members();
283 // $sql .= ' '.$this->outputRefers().' SET';
285 foreach ($f as $fieldName => $field) {
286 if ($this->isValidMember ($field) && ($field != $this->id
) && !is_null ($field->value
)) {
287 $value = $this->escape ($field->value
);
288 $value = $field->value
;
290 $fieldStr.= (!empty ($fieldStr) ?
', ' : ' ') . $fieldName;
291 $valueStr.= (!empty ($valueStr) ?
', ' : ' ') . $value;
296 $sql.= ' ('.$fieldStr.') VALUES ('.$valueStr.')';
297 // $sql.= ' ('.$fieldStr.')' . $this->db->_VALUES ($values); // VALUES ('.$valueStr.')';
299 $this->db
->query($sql);
301 return $this->getLastInsertedId();
305 public function update ($id = null) {
307 $id = $this->id
->value
;
311 throw new Exception('Cannot update record in table '.$this->name
.' because an id hasn\'t been provided');
315 $sql = $this->db
->_UPDATE (array ($this->name
, $this->alias
) );
317 if (is_array ($this->refers
) && !empty ($this->refers
)){
318 $this->refers
= array_reverse ($this->refers
);
320 foreach ($this->refers
as $ref)
324 $sql .= $this->db
->_SET();
326 $fields = $this->get_members();
328 foreach ($fields as $fieldName => $field) {
329 if (($field instanceof tdoAbstractField
) && $field != $this->id
) { // TODO: make a more real check for field is an id
330 $value = $field->value
;
333 if ((isset($value) && !is_null($value))) {
334 $sql.= $field->table
.'.'.$fieldName.' = '.$this->escape ($value).', ';
338 $sql = substr ($sql, 0, -2);
340 $sql.= $this->db
->_WHERE($this->alias
.'.'.$this->id
->name
.' = '.$this->escape($id));
342 // var_dump($sql);die ('<br/>update');
343 $this->db
->query($sql);
348 public function replace ( $id = null ) {
350 $id = $this->id
->value
;
353 if (is_null($id) ||
!$this->idExists ($id) ) {
354 return $this->insert ();
356 return $this->update ($id);
360 // TODO: make this the same way the find first method works
361 public function delete ($id = null) {
362 $id = (!is_null ($id) ?
$id :$this->id
->value
);
364 if (!is_null ($id)) {
365 // no need for other wheres
366 $this->wheres
= array();
367 $this->addWhere ($this->id
, '=', $id);
370 if (empty($this->wheres
)) {
374 $temp = $this->alias
;
375 $this->set_alias(null);
378 $sql = 'DELETE FROM '.$this->name
.' WHERE '; //$this->id->name.' = '.$this->escape($value).' LIMIT 1';
379 $sql.= $this->outputWheres();
381 $affRows = $this->db
->query($sql);
383 $this->set_alias($temp);
387 public function reset () {
388 foreach ($this->fields
as $key => $field) {
389 if ($field instanceof tdoAbstractField
) {
390 $this->fields
[$key] = new tdoAbstractField($key, $this->alias
);
394 $this->wheres
= array();
395 $this->groups
= array();
396 // $this->orders = array();
397 $this->refers
= array();
401 public function idExists ($id = null) {
403 $id = $this->id
->value
;
408 $this->id
->set_modifier ('COUNT(%s)');
410 $t = sprintf ($this->id
->modifier
, $this->id
->name
);
412 $sql = $this->db
->_SELECT ($t).
413 $this->db
->_FROM ($this->name
).
414 $this->db
->_WHERE ($this->id
->name
.' = '.$this->escape($id));
416 $this->db
->query($sql);
418 if ($this->db
->getScalar()) {
425 protected function outputFieldList () {
427 $f = $this->get_members();
429 foreach ($f as $fieldName => $field) {
430 if ($this->isValidMember ($field)) {
432 if (!is_null ($field->modifier
)) {
433 // i replaced str_replace ('%s', $curField, '%s', $curField)
434 // as sometimes I might need it for something else than %s
435 $fields .= sprintf ($field->modifier
, (!is_null ($field->table
) ?
$field->table
. '.' : '') . $field->name
) .
436 $this->db
->_AS($field->name
) . ', ';
437 } elseif ( !$field->inWhere ()) {
438 $fields .= (!is_null ($field->table
) ?
$field->table
.'.' : '') . $field->name
. ', ';
442 trigger_error ($fieldName . ' is not a valid member of ' . get_class($this));
443 // throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this));
447 return substr ($fields,0,-2);//$fields;
450 protected function outputWheres ($bIW = true) {
451 // var_dump($this->wheres);
452 return implode ($this->db
->_AND(), $this->wheres
);
455 protected function outputGroups () {
458 $f = $this->get_members ();
459 foreach ($f as $field) {
460 if (!is_null ($field->group
) ) {
461 $groups .= (!empty($groups) ?
', ' : '') .
462 $field->table
. '.' . $field->name
;
469 protected function outputOrders () {
471 $f = $this->get_members();
472 foreach ($f as $field)
473 if (!is_null($field->order
)) {
474 $orders .= (!empty($orders) ?
', ': '') .
475 $field->table
. '.' . $field->name
.
476 ($field->order
== true ?
' ASC' : ' DESC');
481 protected function outputRefers () {
483 if (!empty($this->refers
) && is_array($this->refers
)){
484 $rs = array_reverse ($this->refers
);
485 foreach ($rs as $ref) // using the __toString magic function
491 protected function buildInherentWheres () {
492 $diff = $this->get_members();
493 // let's hope this doesn't break stuff.
494 // it's needed when we use more queries on the same instance of the object :D
496 if (is_array ($diff)) {
497 foreach ($diff as $fieldName => $field) {
498 if ( $this->isValidMember ($field) ) {
499 if (!is_null($field->value
))
500 $this->addWhere ($field, '=', $this->escape($field->value
));
502 trigger_error ($fieldName . ' is not a valid member of ' . get_class($this));
503 // throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this));
508 if (empty ($this->wheres
)) {
510 $this->wheres
[] = new tdoAbstractClause ($t);
515 * function to add an abstract clause to the current object if it doesn't exist
517 * @param tdoAbstractField|tdoAbstractClause $field1
518 * @param string $condition
519 * @param string $field2
521 public function addWhere (&$field1, $condition = null, $field2 = null) {
522 if (($field1 instanceof tdoAbstractClause
) && ($condition == null ||
$field2 == null)) {
525 $w = new tdoAbstractClause ($field1, $condition, $field2);
527 // this might generate an infinite recursion error on some PHP > 5.2 due to object comparison
528 if (!in_array ($w, $this->wheres
/*, true */))
529 $this->wheres
[] = &$w;
532 public function addOrder (&$orderField, $asc = true) {
533 if (!($orderField instanceof tdoAbstractField
) && is_string ($orderField)) {
534 $orderField = &$this->fields
[$orderField];
536 if ($this->isValidMember ($orderField))
537 $orderField->set_order ($asc);
540 public function addGroup (&$groupField) {
541 if (!($groupField instanceof tdoAbstractField
) && is_string ($groupField)) {
542 $groupField = &$this->fields
[$groupField];
544 if (($groupField instanceof tdoAbstractField
)) {
545 $groupField->set_group (true);
554 public function addLimit ($start = 0, $count=null) {
555 if (empty($this->limit
))
556 $this->limit
= $this->db
->_LIMIT ($start, $count);
560 * building a normal SELECT query
566 protected function buildSql ($start = 0, $count = 0) {
567 $sql = $this->db
->_SELECT($this->outputFieldList()). ' FROM '.$this->name
.' AS '.$this->alias
.' ';
569 $this->buildInherentWheres(); // will it work
571 $sql .= $this->outputRefers();
573 $sql .= $this->db
->_WHERE ($this->outputWheres());
575 $sql .= $this->db
->_GROUP ($this->outputGroups());
577 $sql .= $this->db
->_ORDER ($this->outputOrders());
579 if (empty ($this->limit
)) {
580 $this->addLimit ($start, $count);
582 $sql .= $this->limit
;
586 public function find ($start = 0, $count = 0) {
587 $result = $this->db
->query ($this->buildSql(), $start, $count);
591 public function findFirst () {
592 $this->buildInherentWheres();
594 $this->db
->query($this->buildSql(), 0, 1);
595 $row = $this->db
->getAssoc();
597 foreach ($row as $field => $value){
598 $this->fields
[$field]->value
= $value;
602 public function getArray ($start = 0, $count = 0, $orderBy = null) {
603 $this->buildInherentWheres ();
605 $sql = $this->buildSql ($start, $count, $orderBy);
607 $this->db
->query ($sql);
608 return $this->db
->getArray ();
612 * execute a select count () on the current object
616 public function getCount1() {
617 $this->set_fieldModifier('COUNT(%s)', $this->id
);
618 $this->db
->query($this->buildSql());
619 return $this->db
->getScalar();
622 public function getCount() {
624 // it takes into account the counting rows in many2many table relationshit
625 // but it does not for more than one group by
626 foreach ($this->get_members() as $fieldName => $field) {
627 if ($field->get_group() == true) {
628 $what = 'DISTINCT(' . $field->table
.'.' . $fieldName . ')';
635 // made it a bit more clean and less sql portable by adding hard coded
637 $sql = $this->db
->_SELECT (' COUNT(' . $what . ') ');
639 $sql .= $this->db
->_FROM( $this->name
. $this->db
->_AS($this->alias
) );
641 $this->buildInherentWheres();
643 $sql .= $this->outputRefers();
645 $sql .= $this->db
->_WHERE ($this->outputWheres());
647 // this would need to be replaced with a count(distinct(group by column))
648 // because we're using many2many relations
649 // also I do not know how this behaves for:
650 // 1. multiple group by's
651 // 2. !many2many relations.
652 // $sql .= $this->db->_GROUP ($this->outputGroups());
654 $this->db
->query($sql);
655 return $this->db
->getScalar();
658 public function getVector() {
659 // I really don't see why we need this. ?
663 * Function to load the values of current object from an array
664 * of type field_name => field_value
665 * If strict is false, the current object can have fields that are not already
666 * present in $this->fields[]
668 * @param array $valArray
669 * @param bool $strict
671 public function loadFromArray ($valArray, $strict = true) {
672 foreach ($valArray as $fieldName => $value) {
673 if (array_key_exists ($fieldName, $this->fields
)) {
674 $this->fields
[$fieldName]->set_value($value);
675 } elseif (!$strict) {
676 // if the field name is not in the field list of the current object
677 // it means that the valArray object is got from an JOIN sql
678 $this->fields
[$fieldName] = new tdoAbstractField ($value,'j1');
679 $this->fields
[$fieldName]->set_value ($value);
685 * FIXME: make it work when composing with the same object
687 * @param tdoAbstract $incOb
690 private function composeObj ($incOb) {
691 if (!($incOb instanceof tdoAbstract
))
693 $refs = count($this->refers
);
695 foreach ($incOb->refers
as $alias => $ref) {
698 $this->refers
[$tAl] = $ref;//str_replace(array($alias, $aliases[$alias][1]), array($tAl, $aliases[$alias][2]), $ref);
699 $this->refers
[$tAl]->set_state ($tAl);
704 * Function to execute a join between two tdoAbstract objects
706 * @param string $jType
707 * @param tdoAbstractField $thisJField
708 * @param tdoAbstract $incOb
709 * @param tdoAbstractField $incObJField
712 public function joinWith ($jType = null, &$thisJField = null, &$incOb = null, &$incObJField = null) {
714 !tdoAbstractJoin
::isValidType ($jType) ||
715 !$this->isValidMember ($thisJField)
718 $this->composeObj ($incOb);
720 $tAl = (count($this->refers
));
722 trigger_error('Join aborted for table '.$this->name
.': Too many tables; MySQL can only use 61 tables in a join', E_USER_NOTICE
);
725 $incOb->set_alias($tAl);
727 if($thisJField == null ||
!($thisJField instanceof tdoAbstractField
))
728 $thisJField = $this->id
;
730 if($incObJField == null ||
!($incObJField instanceof tdoAbstractField
))
731 $incObJField = $incOb->id
;
733 $this->refers
[$tAl] = new tdoAbstractJoin ($jType, $this, $incOb, $thisJField, $incObJField, $tAl);
734 $this->refers
[$tAl]->set_state ($tAl);
736 // var_dump ($this->refers);
740 * function to dump a <type>Sql
741 * problem with the field types. :D
743 * @param tdoAbstract $obj
745 static public function dumpSchema ($obj) {
746 if ($obj instanceof tdoAbstract)
747 throw new Exception('Can\'t generate sql dump');
749 $sql = 'CREATE TABLE '. $obj->name . ' (';
751 foreach ($obj->getFields() as $fieldName => $data) {
752 $sql .= $fieldName.' '.$data[0].', ';
761 * class to abstract a where clause in a SQL query
762 * TODO: add possibility of complex wheres: (t1 condition1 OR|AND|XOR t2.condition2)
763 * TODO: abstract the condition part of a where clause - currently string based :D
765 class tdoAbstractClause
{
766 protected $subject, $predicate, $predicative;
769 * initializing a WHERE|ON clause
771 * @param tdoAbstractClause|tdoAbstractField $subject
772 * @param string $predicate
773 * @param tdoAbstractClause|tdoAbstractField|null $complement
775 public function __construct ($subject, $predicate = null, $predicative = null) {
776 // I must be careful with the $subject == 1 because (int)object == 1
777 if (($subject === 1 ||
is_string($subject)) && $predicate == null && $predicative == null) {
778 $this->subject
= $subject;
779 $this->predicate
= '';
780 $this->predicative
= '';
784 if (($subject instanceof tdoAbstractField
) ||
($subject instanceof tdoAbstractClause
)) {
785 $this->subject
= &$subject;
787 $subject->set_where
= true;
789 $this->predicative
= &$predicative;
791 if ($this->validPredicative ($predicate))
792 $this->predicate
= $predicate;
796 $this->predicate
= '';
797 $this->predicative
= '';
802 public function __destruct () {}
804 public function __toString () {
805 // var_dump($this->subject, $this->predicate, $this->predicative);
807 if ($this->subject
=== '1' ||
is_string($this->subject
)) {
808 // var_dump($this->subject);
809 return (string)$this->subject
;
810 } elseif ($this->subject
instanceof tdoAbstractClause
) {
811 $subStr = (string)$this->subject
;
812 } elseif ($this->subject
instanceof tdoAbstractField
) {
813 // without $this->subject->table != 't' we have a bug in the delete op
814 $subStr = ($this->subject
->table
!= 't' ?
$this->subject
->table
.'.': '').$this->subject
->name
;
819 if (is_null($this->predicative
)) {
820 if ($this->validPredicative ($this->predicate
)) {
824 } elseif (is_numeric($this->predicative
)) {
825 $preStr = $this->predicative
;
826 } elseif (is_string($this->predicative
)) {
827 $preStr = $this->predicative
;
829 if ($this->predicate
== 'LIKE') {
830 $preStr = '%'.$this->predicate
.'%';
833 $preStr = (stripos($preStr, '"') !== 0 ?
'"'.$preStr.'"' : $preStr);//'"'.$preStr.'"';
834 } elseif (is_array($this->predicative
)) {
835 $preStr = '("'.implode('", "',$this->predicative
).'")';
836 } elseif ($this->predicative
instanceof tdoAbstractfield
) {
837 $preStr = ($this->predicative
->table
!= 't' ?
$this->predicative
->table
.'.': '').$this->predicative
->name
;
838 } elseif ($this->predicative
instanceof tdoAbstractClause
) {
840 $preStr = $this->predicative
;
843 $retStr = $subStr.' '.$this->predicate
.' '.$preStr;
844 if (($this->subject
instanceof tdoAbstractClause
) && ($this->predicative
instanceof tdoAbstractClause
))
845 return '('.$retStr.')';
850 private function validPredicative ($predicate) {
851 // need to find a way to abstract these
852 // $validPredicates = array (
867 if ($this->predicative
instanceof tdoAbstractClause
) {
868 // we'll have Subject AND|OR|XOR Predicative
869 $validPredicates = array (
876 } elseif (($this->predicative
instanceof tdoAbstractField
) ||
is_numeric($this->predicative
)) {
877 // we'll have Subject =|!= Predicative
878 $validPredicates = array (
886 } elseif (is_array($this->predicative
)) {
887 $validPredicates = array (
891 } elseif (is_string($this->predicative
)) {
892 $validPredicates = array (
901 } elseif (is_null($this->predicative
)) {
902 $validPredicates = array (
908 return in_array($predicate, $validPredicates);
910 // if (in_array($predicate, $validPredicates) && (($predicative instanceof tdoAbstractClause) || ($predicative instanceof tdoAbstractField)))
916 class tdoAbstractJoin
{
917 static public $validTypes = array (
932 public function __construct ($type, &$lt, &$rt, &$lf, &$rf, $state) {
933 if ($rt instanceof tdoAbstract ||
934 $lt instanceof tdoAbstract
936 $this->leftTable
= &$lt;
937 $this->rightTable
= &$rt;
939 if (tdoAbstractJoin
::isValidType($type))
943 $lf instanceof tdoAbstractField
&&
944 $rf instanceof tdoAbstractField
946 $this->rightField
= &$rf;
947 $this->leftField
= &$lf;
950 $this->state
= $state;
953 $this->composeFields ();
954 $this->composeWheres ();
958 public function __destruct () {}
960 public function __toString() {
961 $lAlias = $this->leftTable
->get_alias();
963 return (string)$this->type
.' JOIN '.$this->rightTable
->get_name().
964 ' AS t'.$this->state
.' ON '.(isset($lAlias) ?
$lAlias : $this->leftTable
->get_name()).
965 '.'.$this->leftField
->name
.
966 ' = t'.$this->state
.'.'.$this->rightField
->name
.' ';
970 * Will compose the $rightTable and $leftTable fields
973 public function composeFields () {
974 $leftFields = $this->leftTable
->get_members();
976 $this->rightTable
->set_alias($this->state
);
977 $rightFields = $this->rightTable
->get_members();
980 $this->leftTable
->addFields($rightFields);
984 * Will compose the $rightTable and $leftTable WHERE clauses
987 public function composeWheres () {
988 if (!is_array($this->leftTable
->wheres
))
989 $this->leftTable
->wheres
= array();
990 if (!is_array($this->rightTable
->wheres
))
991 $this->rightTable
->wheres
= array();
993 // var_dump($this->rightTable->wheres, $this->leftTable );die;
994 foreach ($this->rightTable
->wheres
as $where) {
995 $this->leftTable
->addWhere ($where);
997 $this->leftTable
->wheres
= array_merge($this->rightTable
->wheres
, $this->leftTable
->wheres
);
1000 public function set_state ($st) {
1004 static public function isValidType ($inc) {
1005 if (in_array($inc, tdoAbstractJoin
::$validTypes))
1011 define ('INDEX', 1);
1012 define ('PRIMARY', 2);
1013 define ('UNIQUE', 4);
1014 define ('FULLTEXT', 8);
1016 class tdoAbstractField
{
1017 static public $validTypes = array (
1027 protected $name, $type, $flags, $value, $table, $modifier = null, $order = null, $group = null, $where = false;
1029 public function __construct ($incName, $incTable, $incType='INT', $incFlags=0) {
1030 $this->name
= $incName;
1031 $this->table
= $incTable;
1032 $this->type
= $incType;
1033 $this->flags
= $incFlags;
1036 public function __set ( $key, $value ) {
1037 if ( array_key_exists ($key, get_object_vars($this)) ) {
1038 $this->$key = $value;
1040 // if ($key == 'where')
1043 if ( is_null ($this->type
) )
1051 public function __get ( $key ) {
1055 public function __call ( $method, $args) {
1056 $all = get_object_vars($this);
1058 if ( preg_match( '/set_(.*)/', $method, $found ) ) {
1059 if (array_key_exists( $found[1], $all)){
1060 $this->$found[1] = $args[0];
1063 } elseif ( preg_match( '/get_(.*)/', $method, $found ) ) {
1064 if (array_key_exists( $found[1], $all)){
1065 return $this->$found[1];
1071 public function __destruct () {}
1073 public function __toString () {
1074 return (string)$this->value
;
1077 // public function inWhere () {
1078 // return $this->where;
1081 static public function isValidType ($inc) {
1082 if (in_array($inc, tdoAbstractField
::$validTypes)){
1088 public function set_modifier ($modif) {
1089 // if (stristr($modif, '%'))
1090 $this->modifier
= $modif;
1093 public function set_value ($value) {
1094 $this->value
= $value;
1097 public function set_group ($true = true) {
1098 $this->group
= (bool)$true;
1101 public function set_order ($asc = true) {
1102 $this->order
= (bool)$asc;
1105 public function isIndex() {
1106 return (($this->flags
& INDEX
) == INDEX
);
1109 public function isPrimary() {
1110 return (($this->flags
& PRIMARY
) == PRIMARY
);
1113 public function isFullText() {
1114 return (($this->flags
& FULLTEXT
) == FULLTEXT
);
1116 public function isUnique () {
1117 return (($this->flags
& UNIQUE
) == UNIQUE
);
1119 public function setType () {
1120 $incValue = $this->value
;
1122 if (is_int($incValue)) {
1123 $this->type
= 'INT';
1124 } elseif (is_float($incValue)) {
1125 $this->type
= 'FLOAT';
1126 } elseif (is_string($incValue)) {
1127 if (strtotime($this->value
)){
1128 $this->type
= 'DATE';
1129 }elseif (strlen($incValue) > 255)
1130 $this->type
= 'TEXT';
1132 $this->type
= 'VARCHAR';