[~BUGFIX] Extbase (Persistence): Fixed some broken MM settings for some IRRE setups...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Mapper / DataMap.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * A data map to map a single table configured in $TCA on a domain object.
27 *
28 * @package Extbase
29 * @subpackage Persistence\Mapper
30 * @version $ID:$
31 */
32 class Tx_Extbase_Persistence_Mapper_DataMap {
33
34 /**
35 * The domain class name
36 *
37 * @var string
38 **/
39 protected $className;
40
41 /**
42 * The table name corresponding to the domain class configured in $TCA
43 *
44 * @var string
45 **/
46 protected $tableName;
47
48 /**
49 * An array of column maps configured in $TCA
50 *
51 * @var array
52 **/
53 protected $columnMaps;
54
55 /**
56 * Constructs this DataMap
57 *
58 * @param string $className The class name. This determines the table to fetch the configuration for
59 */
60 // TODO Refactor to factory pattern (DataMapFactory) and value object (DataMap)
61 public function __construct($className, $tableName = '', array $mapping = array()) {
62 $this->setClassName($className);
63 if (empty($tableName)) {
64 $this->setTableName(strtolower($className));
65 } else {
66 $this->setTableName($tableName);
67 }
68 $this->initialize($mapping);
69 }
70
71 /**
72 * Sets the name of the class the colum map represents
73 *
74 * @return void
75 */
76 public function setClassName($className) {
77 $this->className = $className;
78 }
79
80 /**
81 * Returns the name of the class the column map represents
82 *
83 * @return string The class name
84 */
85 public function getClassName() {
86 return $this->className;
87 }
88
89 /**
90 * Sets the name of the table the colum map represents
91 *
92 * @return void
93 */
94 public function setTableName($tableName) {
95 $this->tableName = $tableName;
96 }
97
98 /**
99 * Returns the name of the table the column map represents
100 *
101 * @return string The table name
102 */
103 public function getTableName() {
104 return $this->tableName;
105 }
106
107 /**
108 * Initializes the data map by adding column maps for all the configured columns in the $TCA.
109 * It also resolves the type of values the column is holding and the typo of relation the column
110 * represents.
111 *
112 * @return void
113 */
114 protected function initialize(array $mapping) {
115 $this->addCommonColumns();
116 $columnConfigurations = array();
117 foreach ($this->getColumnsDefinition() as $columnName => $columnDefinition) {
118 $columnConfigurations[$columnName] = $columnDefinition['config'];
119 $columnConfigurations[$columnName]['mapOnProperty'] = Tx_Extbase_Utility_Extension::convertUnderscoredToLowerCamelCase($columnName);
120 }
121 $columnConfigurations = t3lib_div::array_merge_recursive_overrule($columnConfigurations, $mapping);
122 foreach ($columnConfigurations as $columnName => $columnConfiguration) {
123 $columnMap = new Tx_Extbase_Persistence_Mapper_ColumnMap($columnName, $columnConfiguration['mapOnProperty']);
124 $this->setPropertyType($columnMap, $columnConfiguration);
125 $this->setRelations($columnMap, $columnConfiguration);
126 $this->addColumnMap($columnMap);
127 }
128 }
129
130 /**
131 * Returns the TCA columns array of the specified table
132 *
133 * @param string $tableName An optional table name to fetch the columns definition from
134 * @return array The TCA columns definition
135 */
136 public function getColumnsDefinition($tableName = '') {
137 $tableName = strlen($tableName) > 0 ? $tableName : $this->getTableName();
138 if (TYPO3_MODE === 'FE') {
139 $GLOBALS['TSFE']->includeTCA();
140 }
141 t3lib_div::loadTCA($tableName);
142 $columns = is_array($GLOBALS['TCA'][$tableName]['columns']) ? $GLOBALS['TCA'][$tableName]['columns'] : array();
143 return $columns;
144 }
145
146 /**
147 * Adds available common columns (e.g. tstamp or crdate) to the data map. It takes the configured column names
148 * into account.
149 *
150 * @return void
151 */
152 protected function addCommonColumns() {
153 $this->addColumn('uid', NULL, Tx_Extbase_Persistence_PropertyType::LONG);
154 if ($this->hasPidColumn()) {
155 $this->addColumn('pid', NULL, Tx_Extbase_Persistence_PropertyType::LONG);
156 }
157 if ($this->hasTimestampColumn()) {
158 $this->addColumn($this->getTimestampColumnName(), NULL, Tx_Extbase_Persistence_PropertyType::DATE);
159 }
160 if ($this->hasCreationDateColumn()) {
161 $this->addColumn($this->getCreationDateColumnName(), NULL, Tx_Extbase_Persistence_PropertyType::DATE);
162 }
163 if ($this->hasCreatorUidColumn()) {
164 $this->addColumn($this->getCreatorUidColumnName(), NULL, Tx_Extbase_Persistence_PropertyType::LONG);
165 }
166 if ($this->hasDeletedColumn()) {
167 $this->addColumn($this->getDeletedColumnName(), NULL, Tx_Extbase_Persistence_PropertyType::BOOLEAN);
168 }
169 if ($this->hasHiddenColumn()) {
170 $this->addColumn($this->getHiddenColumnName(), NULL, Tx_Extbase_Persistence_PropertyType::BOOLEAN);
171 }
172 }
173
174 /**
175 * This method tries to determine the type of value the column hold by inspectiong the $TCA column configuration
176 * and sets it.
177 *
178 * @param string $columnMap The column map
179 * @param string $columnConfiguration The column configuration from $TCA
180 * @return void
181 */
182 protected function setPropertyType(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
183 $evalConfiguration = t3lib_div::trimExplode(',', $columnConfiguration['eval']);
184 if (in_array('date', $evalConfiguration) || in_array('datetime', $evalConfiguration)) {
185 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::DATE);
186 } elseif ($columnConfiguration['type'] === 'check' && empty($columnConfiguration['items'])) {
187 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::BOOLEAN);
188 } elseif (in_array('int', $evalConfiguration)) {
189 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::LONG);
190 } elseif (in_array('double2', $evalConfiguration)) {
191 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::DOUBLE);
192 } else {
193 if (isset($columnConfiguration['foreign_table'])) {
194 if (isset($columnConfiguration['loadingStrategy'])) {
195 $columnMap->setLoadingStrategy($columnConfiguration['loadingStrategy']);
196 } else {
197 $columnMap->setLoadingStrategy(Tx_Extbase_Persistence_Mapper_ColumnMap::STRATEGY_EAGER);
198 }
199 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::REFERENCE);
200 } else {
201 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::STRING);
202 }
203 }
204 }
205
206 /**
207 * This method tries to determine the type of type of relation to other tables and sets it based on
208 * the $TCA column configuration
209 *
210 * @param Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap The column map
211 * @param string $columnConfiguration The column configuration from $TCA
212 * @return void
213 */
214 protected function setRelations(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
215 if (isset($columnConfiguration) && $columnConfiguration['type'] !== 'passthrough') {
216 if (isset($columnConfiguration['foreign_table']) && !isset($columnConfiguration['MM']) && !(isset($columnConfiguration['foreign_label']) || isset($columnConfiguration['foreign_selector']))) {
217 if ($columnConfiguration['maxitems'] == 1) {
218 $this->setOneToOneRelation($columnMap, $columnConfiguration);
219 } else {
220 $this->setOneToManyRelation($columnMap, $columnConfiguration);
221 }
222 } elseif (isset($columnConfiguration['MM']) || (isset($columnConfiguration['foreign_label']) || isset($columnConfiguration['foreign_selector']))) {
223 $this->setManyToManyRelation($columnMap, $columnConfiguration);
224 } else {
225 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE);
226 }
227 }
228 }
229
230 /**
231 * This method sets the configuration for a 1:1 relation based on
232 * the $TCA column configuration
233 *
234 * @param string $columnMap The column map
235 * @param string $columnConfiguration The column configuration from $TCA
236 * @return void
237 */
238 protected function setOneToOneRelation(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
239 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_ONE);
240 $columnMap->setChildClassName($this->determineChildClassName($columnConfiguration));
241 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
242 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
243 $columnMap->setChildSortbyFieldName($columnConfiguration['foreign_sortby']);
244 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
245 $columnMap->setParentTableFieldName($columnConfiguration['foreign_table_field']);
246 }
247
248 /**
249 * This method sets the configuration for a 1:n relation based on
250 * the $TCA column configuration
251 *
252 * @param string $columnMap The column map
253 * @param string $columnConfiguration The column configuration from $TCA
254 * @return void
255 */
256 protected function setOneToManyRelation(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
257 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY);
258 $columnMap->setChildClassName($this->determineChildClassName($columnConfiguration));
259 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
260 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
261 $columnMap->setChildSortbyFieldName($columnConfiguration['foreign_sortby']);
262 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
263 $columnMap->setParentTableFieldName($columnConfiguration['foreign_table_field']);
264 }
265
266 /**
267 * This method sets the configuration for a m:n relation based on
268 * the $TCA column configuration
269 *
270 * @param string $columnMap The column map
271 * @param string $columnConfiguration The column configuration from $TCA
272 * @return void
273 */
274 protected function setManyToManyRelation(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
275 // TODO support multi table relationships
276 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY);
277 if ($columnConfiguration['type'] === 'inline') {
278 $columns = $this->getColumnsDefinition($columnConfiguration['foreign_table']);
279 $childKeyFieldName = $this->determineChildKeyFieldName($columnConfiguration);
280 $columnMap->setChildClassName($this->determineChildClassName($columns[$childKeyFieldName]['config']));
281 $columnMap->setChildTableName($columns[$childKeyFieldName]['config']['foreign_table']);
282 $columnMap->setRelationTableName($columnConfiguration['foreign_table']);
283 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
284 $columnMap->setChildKeyFieldName($childKeyFieldName);
285 $columnMap->setChildSortByFieldName($columnConfiguration['foreign_sortby']);
286 } else {
287 $columnMap->setChildClassName($this->determineChildClassName($columnConfiguration));
288 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
289 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
290 $columnMap->setRelationTableName($columnConfiguration['MM']);
291 if (is_array($columnConfiguration['MM_match_fields'])) {
292 $columnMap->setRelationTableMatchFields($columnConfiguration['MM_match_fields']);
293 }
294 if (is_array($columnConfiguration['MM_insert_fields'])) {
295 $columnMap->setRelationTableInsertFields($columnConfiguration['MM_insert_fields']);
296 }
297 $columnMap->setRelationTableWhereStatement($columnConfiguration['MM_table_where']);
298 if (!empty($columnConfiguration['MM_opposite_field'])) {
299 $columnMap->setParentKeyFieldName('uid_foreign');
300 $columnMap->setChildKeyFieldName('uid_local');
301 $columnMap->setChildSortByFieldName('sorting_foreign');
302 } else {
303 $columnMap->setParentKeyFieldName('uid_local');
304 $columnMap->setChildKeyFieldName('uid_foreign');
305 $columnMap->setChildSortByFieldName('sorting');
306 }
307 }
308 }
309
310 /**
311 * This method returns the foreign key field name in the relation table. For IRRE setups it will return
312 * the foreign_label; if not present the foreign_selector. Default is uid_foreign.
313 *
314 * @param string $columnConfiguration The column configuration of the parent table relation field
315 * @return string The foreign key field name of the relation table
316 */
317 public function determineChildKeyFieldName($columnConfiguration) {
318 if (isset($columnConfiguration['foreign_label'])) {
319 $childKeyFieldName = $columnConfiguration['foreign_label'];
320 } elseif (isset($columnConfiguration['foreign_selector'])) {
321 $childKeyFieldName = $columnConfiguration['foreign_selector'];
322 } else {
323 $childKeyFieldName = 'uid_foreign';
324 }
325 return $childKeyFieldName;
326 }
327
328 /**
329 * This function determines the child class name. It can either be defined as foreign_class in the column configuration (TCA)
330 * or it must be defined in the extbase framework configuration (reverse mapping from tableName to className).
331 *
332 * @param $columnConfiguration The column configuration (from TCA)
333 * @return string The class name of the related child object
334 */
335 protected function determineChildClassName($columnConfiguration) {
336 $foreignClassName = '';
337 if (is_string($columnConfiguration['foreign_class']) && (strlen($columnConfiguration['foreign_class']) > 0)) {
338 $foreignClassName = $columnConfiguration['foreign_class'];
339 }
340 if (empty($foreignClassName)){
341 $extbaseSettings = Tx_Extbase_Dispatcher::getExtbaseFrameworkConfiguration();
342 // TODO Apply a cache to increase performance (profile first)
343 if (is_array($extbaseSettings['persistence']['classes'])) {
344 foreach ($extbaseSettings['persistence']['classes'] as $className => $classConfiguration) {
345 if ($classConfiguration['mapping']['tableName'] === $columnConfiguration['foreign_table']) {
346 $foreignClassName = $className;
347 break;
348 }
349 }
350 }
351 }
352 // TODO Throw exception if no appropriate class name was found
353 return $foreignClassName;
354 }
355
356 /**
357 * Sets the column maps.
358 *
359 * @param array $columnMaps The column maps stored in a flat array.
360 * @return void
361 */
362 public function setColumnMaps(array $columnMaps) {
363 $this->columnMaps = $columnMaps;
364 }
365
366 /**
367 * Adds a given column map to the data map.
368 *
369 * @param Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap The column map
370 * @return void
371 */
372 public function addColumnMap(Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap) {
373 $this->columnMaps[$columnMap->getPropertyName()] = $columnMap;
374 }
375
376 /**
377 * Builds a column map out of the given column name, type of value (optional), and type of
378 * relation (optional) and adds it to the data map.
379 *
380 * @param string $columnName The column name
381 * @param string $propertyName The property name
382 * @param string $propertyType The type of value (default: string)
383 * @param string $typeOfRelation The type of relation (default: none)
384 * @return Tx_Extbase_Persistence_Mapper_DataMap Returns itself for a fluent interface
385 */
386 public function addColumn($columnName, $propertyName = '', $propertyType = Tx_Extbase_Persistence_PropertyType::STRING, $typeOfRelation = Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE) {
387 if (empty($propertyName)) {
388 $propertyName = Tx_Extbase_Utility_Extension::convertUnderscoredToLowerCamelCase($columnName);
389 }
390
391 $columnMap = new Tx_Extbase_Persistence_Mapper_ColumnMap($columnName, $propertyName);
392 $columnMap->setPropertyType($propertyType);
393 $columnMap->setTypeOfRelation($typeOfRelation);
394 $this->addColumnMap($columnMap);
395 return $this;
396 }
397
398 /**
399 * Returns all column maps
400 *
401 * @return array The column maps
402 */
403 public function getColumnMaps() {
404 return $this->columnMaps;
405 }
406
407 /**
408 * Returns the column map corresponding to the given property name.
409 *
410 * @param string $propertyName
411 * @return Tx_Extbase_Persistence_Mapper_ColumnMap|NULL The column map or NULL if no corresponding column map was found.
412 */
413 public function getColumnMap($propertyName) {
414 return $this->columnMaps[$propertyName];
415 }
416
417 /**
418 * Returns TRUE if the property is persistable (configured in $TCA)
419 *
420 * @param string $propertyName The property name
421 * @return boolean TRUE if the property is persistable (configured in $TCA)
422 */
423 public function isPersistableProperty($propertyName) {
424 return isset($this->columnMaps[$propertyName]);
425 }
426
427 /**
428 * Check if versioning is enabled .
429 *
430 * @return boolean
431 */
432 public function isVersionable() {
433 return ($GLOBALS['TCA'] [$this->tableName] ['ctrl'] ['versioningWS'] === '1');
434 }
435
436 /**
437 * Returns TRUE if the table has a pid column holding the id of the page the record is virtually stored on.
438 * Currently we don't support tables without a pid column.
439 *
440 * @return boolean The result
441 */
442 public function hasPidColumn() {
443 // TODO Should we implement a check for having a pid column?
444 return TRUE;
445 }
446
447 /**
448 * Returns the name of a column holding the timestamp the record was modified
449 *
450 * @return string The field name
451 */
452 public function getTimestampColumnName() {
453 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['tstamp'];
454 }
455
456 /**
457 * Returns TRUE if the table has a column holding the timestamp the record was modified
458 *
459 * @return boolean The result
460 */
461 public function hasTimestampColumn() {
462 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['tstamp']);
463 }
464
465 /**
466 * Returns the name of a column holding the creation date timestamp
467 *
468 * @return string The field name
469 */
470 public function getCreationDateColumnName() {
471 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['crdate'];
472 }
473
474 /**
475 * Returns TRUE if the table has olumn holding the creation date timestamp
476 *
477 * @return boolean The result
478 */
479 public function hasCreationDateColumn() {
480 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['crdate']);
481 }
482
483 /**
484 * Returns the name of a column holding the uid of the back-end user who created this record
485 *
486 * @return string The field name
487 */
488 public function getCreatorUidColumnName() {
489 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['cruser_id'];
490 }
491
492 /**
493 * Returns TRUE if the table has a column holding the uid of the back-end user who created this record
494 *
495 * @return boolean The result
496 */
497 public function hasCreatorUidColumn() {
498 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['cruser_id']);
499 }
500
501 /**
502 * Returns the name of a column indicating the 'deleted' state of the row
503 *
504 * @return string The field name
505 */
506 public function getDeletedColumnName() {
507 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['delete'];
508 }
509
510 /**
511 * Returns TRUE if the table has a column indicating the 'deleted' state of the row
512 *
513 * @return boolean The result
514 */
515 public function hasDeletedColumn() {
516 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['delete']);
517 }
518
519 /**
520 * Returns the name of a column indicating the 'hidden' state of the row
521 *
522 * @return string The field name
523 */
524 public function getHiddenColumnName() {
525 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['disabled'];
526 }
527
528 /**
529 * Returns TRUE if the table has a column indicating the 'hidden' state of the row
530 *
531 * @return boolean The result
532 */
533 public function hasHiddenColumn() {
534 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['disabled']);
535 }
536
537 /**
538 * Returns the name of a column holding the timestamp the record should not displayed before
539 *
540 * @return string The field name
541 */
542 public function getStartTimeColumnName() {
543 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['starttime'];
544 }
545
546 /**
547 * Returns TRUE if the table has a column holding the timestamp the record should not displayed before
548 *
549 * @return boolean The result
550 */
551 public function hasStartTimeColumn() {
552 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['starttime']);
553 }
554
555 /**
556 * Returns the name of a column holding the timestamp the record should not displayed afterwards
557 *
558 * @return string The field name
559 */
560 public function getEndTimeColumnName() {
561 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['endtime'];
562 }
563
564 /**
565 * Returns TRUE if the table has a column holding the timestamp the record should not displayed afterwards
566 *
567 * @return boolean The result
568 */
569 public function hasEndTimeColumn() {
570 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['endtime']);
571 }
572
573 /**
574 * Returns the name of a column holding the uid of the front-end user group which is allowed to edit this record
575 *
576 * @return string The field name
577 */
578 public function getFrontEndUserGroupColumnName() {
579 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['fe_group'];
580 }
581
582 /**
583 * Returns TRUE if the table has a column holding the uid of the front-end user group which is allowed to edit this record
584 *
585 * @return boolean The result
586 */
587 public function hasFrontEndUserGroupColumn() {
588 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['fe_group']);
589 }
590
591 /**
592 * Converts a field name to the property name. It respects property name aliases defined in $TCA.
593 *
594 * @param string $fieldName The field name
595 * @return string $propertyName The property name
596 */
597 public function convertFieldNameToPropertyName($fieldName) {
598 $propertyName = $fieldName;
599 return $propertyName; // TODO Implement aliases for field names (see also convertPropertyNameToFieldName())
600 }
601
602 /**
603 * Converts a preoperty name to the field name. It respects property name aliases defined in $TCA.
604 *
605 * @param string $fieldName The field name
606 * @return string $propertyName The property name
607 */
608 public function convertPropertyNameToFieldName($propertyName) {
609 $fieldName = $propertyName;
610 return $fieldName;
611 }
612
613 /**
614 * Converts the given string into the given type
615 *
616 * @param integer $type one of the constants defined in Tx_Extbase_Persistence_PropertyType
617 * @param string $string a string representing a value of the given type
618 *
619 * @return string|int|float|DateTime|boolean
620 */
621 public function convertFieldValueToPropertyValue($type, $string) {
622 switch ($type) {
623 case Tx_Extbase_Persistence_PropertyType::LONG:
624 return (int) $string;
625 case Tx_Extbase_Persistence_PropertyType::DOUBLE:
626 case Tx_Extbase_Persistence_PropertyType::DECIMAL:
627 return (float) $string;
628 case Tx_Extbase_Persistence_PropertyType::DATE:
629 return new DateTime(date('r', $string));
630 case Tx_Extbase_Persistence_PropertyType::BOOLEAN:
631 return (boolean) $string;
632 default:
633 return $string;
634 }
635 }
636
637 /**
638 * Converts a value from a property type to a database field type
639 *
640 * @param mixed $propertyValue The property value
641 * @return mixed The converted value
642 */
643 public function convertPropertyValueToFieldValue($propertyValue) {
644 if (is_bool($propertyValue)) {
645 $convertedValue = $propertyValue ? 1 : 0;
646 } elseif ($propertyValue instanceof Tx_Extbase_DomainObject_AbstractDomainObject) {
647 $convertedValue = $propertyValue->getUid();
648 } elseif (is_a($propertyValue, 'DateTime')) {
649 $convertedValue = $propertyValue->format('U');
650 } elseif (is_int($propertyValue)) {
651 $convertedValue = $propertyValue;
652 } else {
653 $convertedValue = $propertyValue;
654 }
655 return $convertedValue;
656 }
657
658 }