[BUGFIX] Exception editing inline mm with deleted child child
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / AbstractDatabaseRecordProvider.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Database\DatabaseConnection;
20
21 /**
22 * Extended by other provider that fetch records from database
23 */
24 abstract class AbstractDatabaseRecordProvider
25 {
26 /**
27 * Fetch a record from database. Deleted records will NOT be fetched.
28 * Method is similar to BackendUtility::getRecord, but is more picky
29 * about input and result.
30 *
31 * @param string $tableName The table name to fetch record from
32 * @param int $uid Uid of record to fetch
33 * @return array Fetched record row
34 * @throws DatabaseRecordException|\InvalidArgumentException|\UnexpectedValueException|\RuntimeException
35 */
36 protected function getRecordFromDatabase($tableName, $uid)
37 {
38 if ($uid <= 0) {
39 throw new \InvalidArgumentException(
40 '$uid must be positive integer, ' . $uid . ' given',
41 1437656456
42 );
43 }
44 $database = $this->getDatabase();
45 $tableName = $database->quoteStr($tableName, $tableName);
46 $where = 'uid=' . (int)$uid . BackendUtility::deleteClause($tableName);
47 $row = $database->exec_SELECTgetSingleRow('*', $tableName, $where);
48 if ($row === null) {
49 // Indicates a program / usage error
50 throw new \RuntimeException(
51 'Database error fetching record from tablename ' . $tableName . ' with uid ' . $uid,
52 1437655862
53 );
54 }
55 if ($row === false) {
56 // Indicates a runtime error (eg. record was killed by other editor meanwhile) can be caught elsewhere
57 // and transformed to a message to the user or something
58 throw new DatabaseRecordException(
59 'Record with uid ' . $uid . ' from table ' . $tableName . ' not found',
60 1437656081,
61 null,
62 $tableName,
63 (int)$uid
64 );
65 }
66 if (!is_array($row)) {
67 // Database connection behaves weird ...
68 throw new \UnexpectedValueException(
69 'Database exec_SELECTgetSingleRow() did not return error type or result',
70 1437656323
71 );
72 }
73 return $row;
74 }
75
76 /**
77 * @return DatabaseConnection
78 */
79 protected function getDatabase()
80 {
81 return $GLOBALS['TYPO3_DB'];
82 }
83 }