Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezpersistentobject.php
Go to the documentation of this file.
1 <?php
32 class eZPersistentObject
33 {
41 public $PersistentDataDirty;
42
52 public function __construct( $row = null )
53 {
54 $this->PersistentDataDirty = false;
55 if ( is_numeric( $row ) )
56 $row = $this->fetch( $row, false );
57 $this->fill( $row );
58 }
59
64 public function eZPersistentObject( $row = null )
65 {
66 self::__construct( $row );
67 }
68
80 public function fill( $row )
81 {
82 if ( !is_array( $row ) )
83 return;
84 $def = $this->definition();
85
86 foreach ( $def["fields"] as $key => $item )
87 {
88 if ( isset( $item['name'] ) )
89 {
90 $item = $item['name'];
91 }
92 $this->$item = isset( $row[$key] ) ? $row[$key] : null;
93 }
94
95 return true;
96 }
97
110 public static function replaceFieldsWithShortNames( eZDBInterface $db, array $fieldDefs, &$fields )
111 {
112 if ( !$db->useShortNames() || !$fields )
113 return;
114
115 $short_fields_names = array();
116 foreach ( $fields as $key => $val )
117 {
118 if( is_numeric( $key ) ) // $fields is not an associative array
119 {
120 if ( array_key_exists( $val, $fieldDefs ) &&
121 array_key_exists( 'short_name', $fieldDefs[$val] ) )
122 {
123 $short_fields_names[$key] = $fieldDefs[$val]['short_name'];
124 }
125 else
126 $short_fields_names[$key] = $val;
127 }
128 else // $fields is an associative array
129 {
130 if ( array_key_exists( $key, $fieldDefs ) &&
131 array_key_exists( 'short_name', $fieldDefs[$key] ) )
132 {
133 $newkey = $fieldDefs[$key]['short_name'];
134 }
135 else
136 $newkey = $key;
137 $short_fields_names[$newkey] = $val;
138 }
139
140 }
141 $fields = $short_fields_names;
142 }
143
154 protected static function replaceFieldsWithLongNames( eZDBInterface $db, array $fieldDefs, &$fields )
155 {
156 if ( !$db->useShortNames() || !$fields )
157 return;
158
159 foreach ( $fieldDefs as $fieldName => $fieldDefinition )
160 {
161 if ( !isset( $fieldDefinition['short_name'] ) )
162 {
163 continue;
164 }
165 $shortName = $fieldDefinition['short_name'];
166 if ( !isset( $fields[$shortName] ) )
167 {
168 continue;
169 }
170 $fields[$fieldName] = $fields[$shortName];
171 unset( $fields[$shortName] );
172 }
173 }
174
187 public static function count( $def, $conds = null, $field = null )
188 {
189 if ( !isset( $field ) )
190 {
191 $field = '*';
192 }
193 $customFields = array( array( 'operation' => 'COUNT( ' . $field . ' )', 'name' => 'row_count' ) );
194 $rows = eZPersistentObject::fetchObjectList( $def, array(), $conds, array(), null, false, false, $customFields );
195 return $rows[0]['row_count'];
196 }
197
213 public static function fetchObject( $def, $field_filters, $conds, $asObject = true, $grouping = null, $custom_fields = null )
214 {
215 $rows = eZPersistentObject::fetchObjectList( $def, $field_filters, $conds,
216 array(), null, $asObject,
217 $grouping, $custom_fields );
218 if ( $rows )
219 return $rows[0];
220 return null;
221 }
222
239 public function remove( $conditions = null, $extraConditions = null )
240 {
241 $def = $this->definition();
242 $keys = $def["keys"];
243 if ( !is_array( $conditions ) )
244 {
245 $conditions = array();
246 foreach ( $keys as $key )
247 {
248 $value = $this->attribute( $key );
249 $conditions[$key] = $value;
250 }
251 }
252 eZPersistentObject::removeObject( $def, $conditions, $extraConditions );
253 }
254
271 public static function removeObject( $def, $conditions = null, $extraConditions = null )
272 {
273 $db = eZDB::instance();
274
275 $table = $def["name"];
276 if ( is_array( $extraConditions ) )
277 {
278 foreach ( $extraConditions as $key => $cond )
279 {
280 $conditions[$key] = $cond;
281 }
282 }
283
284 // substitute fields mentioned the conditions whith their short names (if any)
285 $fields = $def['fields'];
286 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $conditions );
287
288 $cond_text = eZPersistentObject::conditionText( $conditions );
289
290 $db->query( "DELETE FROM $table $cond_text" );
291 }
292
304 public function store( $fieldFilters = null )
305 {
306 eZPersistentObject::storeObject( $this, $fieldFilters );
307 }
308
319 public function sync( $fieldFilters = null )
320 {
321 if ( $this->hasDirtyData() )
322 $this->store( $fieldFilters );
323 }
324
338 public static function storeObject( $obj, $fieldFilters = null )
339 {
340 $db = eZDB::instance();
341 $useFieldFilters = ( isset( $fieldFilters ) && is_array( $fieldFilters ) && $fieldFilters );
342
343 $def = $obj->definition();
344 $fields = $def["fields"];
345 $keys = $def["keys"];
346 $table = $def["name"];
347 $relations = isset( $def["relations"] ) ? $def["relations"] : null;
348 $insert_object = false;
349 $exclude_fields = array();
350 foreach ( $keys as $key )
351 {
352 $value = $obj->attribute( $key );
353 if ( $value === null )
354 {
355 $insert_object = true;
356 $exclude_fields[] = $key;
357 }
358 }
359
360 if ( $useFieldFilters )
361 $insert_object = false;
362
363 $use_fields = array_diff( array_keys( $fields ), $exclude_fields );
364 // If we filter out some of the fields we need to intersect it with $use_fields
365 if ( is_array( $fieldFilters ) )
366 $use_fields = array_intersect( $use_fields, $fieldFilters );
367 $doNotEscapeFields = array();
368 $changedValueFields = array();
369 $numericDataTypes = array( 'integer', 'float', 'double' );
370
371 foreach ( $use_fields as $field_name )
372 {
373 $field_def = $fields[$field_name];
374 $value = $obj->attribute( $field_name );
375
376 if ( $value === null )
377 {
378 if ( ! is_array( $field_def ) )
379 {
380 $exclude_fields[] = $field_name;
381 }
382 else
383 {
384 if ( array_key_exists( 'default', $field_def ) &&
385 ( $field_def['default'] !== null ||
386 ( $field_name == 'data_int' &&
387 array_key_exists( 'required', $field_def ) &&
388 $field_def[ 'required' ] == false ) ) )
389 {
390 $obj->setAttribute( $field_name, $field_def[ 'default' ] );
391 }
392 else
393 {
394 //if ( in_array( $field_def['datatype'], $numericDataTypes )
395 $exclude_fields[] = $field_name;
396 }
397 }
398 }
399
400 if ( strlen( (string) $value ) == 0 &&
401 is_array( $field_def ) &&
402 in_array( $field_def['datatype'], $numericDataTypes ) &&
403 array_key_exists( 'default', $field_def ) &&
404 ( $field_def[ 'default' ] === null || is_numeric( $field_def[ 'default' ] ) ) )
405 {
406 $obj->setAttribute( $field_name, $field_def[ 'default' ] );
407 }
408
409 if ( $value !== null &&
410 $field_def['datatype'] === 'string' &&
411 array_key_exists( 'max_length', $field_def ) &&
412 $field_def['max_length'] > 0 )
413 {
414 $obj->setAttribute( $field_name, $db->truncateString( $value, $field_def['max_length'], $field_name ) );
415 }
416 $bindDataTypes = array( 'text' );
417 if ( $db->bindingType() != eZDBInterface::BINDING_NO &&
418 $db->countStringSize( $value ) > 2000 &&
419 is_array( $field_def ) &&
420 in_array( $field_def['datatype'], $bindDataTypes )
421 )
422 {
423 $boundValue = $db->bindVariable( $value, $field_def );
424 // $obj->setAttribute( $field_name, $value );
425 $doNotEscapeFields[] = $field_name;
426 $changedValueFields[$field_name] = $boundValue;
427 }
428
429 }
430 $key_conds = array();
431 foreach ( $keys as $key )
432 {
433 $value = $obj->attribute( $key );
434 $key_conds[$key] = $value;
435 }
436 unset( $value );
437
438 $important_keys = $keys;
439 if ( is_array( $relations ) )
440 {
441 // $important_keys = array();
442 foreach( $relations as $relation => $relation_data )
443 {
444 if ( !in_array( $relation, $keys ) )
445 $important_keys[] = $relation;
446 }
447 }
448 if ( count( $important_keys ) == 0 && !$useFieldFilters )
449 {
450 $insert_object = true;
451 }
452 else if ( !$insert_object )
453 {
454 $rows = eZPersistentObject::fetchObjectList( $def, $keys, $key_conds,
455 array(), null, false,
456 null, null );
457 if ( count( $rows ) == 0 )
458 {
459 /* If we only want to update some fields in a record
460 * and that records does not exist, then we should do nothing, only return.
461 */
462 if ( $useFieldFilters )
463 return;
464
465 $insert_object = true;
466 }
467 }
468
469 if ( $insert_object )
470 {
471 // Note: When inserting we cannot hone the $fieldFilters parameters
472
473 $use_fields = array_diff( array_keys( $fields ), $exclude_fields );
474 $use_field_names = $use_fields;
475 if ( $db->useShortNames() )
476 {
477 $use_short_field_names = $use_field_names;
478 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $use_short_field_names );
479 $field_text = implode( ', ', $use_short_field_names );
480 unset( $use_short_field_names );
481 }
482 else
483 $field_text = implode( ', ', $use_field_names );
484
485 $use_values_hash = array();
486 $escapeFields = array_diff( $use_fields, $doNotEscapeFields );
487
488 foreach ( $escapeFields as $key )
489 {
490 $value = $obj->attribute( $key );
491 $field_def = $fields[$key];
492
493 if ( $field_def['datatype'] == 'float' || $field_def['datatype'] == 'double' )
494 {
495 if ( $value === null )
496 {
497 $use_values_hash[$key] = 'NULL';
498 }
499 else
500 {
501 $use_values_hash[$key] = sprintf( '%F', $value );
502 }
503 }
504 else if ( $field_def['datatype'] == 'int' || $field_def['datatype'] == 'integer' )
505 {
506 if ( $value === null )
507 {
508 $use_values_hash[$key] = 'NULL';
509 }
510 else
511 {
512 $use_values_hash[$key] = sprintf( '%d', $value );
513 }
514 }
515 else
516 {
517 // Note: for more colherence, we might use NULL for sql strings if the php value is NULL and not an empty sring
518 // but to keep compatibility with ez db, where most string columns are "not null default ''",
519 // and code feeding us a php null value without meaning it, we do not.
520 $use_values_hash[$key] = "'" . $db->escapeString( $value ) . "'";
521 }
522 }
523 foreach ( $doNotEscapeFields as $key )
524 {
525 $use_values_hash[$key] = $changedValueFields[$key];
526 }
527 $use_values = array();
528 foreach ( $use_field_names as $field )
529 $use_values[] = $use_values_hash[$field];
530 unset( $use_values_hash );
531 $value_text = implode( ", ", $use_values );
532
533 if( $db->DatabaseName() == 'sqlite' )
534 {
535 $sql = "INSERT OR REPLACE INTO $table ($field_text) VALUES($value_text)";
536 }
537 else
538 {
539 $sql = "INSERT INTO $table ($field_text) VALUES($value_text)";
540 }
541 $db->query( $sql );
542
543 if ( isset( $def["increment_key"] ) &&
544 is_string( $def["increment_key"] ) &&
545 !( $obj->attribute( $def["increment_key"] ) > 0 ) )
546 {
547 $inc = $def["increment_key"];
548 $id = $db->lastSerialID( $table, $inc );
549 if ( $id !== false )
550 $obj->setAttribute( $inc, $id );
551 }
552 }
553 else
554 {
555 $use_fields = array_diff( array_keys( $fields ), array_merge( $keys, $exclude_fields ) );
556 if ( count( $use_fields ) > 0 )
557 {
558 // If we filter out some of the fields we need to intersect it with $use_fields
559 if ( is_array( $fieldFilters ) )
560 $use_fields = array_intersect( $use_fields, $fieldFilters );
561 $use_field_names = array();
562 foreach ( $use_fields as $key )
563 {
564 if ( $db->useShortNames() && is_array( $fields[$key] ) && array_key_exists( 'short_name', $fields[$key] ) && strlen( $fields[$key]['short_name'] ) > 0 )
565 $use_field_names[$key] = $fields[$key]['short_name'];
566 else
567 $use_field_names[$key] = $key;
568 }
569
570 $field_text = "";
571 $field_text_len = 0;
572 $i = 0;
573
574
575 foreach ( $use_fields as $key )
576 {
577 $value = $obj->attribute( $key );
578
579 if ( $fields[$key]['datatype'] == 'float' || $fields[$key]['datatype'] == 'double' )
580 {
581 if ( $value === null )
582 $field_text_entry = $use_field_names[$key] . '=NULL';
583 else
584 $field_text_entry = $use_field_names[$key] . "=" . sprintf( '%F', $value );
585 }
586 else if ($fields[$key]['datatype'] == 'int' || $fields[$key]['datatype'] == 'integer' )
587 {
588 if ( $value === null )
589 $field_text_entry = $use_field_names[$key] . '=NULL';
590 else
591 $field_text_entry = $use_field_names[$key] . "=" . sprintf( '%d', $value );
592 }
593 else if ( in_array( $use_field_names[$key], $doNotEscapeFields ) )
594 {
595 $field_text_entry = $use_field_names[$key] . "=" . $changedValueFields[$key];
596 }
597 else
598 {
599 $field_text_entry = $use_field_names[$key] . "='" . $db->escapeString( $value ) . "'";
600 }
601
602 $field_text_len += strlen( $field_text_entry );
603 $needNewline = false;
604 if ( $field_text_len > 60 )
605 {
606 $needNewline = true;
607 $field_text_len = 0;
608 }
609 if ( $i > 0 )
610 $field_text .= "," . ($needNewline ? "\n " : ' ');
611 $field_text .= $field_text_entry;
612 ++$i;
613 }
614 $cond_text = eZPersistentObject::conditionText( $key_conds );
615 $sql = "UPDATE $table SET $field_text$cond_text";
616 $db->query( $sql );
617 }
618 }
619 $obj->setHasDirtyData( false );
620 }
621
628 public static function conditionText( $conditions )
629 {
630 return eZPersistentObject::conditionTextByRow( $conditions, null );
631 }
632
643 public static function conditionTextByRow( $conditions, $row )
644 {
645 $db = eZDB::instance();
646
647 $where_text = "";
648 if ( is_array( $conditions ) and
649 count( $conditions ) > 0 )
650 {
651 $where_text = " WHERE ";
652 $i = 0;
653 foreach ( $conditions as $id => $cond )
654 {
655 if ( $i > 0 )
656 $where_text .= " AND ";
657 if ( is_array( $row ) )
658 {
659 $where_text .= $cond . "='" . $db->escapeString( $row[$cond] ) . "'";
660 }
661 else
662 {
663 if ( is_array( $cond ) )
664 {
665 if ( is_array( $cond[0] ) )
666 {
667 $where_text .= $id . ' IN ( ';
668 $j = 0;
669 foreach ( $cond[0] as $value )
670 {
671 if ( $j > 0 )
672 $where_text .= ", ";
673 $where_text .= "'" . $db->escapeString( $value ) . "'";
674 ++$j;
675 }
676 $where_text .= ' ) ';
677 }
678 else if ( is_array( $cond[1] ) )
679 {
680 $range = $cond[1];
681 $where_text .= "$id BETWEEN '" . $db->escapeString( $range[0] ) . "' AND '" . $db->escapeString( $range[1] ) . "'";
682 }
683 else
684 {
685 switch ( $cond[0] )
686 {
687 case '>=':
688 case '<=':
689 case '<':
690 case '>':
691 case '=':
692 case '<>':
693 case '!=':
694 case 'like':
695 {
696 $where_text .= $db->escapeString( $id ) . " " . $cond[0] . " '" . $db->escapeString( $cond[1] ) . "'";
697 } break;
698 default:
699 {
700 eZDebug::writeError( "Conditional operator '$cond[0]' is not supported.", __METHOD__ );
701 } break;
702 }
703
704 }
705 }
706 else
707 $where_text .= $db->escapeString( $id ) . "='" . $db->escapeString( $cond ) . "'";
708 }
709 ++$i;
710 }
711 }
712 return $where_text;
713 }
714
806 public static function fetchObjectList( $def,
807 $field_filters = null,
808 $conds = null,
809 $sorts = null,
810 $limit = null,
811 $asObject = true,
812 $grouping = false,
813 $custom_fields = null,
814 $custom_tables = null,
815 $custom_conds = null )
816 {
817 $db = eZDB::instance();
818 $fields = $def["fields"];
819 $tables = $def["name"];
820 $class_name = $def["class_name"];
821 if ( is_array( $custom_tables ) )
822 {
823 foreach( $custom_tables as $custom_table )
824 $tables .= ', ' . $db->escapeString( $custom_table );
825 }
826 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $conds );
827 if ( is_array( $field_filters ) )
828 $field_array = array_unique( array_intersect(
829 $field_filters, array_keys( $fields ) ) );
830 else
831 $field_array = array_keys( $fields );
832 if ( $custom_fields !== null and is_array( $custom_fields ) )
833 {
834 foreach( $custom_fields as $custom_field )
835 {
836 if ( is_array( $custom_field ) )
837 {
838 $custom_text = $custom_field["operation"];
839 if ( isset( $custom_field["name"] ) )
840 {
841 $field_name = $custom_field["name"];
842 $custom_text .= " AS $field_name";
843 }
844 }
845 else
846 {
847 $custom_text = $custom_field;
848 }
849 $field_array[] = $custom_text;
850 }
851 }
852 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $field_array );
853 $field_text = '';
854 $i = 0;
855 foreach ( $field_array as $field_item )
856 {
857 if ( ( $i % 7 ) == 0 and
858 $i > 0 )
859 $field_text .= ", ";
860 else if ( $i > 0 )
861 $field_text .= ', ';
862 $field_text .= $field_item;
863 ++$i;
864 }
865
866 $where_text = eZPersistentObject::conditionText( $conds );
867 if ( $custom_conds )
868 $where_text .= $custom_conds;
869
870 $sort_text = "";
871 if ( $sorts !== false and ( isset( $def["sort"] ) or is_array( $sorts ) ) )
872 {
873 $sort_list = array();
874 if ( is_array( $sorts ) )
875 {
876 $sort_list = $sorts;
877 }
878 else if ( isset( $def['sort'] ) )
879 {
880 $sort_list = $def["sort"];
881 }
882 if ( count( $sort_list ) > 0 )
883 {
884 $sort_text = " ORDER BY ";
885 $i = 0;
886 foreach ( $sort_list as $sort_id => $sort_type )
887 {
888 if ( $i > 0 )
889 $sort_text .= ", ";
890 if ( $sort_type == "desc" )
891 $sort_text .= "$sort_id DESC";
892 else
893 $sort_text .= "$sort_id ASC";
894 ++$i;
895 }
896 }
897 }
898
899 $grouping_text = "";
900 if ( isset( $def["grouping"] ) or ( is_array( $grouping ) and count( $grouping ) > 0 ) )
901 {
902 $grouping_list = isset( $def["grouping"] ) ? $def["grouping"] : array();
903 if ( is_array( $grouping ) )
904 $grouping_list = $grouping;
905 if ( count( $grouping_list ) > 0 )
906 {
907 $grouping_text = " GROUP BY ";
908 $i = 0;
909 foreach ( $grouping_list as $grouping_id )
910 {
911 if ( $i > 0 )
912 $grouping_text .= ", ";
913 $grouping_text .= "$grouping_id";
914 ++$i;
915 }
916 }
917 }
918
919 $db_params = array();
920 if ( is_array( $limit ) )
921 {
922 if ( isset( $limit["offset"] ) )
923 {
924 $db_params["offset"] = $limit["offset"];
925 }
926 if ( isset( $limit['limit'] ) )
927 {
928 $db_params["limit"] = $limit["limit"];
929 }
930 else
931 {
932 $db_params["limit"] = $limit["length"];
933 }
934 }
935
936 $sqlText = "SELECT $field_text
937 FROM $tables" .
938 $where_text .
939 $grouping_text .
940 $sort_text;
941 $rows = $db->arrayQuery( $sqlText,
942 $db_params );
943
944 // Indicate that a DB error occured.
945 if ( $rows === false )
946 return null;
947
948 $objectList = eZPersistentObject::handleRows( $rows, $class_name, $asObject );
949 return $objectList;
950 }
951
963 public static function handleRows( $rows, $class_name, $asObject )
964 {
965 $objectDefinition = $class_name::definition();
966
967 if ( is_array( $rows ) )
968 {
969 $db = eZDB::instance();
970 foreach( $rows as &$row )
971 {
972 self::replaceFieldsWithLongNames( $db, $objectDefinition['fields'], $row );
973 }
974 }
975
976 if ( $asObject )
977 {
978 $objects = array();
979 if ( is_array( $rows ) )
980 {
981 foreach ( $rows as &$row )
982 {
983 $objects[] = new $class_name( $row );
984 }
985 }
986 return $objects;
987 }
988 else
989 return $rows;
990 }
991
1007 public static function swapRow( $table, $keys, $order_id, $rows, $id1, $id2 )
1008 {
1009 $db = eZDB::instance();
1010 $text = $order_id . "='" . $db->escapeString( $rows[$id1][$order_id] ) . "' WHERE ";
1011 $i = 0;
1012 foreach ( $keys as $key )
1013 {
1014 if ( $i > 0 )
1015 $text .= " AND ";
1016 $text .= $key . "='" . $db->escapeString( $rows[$id2][$key] ) . "'";
1017 ++$i;
1018 }
1019 return "UPDATE $table SET $text";
1020 }
1021
1034 public static function newObjectOrder( $def, $orderField, $conditions )
1035 {
1036 $db = eZDB::instance();
1037 $table = $def["name"];
1038 $keys = $def["keys"];
1039
1040 $cond_text = eZPersistentObject::conditionText( $conditions );
1041 $rows = $db->arrayQuery( "SELECT MAX($orderField) AS $orderField FROM $table $cond_text" );
1042 if ( count( $rows ) > 0 and isset( $rows[0][$orderField] ) )
1043 return $rows[0][$orderField] + 1;
1044 else
1045 return 1;
1046 }
1047
1069 public static function reorderObject( $def, $orderField, $conditions, $down = true )
1070 {
1071 $db = eZDB::instance();
1072 $table = $def["name"];
1073 $keys = $def["keys"];
1074
1075 reset( $orderField );
1076 $order_id = key( $orderField );
1077 $order_val = $orderField[$order_id];
1078 if ( $down )
1079 {
1080 $order_operator = ">=";
1081 $order_type = "asc";
1082 $order_add = -1;
1083 }
1084 else
1085 {
1086 $order_operator = "<=";
1087 $order_type = "desc";
1088 $order_add = 1;
1089 }
1090 $fields = array_merge( $keys, array( $order_id ) );
1091 $rows = eZPersistentObject::fetchObjectList( $def,
1092 $fields,
1093 array_merge( $conditions,
1094 array( $order_id => array( $order_operator,
1095 $order_val ) ) ),
1096 array( $order_id => $order_type ),
1097 array( "length" => 2 ),
1098 false );
1099 if ( count( $rows ) == 2 )
1100 {
1101 $swapSQL1 = eZPersistentObject::swapRow( $table, $keys, $order_id, $rows, 1, 0 );
1102 $swapSQL2 = eZPersistentObject::swapRow( $table, $keys, $order_id, $rows, 0, 1 );
1103 $db->begin();
1104 $db->query( $swapSQL1 );
1105 $db->query( $swapSQL2 );
1106 $db->commit();
1107 }
1108 else
1109 {
1110 $tmp = eZPersistentObject::fetchObjectList( $def,
1111 $fields,
1112 $conditions,
1113 array( $order_id => $order_type ),
1114 array( "length" => 1 ),
1115 false );
1116 $where_text = eZPersistentObject::conditionTextByRow( $keys, $rows[0] );
1117 $db->query( "UPDATE $table SET $order_id='" . ( $tmp[0][$order_id] + $order_add ) .
1118 "'$where_text" );
1119 }
1120 }
1121
1166 public static function definition()
1167 {
1168 return array();
1169 }
1170
1177 public static function escapeArray( $array )
1178 {
1179 $db = eZDB::instance();
1180 $out = array();
1181 foreach( $array as $key => $value )
1182 {
1183 if ( is_array( $value ) )
1184 {
1185 $tmp = array();
1186 foreach( $value as $valueItem )
1187 {
1188 $tmp[] = $db->escapeString( $valueItem );
1189 }
1190 $out[$key] = $tmp;
1191 unset( $tmp );
1192 }
1193 else
1194 $out[$key] = $db->escapeString( $value );
1195 }
1196 return $out;
1197 }
1198
1209 public static function updateObjectList( $parameters )
1210 {
1211 $db = eZDB::instance();
1212 $def = $parameters['definition'];
1213 $table = $def['name'];
1214 $fields = $def['fields'];
1215 $keys = $def['keys'];
1216
1217 $updateFields = $parameters['update_fields'];
1218 $conditions = $parameters['conditions'];
1219
1220 $query = "UPDATE $table SET ";
1221 $i = 0;
1222 $valueBound = false;
1223
1224 foreach( $updateFields as $field => $value )
1225 {
1226 $fieldDef = $fields[ $field ];
1227 $numericDataTypes = array( 'integer', 'float', 'double' );
1228 if ( strlen( $value ) == 0 &&
1229 is_array( $fieldDef ) &&
1230 in_array( $fieldDef['datatype'], $numericDataTypes ) &&
1231 array_key_exists( 'default', $fieldDef ) &&
1232 $fieldDef[ 'default' ] !== null )
1233 {
1234 $value = $fieldDef[ 'default' ];
1235 }
1236
1237 $bindDataTypes = array( 'text' );
1238 if ( $db->bindingType() != eZDBInterface::BINDING_NO &&
1239 $db->countStringSize( $value ) > 2000 &&
1240 is_array( $fieldDef ) &&
1241 in_array( $fieldDef['datatype'], $bindDataTypes )
1242 )
1243 {
1244 $value = $db->bindVariable( $value, $fieldDef );
1245 $valueBound = true;
1246 }
1247 else
1248 $valueBound = false;
1249
1250 if ( $i > 0 )
1251 $query .= ', ';
1252 if ( $valueBound )
1253 $query .= $field . "=" . $value;
1254 else
1255 $query .= $field . "='" . $db->escapeString( $value ) . "'";
1256 ++$i;
1257 }
1258 $query .= ' WHERE ';
1259 $i = 0;
1260 foreach( $conditions as $conditionKey => $condition )
1261 {
1262 if ( $i > 0 )
1263 $query .= ' AND ';
1264 if ( is_array( $condition ) )
1265 {
1266 $query .= $conditionKey . ' IN (';
1267 $j = 0;
1268 foreach( $condition as $conditionValue )
1269 {
1270 if ( $j > 0 )
1271 $query .= ', ';
1272 $query .= "'" . $db->escapeString( $conditionValue ) . "'";
1273 ++$j;
1274 }
1275 $query .= ')';
1276 }
1277 else
1278 $query .= $conditionKey . "='" . $db->escapeString( $condition ) . "'";
1279 ++$i;
1280 }
1281 $db->query( $query );
1282 }
1283
1292 public function attributes()
1293 {
1294 $def = $this->definition();
1295 $attrs = array_keys( $def["fields"] );
1296 if ( isset( $def["function_attributes"] ) )
1297 $attrs = array_unique( array_merge( $attrs, array_keys( $def["function_attributes"] ) ) );
1298 if ( isset( $def["functions"] ) )
1299 $attrs = array_unique( array_merge( $attrs, array_keys( $def["functions"] ) ) );
1300 return $attrs;
1301 }
1302
1309 public function hasAttribute( $attr )
1310 {
1311 $def = $this->definition();
1312 $has_attr = isset( $def["fields"][$attr] );
1313 if ( !$has_attr and isset( $def["function_attributes"] ) )
1314 $has_attr = isset( $def["function_attributes"][$attr] );
1315 if ( !$has_attr and isset( $def["functions"] ) )
1316 $has_attr = isset( $def["functions"][$attr] );
1317 return $has_attr;
1318 }
1319
1329 public function attribute( $attr, $noFunction = false )
1330 {
1331 $def = $this->definition();
1332 $attrFunctions = isset( $def["function_attributes"] ) ? $def["function_attributes"] : null;
1333 if ( $noFunction === false && isset( $attrFunctions[$attr] ) )
1334 {
1335 $functionName = $attrFunctions[$attr];
1336 if ( method_exists( $this, $functionName ) )
1337 {
1338 return $this->$functionName();
1339 }
1340
1341 eZDebug::writeError( 'Could not find function : "' . get_class( $this ) . '::' . $functionName . '()".', __METHOD__ );
1342 return null;
1343 }
1344
1345 $fields = $def["fields"];
1346 if ( isset( $fields[$attr] ) )
1347 {
1348 $attrName = $fields[$attr];
1349 if ( isset( $attrName['name'] ) )
1350 {
1351 $attrName = $attrName['name'];
1352 }
1353 return $this->$attrName;
1354 }
1355
1356 if ( isset( $def["functions"][$attr] ) )
1357 {
1358 return $this->{$def["functions"][$attr]}();
1359 }
1360
1361 eZDebug::writeError( "Attribute '$attr' does not exist", $def['class_name'] . '::attribute' );
1362 return null;
1363 }
1364
1374 public function setAttribute( $attr, $val )
1375 {
1376 $def = $this->definition();
1377 $fields = $def["fields"];
1378 $functions = isset( $def["set_functions"] ) ? $def["set_functions"] : null;
1379 if ( isset( $fields[$attr] ) )
1380 {
1381 $attrName = $fields[$attr];
1382 if ( is_array( $attrName ) )
1383 {
1384 $attrName = $attrName['name'];
1385 }
1386
1387 $oldValue = null;
1388 if ( isset( $this->$attrName ) )
1389 $oldValue = $this->$attrName;
1390 $this->$attrName = $val;
1391 if ( $oldValue === null ||
1392 $oldValue !== $val )
1393 $this->setHasDirtyData( true );
1394 }
1395 else if ( isset( $functions[$attr] ) )
1396 {
1397 $functionName = $functions[$attr];
1398 $oldValue = $this->$functionName( $val );
1399 if ( $oldValue === null or $oldValue !== $val )
1400 $this->setHasDirtyData( true );
1401 }
1402 else
1403 {
1404 eZDebug::writeError( "Undefined attribute '$attr', cannot set",
1405 $def['class_name'] );
1406 }
1407 }
1408
1414 public function hasDirtyData()
1415 {
1416 return $this->PersistentDataDirty;
1417 }
1418
1425 public function setHasDirtyData( $hasDirtyData )
1426 {
1427 $this->PersistentDataDirty = $hasDirtyData;
1428 }
1429
1438 public static function getShortAttributeName( $db, $def, $attrName )
1439 {
1440 $fields = $def['fields'];
1441
1442 if ( $db->useShortNames() && isset( $fields[$attrName] ) && array_key_exists( 'short_name', $fields[$attrName] ) && $fields[$attrName]['short_name'] )
1443 return $fields[$attrName]['short_name'];
1444
1445 return $attrName;
1446 }
1447
1448 public $ID;
1449 public $ContentObjectID;
1450 public $SectionID;
1451 public $OwnerID;
1452 public $ClassID;
1453 public $Published;
1454 public $Modified;
1455 public $CurrentVersion;
1456 public $Status;
1457 public $RemoteID;
1458 public $LanguageMask;
1459 public $InitialLanguageID;
1460 public $NodeID;
1461 public $ParentNodeID;
1462 public $MainNodeID;
1463 public $ContentObjectVersion;
1464 public $ContentObjectIsPublished;
1465 public $Depth;
1466 public $SortField;
1467 public $SortOrder;
1468 public $Priority;
1469 public $ModifiedSubNode;
1470 public $PathString;
1471 public $PathIdentificationString;
1472 public $IsHidden;
1473 public $IsInvisible;
1474 public $UserID;
1475 public $Name;
1476 public $Locale;
1477 public $Identifier;
1478 public $NavigationPartIdentifier;
1479 public $SerializedDescriptionList;
1480 public $URLAliasName;
1481 public $AlwaysAvailable;
1482 public $CanTranslate;
1483 public $DataInt1;
1484 public $DataInt2;
1485 public $DataInt3;
1486 public $DataInt4;
1487 public $DataFloat1;
1488 public $DataFloat2;
1489 public $DataFloat3;
1490 public $DataFloat4;
1491 public $DataText1;
1492 public $DataText2;
1493 public $DataText3;
1494 public $DataText4;
1495 public $DataText5;
1496 public $SerializedDataText;
1497 public $Category;
1498 public $PolicyID;
1499 public $Values;
1500 public $Created;
1501 public $CurrencyCode;
1502 public $ProductCollectionID;
1503 public $ItemCount;
1504 public $Price;
1505 public $IsVATIncluded;
1506 public $VATValue;
1507 public $DiscountValue;
1508 public $Email;
1509 public $IsTemporary;
1510 public $IsArchived;
1511 public $IsActive;
1512 public $OrderID;
1513 public $ModifierID;
1514 public $Code;
1515 public $Symbol;
1516 public $AutoRateValue;
1517 public $CustomRateValue;
1518 public $RateFactor;
1519 public $Main;
1520 public $MementoKey;
1521 public $MainKey;
1522 public $WorkflowEventPos;
1523 public $LanguageID;
1524 public $ContentObjectStateGroupID;
1525 public $contentobject_attribute_id;
1526 public $contentobject_id;
1527 public $rating;
1528 public $rating_count;
1529 public $rating_average;
1530 }
1531
1532 ?>
eZDBInterface
Definition: ezdbinterface.php:20
eZDBInterface\BINDING_NO
const BINDING_NO
Definition: ezdbinterface.php:21
eZDBInterface\useShortNames
useShortNames()
Definition: ezdbinterface.php:488
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZPersistentObject
Definition: ezpersistentobject.php:33
eZPersistentObject\$Email
$Email
Definition: ezpersistentobject.php:1508
eZPersistentObject\$NodeID
$NodeID
Definition: ezpersistentobject.php:1460
eZPersistentObject\$PersistentDataDirty
$PersistentDataDirty
Definition: ezpersistentobject.php:41
eZPersistentObject\conditionTextByRow
static conditionTextByRow( $conditions, $row)
Definition: ezpersistentobject.php:643
eZPersistentObject\$DataInt2
$DataInt2
Definition: ezpersistentobject.php:1484
eZPersistentObject\newObjectOrder
static newObjectOrder( $def, $orderField, $conditions)
Definition: ezpersistentobject.php:1034
eZPersistentObject\updateObjectList
static updateObjectList( $parameters)
Definition: ezpersistentobject.php:1209
eZPersistentObject\$MainNodeID
$MainNodeID
Definition: ezpersistentobject.php:1462
eZPersistentObject\$LanguageMask
$LanguageMask
Definition: ezpersistentobject.php:1458
eZPersistentObject\$Values
$Values
Definition: ezpersistentobject.php:1499
eZPersistentObject\$ModifierID
$ModifierID
Definition: ezpersistentobject.php:1513
eZPersistentObject\$DataText4
$DataText4
Definition: ezpersistentobject.php:1494
eZPersistentObject\$DiscountValue
$DiscountValue
Definition: ezpersistentobject.php:1507
eZPersistentObject\$PathIdentificationString
$PathIdentificationString
Definition: ezpersistentobject.php:1471
eZPersistentObject\$LanguageID
$LanguageID
Definition: ezpersistentobject.php:1523
eZPersistentObject\$IsInvisible
$IsInvisible
Definition: ezpersistentobject.php:1473
eZPersistentObject\$DataFloat1
$DataFloat1
Definition: ezpersistentobject.php:1487
eZPersistentObject\hasAttribute
hasAttribute( $attr)
Definition: ezpersistentobject.php:1309
eZPersistentObject\fetchObject
static fetchObject( $def, $field_filters, $conds, $asObject=true, $grouping=null, $custom_fields=null)
Definition: ezpersistentobject.php:213
eZPersistentObject\$DataText2
$DataText2
Definition: ezpersistentobject.php:1492
eZPersistentObject\$CurrencyCode
$CurrencyCode
Definition: ezpersistentobject.php:1501
eZPersistentObject\$Symbol
$Symbol
Definition: ezpersistentobject.php:1515
eZPersistentObject\$ParentNodeID
$ParentNodeID
Definition: ezpersistentobject.php:1461
eZPersistentObject\$Depth
$Depth
Definition: ezpersistentobject.php:1465
eZPersistentObject\swapRow
static swapRow( $table, $keys, $order_id, $rows, $id1, $id2)
Definition: ezpersistentobject.php:1007
eZPersistentObject\$IsTemporary
$IsTemporary
Definition: ezpersistentobject.php:1509
eZPersistentObject\$AlwaysAvailable
$AlwaysAvailable
Definition: ezpersistentobject.php:1481
eZPersistentObject\__construct
__construct( $row=null)
Definition: ezpersistentobject.php:52
eZPersistentObject\$VATValue
$VATValue
Definition: ezpersistentobject.php:1506
eZPersistentObject\$Status
$Status
Definition: ezpersistentobject.php:1456
eZPersistentObject\$DataFloat2
$DataFloat2
Definition: ezpersistentobject.php:1488
eZPersistentObject\$IsArchived
$IsArchived
Definition: ezpersistentobject.php:1510
eZPersistentObject\$DataText3
$DataText3
Definition: ezpersistentobject.php:1493
eZPersistentObject\$MainKey
$MainKey
Definition: ezpersistentobject.php:1521
eZPersistentObject\$DataInt4
$DataInt4
Definition: ezpersistentobject.php:1486
eZPersistentObject\definition
static definition()
Definition: ezpersistentobject.php:1166
eZPersistentObject\$rating
$rating
Definition: ezpersistentobject.php:1527
eZPersistentObject\$ID
$ID
Definition: ezpersistentobject.php:1448
eZPersistentObject\store
store( $fieldFilters=null)
Definition: ezpersistentobject.php:304
eZPersistentObject\$OwnerID
$OwnerID
Definition: ezpersistentobject.php:1451
eZPersistentObject\escapeArray
static escapeArray( $array)
Definition: ezpersistentobject.php:1177
eZPersistentObject\$SortField
$SortField
Definition: ezpersistentobject.php:1466
eZPersistentObject\sync
sync( $fieldFilters=null)
Definition: ezpersistentobject.php:319
eZPersistentObject\$Priority
$Priority
Definition: ezpersistentobject.php:1468
eZPersistentObject\setHasDirtyData
setHasDirtyData( $hasDirtyData)
Definition: ezpersistentobject.php:1425
eZPersistentObject\$DataFloat4
$DataFloat4
Definition: ezpersistentobject.php:1490
eZPersistentObject\getShortAttributeName
static getShortAttributeName( $db, $def, $attrName)
Definition: ezpersistentobject.php:1438
eZPersistentObject\$InitialLanguageID
$InitialLanguageID
Definition: ezpersistentobject.php:1459
eZPersistentObject\$Published
$Published
Definition: ezpersistentobject.php:1453
eZPersistentObject\$PathString
$PathString
Definition: ezpersistentobject.php:1470
eZPersistentObject\reorderObject
static reorderObject( $def, $orderField, $conditions, $down=true)
Definition: ezpersistentobject.php:1069
eZPersistentObject\$AutoRateValue
$AutoRateValue
Definition: ezpersistentobject.php:1516
eZPersistentObject\$Identifier
$Identifier
Definition: ezpersistentobject.php:1477
eZPersistentObject\removeObject
static removeObject( $def, $conditions=null, $extraConditions=null)
Definition: ezpersistentobject.php:271
eZPersistentObject\$PolicyID
$PolicyID
Definition: ezpersistentobject.php:1498
eZPersistentObject\$UserID
$UserID
Definition: ezpersistentobject.php:1474
eZPersistentObject\conditionText
static conditionText( $conditions)
Definition: ezpersistentobject.php:628
eZPersistentObject\$CanTranslate
$CanTranslate
Definition: ezpersistentobject.php:1482
eZPersistentObject\$Locale
$Locale
Definition: ezpersistentobject.php:1476
eZPersistentObject\$DataText1
$DataText1
Definition: ezpersistentobject.php:1491
eZPersistentObject\$NavigationPartIdentifier
$NavigationPartIdentifier
Definition: ezpersistentobject.php:1478
eZPersistentObject\$ContentObjectStateGroupID
$ContentObjectStateGroupID
Definition: ezpersistentobject.php:1524
eZPersistentObject\$Code
$Code
Definition: ezpersistentobject.php:1514
eZPersistentObject\storeObject
static storeObject( $obj, $fieldFilters=null)
Definition: ezpersistentobject.php:338
eZPersistentObject\$IsVATIncluded
$IsVATIncluded
Definition: ezpersistentobject.php:1505
eZPersistentObject\$WorkflowEventPos
$WorkflowEventPos
Definition: ezpersistentobject.php:1522
eZPersistentObject\$rating_count
$rating_count
Definition: ezpersistentobject.php:1528
eZPersistentObject\$MementoKey
$MementoKey
Definition: ezpersistentobject.php:1520
eZPersistentObject\$ClassID
$ClassID
Definition: ezpersistentobject.php:1452
eZPersistentObject\$IsHidden
$IsHidden
Definition: ezpersistentobject.php:1472
eZPersistentObject\setAttribute
setAttribute( $attr, $val)
Definition: ezpersistentobject.php:1374
eZPersistentObject\$ItemCount
$ItemCount
Definition: ezpersistentobject.php:1503
eZPersistentObject\$DataInt1
$DataInt1
Definition: ezpersistentobject.php:1483
eZPersistentObject\fill
fill( $row)
Definition: ezpersistentobject.php:80
eZPersistentObject\$Main
$Main
Definition: ezpersistentobject.php:1519
eZPersistentObject\$OrderID
$OrderID
Definition: ezpersistentobject.php:1512
eZPersistentObject\handleRows
static handleRows( $rows, $class_name, $asObject)
Definition: ezpersistentobject.php:963
eZPersistentObject\$Created
$Created
Definition: ezpersistentobject.php:1500
eZPersistentObject\$DataInt3
$DataInt3
Definition: ezpersistentobject.php:1485
eZPersistentObject\$RemoteID
$RemoteID
Definition: ezpersistentobject.php:1457
eZPersistentObject\$ContentObjectVersion
$ContentObjectVersion
Definition: ezpersistentobject.php:1463
eZPersistentObject\$SortOrder
$SortOrder
Definition: ezpersistentobject.php:1467
eZPersistentObject\$Modified
$Modified
Definition: ezpersistentobject.php:1454
eZPersistentObject\$CurrentVersion
$CurrentVersion
Definition: ezpersistentobject.php:1455
eZPersistentObject\attributes
attributes()
Definition: ezpersistentobject.php:1292
eZPersistentObject\$contentobject_attribute_id
$contentobject_attribute_id
Definition: ezpersistentobject.php:1525
eZPersistentObject\$DataText5
$DataText5
Definition: ezpersistentobject.php:1495
eZPersistentObject\$CustomRateValue
$CustomRateValue
Definition: ezpersistentobject.php:1517
eZPersistentObject\$DataFloat3
$DataFloat3
Definition: ezpersistentobject.php:1489
eZPersistentObject\$RateFactor
$RateFactor
Definition: ezpersistentobject.php:1518
eZPersistentObject\$rating_average
$rating_average
Definition: ezpersistentobject.php:1529
eZPersistentObject\hasDirtyData
hasDirtyData()
Definition: ezpersistentobject.php:1414
eZPersistentObject\count
static count( $def, $conds=null, $field=null)
Definition: ezpersistentobject.php:187
eZPersistentObject\$ProductCollectionID
$ProductCollectionID
Definition: ezpersistentobject.php:1502
eZPersistentObject\$ContentObjectIsPublished
$ContentObjectIsPublished
Definition: ezpersistentobject.php:1464
eZPersistentObject\$Price
$Price
Definition: ezpersistentobject.php:1504
eZPersistentObject\attribute
attribute( $attr, $noFunction=false)
Definition: ezpersistentobject.php:1329
eZPersistentObject\$ModifiedSubNode
$ModifiedSubNode
Definition: ezpersistentobject.php:1469
eZPersistentObject\$SectionID
$SectionID
Definition: ezpersistentobject.php:1450
eZPersistentObject\replaceFieldsWithShortNames
static replaceFieldsWithShortNames(eZDBInterface $db, array $fieldDefs, &$fields)
Definition: ezpersistentobject.php:110
eZPersistentObject\$SerializedDataText
$SerializedDataText
Definition: ezpersistentobject.php:1496
eZPersistentObject\$URLAliasName
$URLAliasName
Definition: ezpersistentobject.php:1480
eZPersistentObject\fetchObjectList
static fetchObjectList( $def, $field_filters=null, $conds=null, $sorts=null, $limit=null, $asObject=true, $grouping=false, $custom_fields=null, $custom_tables=null, $custom_conds=null)
Definition: ezpersistentobject.php:806
eZPersistentObject\$contentobject_id
$contentobject_id
Definition: ezpersistentobject.php:1526
eZPersistentObject\$SerializedDescriptionList
$SerializedDescriptionList
Definition: ezpersistentobject.php:1479
eZPersistentObject\$Category
$Category
Definition: ezpersistentobject.php:1497
eZPersistentObject\$IsActive
$IsActive
Definition: ezpersistentobject.php:1511
eZPersistentObject\$ContentObjectID
$ContentObjectID
Definition: ezpersistentobject.php:1449
eZPersistentObject\replaceFieldsWithLongNames
static replaceFieldsWithLongNames(eZDBInterface $db, array $fieldDefs, &$fields)
Definition: ezpersistentobject.php:154
eZPersistentObject\eZPersistentObject
eZPersistentObject( $row=null)
Definition: ezpersistentobject.php:64
eZPersistentObject\$Name
$Name
Definition: ezpersistentobject.php:1475
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.