Completed
Push — 4.3 ( 28cdbc )
by David
21:20 queued 01:32
created
src/Mouf/Database/TDBM/Utils/ScalarBeanPropertyDescriptor.php 1 patch
Indentation   +167 added lines, -167 removed lines patch added patch discarded remove patch
@@ -11,125 +11,125 @@  discard block
 block discarded – undo
11 11
  */
12 12
 class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor
13 13
 {
14
-    /**
15
-     * @var Column
16
-     */
17
-    private $column;
18
-
19
-    public function __construct(Table $table, Column $column)
20
-    {
21
-        parent::__construct($table);
22
-        $this->table = $table;
23
-        $this->column = $column;
24
-    }
25
-
26
-    /**
27
-     * Returns the foreign-key the column is part of, if any. null otherwise.
28
-     *
29
-     * @return ForeignKeyConstraint|null
30
-     */
31
-    public function getForeignKey()
32
-    {
33
-        return false;
34
-    }
35
-
36
-    /**
37
-     * Returns the param annotation for this property (useful for constructor).
38
-     *
39
-     * @return string
40
-     */
41
-    public function getParamAnnotation()
42
-    {
43
-        $className = $this->getClassName();
44
-        $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType());
45
-
46
-        $str = '     * @param %s %s';
47
-
48
-        return sprintf($str, $paramType, $this->getVariableName());
49
-    }
50
-
51
-    public function getUpperCamelCaseName()
52
-    {
53
-        return TDBMDaoGenerator::toCamelCase($this->column->getName());
54
-    }
55
-
56
-    /**
57
-     * Returns the name of the class linked to this property or null if this is not a foreign key.
58
-     *
59
-     * @return null|string
60
-     */
61
-    public function getClassName()
62
-    {
63
-        return;
64
-    }
65
-
66
-    /**
67
-     * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
68
-     *
69
-     * @return bool
70
-     */
71
-    public function isCompulsory()
72
-    {
73
-        return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null;
74
-    }
75
-
76
-    /**
77
-     * Returns true if the property has a default value.
78
-     *
79
-     * @return bool
80
-     */
81
-    public function hasDefault()
82
-    {
83
-        return $this->column->getDefault() !== null;
84
-    }
85
-
86
-    /**
87
-     * Returns the code that assigns a value to its default value.
88
-     *
89
-     * @return string
90
-     */
91
-    public function assignToDefaultCode()
92
-    {
93
-        $str = '        $this->%s(%s);';
94
-
95
-        $default = $this->column->getDefault();
96
-
97
-        if (strtoupper($default) === 'CURRENT_TIMESTAMP') {
98
-            $defaultCode = 'new \DateTimeImmutable()';
99
-        } else {
100
-            $defaultCode = var_export($this->column->getDefault(), true);
101
-        }
102
-
103
-        return sprintf($str, $this->getSetterName(), $defaultCode);
104
-    }
105
-
106
-    /**
107
-     * Returns true if the property is the primary key.
108
-     *
109
-     * @return bool
110
-     */
111
-    public function isPrimaryKey()
112
-    {
113
-        return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns());
114
-    }
115
-
116
-    /**
117
-     * Returns the PHP code for getters and setters.
118
-     *
119
-     * @return string
120
-     */
121
-    public function getGetterSetterCode()
122
-    {
123
-        $type = $this->column->getType();
124
-        $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type);
125
-
126
-        $columnGetterName = $this->getGetterName();
127
-        $columnSetterName = $this->getSetterName();
128
-
129
-        // A column type can be forced if it is not nullable and not auto-incrmentable (for auto-increment columns, we can get "null" as long as the bean is not saved).
130
-        $canForceGetterReturnType = $this->column->getNotnull() && !$this->column->getAutoincrement();
131
-
132
-        $getterAndSetterCode = '    /**
14
+	/**
15
+	 * @var Column
16
+	 */
17
+	private $column;
18
+
19
+	public function __construct(Table $table, Column $column)
20
+	{
21
+		parent::__construct($table);
22
+		$this->table = $table;
23
+		$this->column = $column;
24
+	}
25
+
26
+	/**
27
+	 * Returns the foreign-key the column is part of, if any. null otherwise.
28
+	 *
29
+	 * @return ForeignKeyConstraint|null
30
+	 */
31
+	public function getForeignKey()
32
+	{
33
+		return false;
34
+	}
35
+
36
+	/**
37
+	 * Returns the param annotation for this property (useful for constructor).
38
+	 *
39
+	 * @return string
40
+	 */
41
+	public function getParamAnnotation()
42
+	{
43
+		$className = $this->getClassName();
44
+		$paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType());
45
+
46
+		$str = '     * @param %s %s';
47
+
48
+		return sprintf($str, $paramType, $this->getVariableName());
49
+	}
50
+
51
+	public function getUpperCamelCaseName()
52
+	{
53
+		return TDBMDaoGenerator::toCamelCase($this->column->getName());
54
+	}
55
+
56
+	/**
57
+	 * Returns the name of the class linked to this property or null if this is not a foreign key.
58
+	 *
59
+	 * @return null|string
60
+	 */
61
+	public function getClassName()
62
+	{
63
+		return;
64
+	}
65
+
66
+	/**
67
+	 * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
68
+	 *
69
+	 * @return bool
70
+	 */
71
+	public function isCompulsory()
72
+	{
73
+		return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null;
74
+	}
75
+
76
+	/**
77
+	 * Returns true if the property has a default value.
78
+	 *
79
+	 * @return bool
80
+	 */
81
+	public function hasDefault()
82
+	{
83
+		return $this->column->getDefault() !== null;
84
+	}
85
+
86
+	/**
87
+	 * Returns the code that assigns a value to its default value.
88
+	 *
89
+	 * @return string
90
+	 */
91
+	public function assignToDefaultCode()
92
+	{
93
+		$str = '        $this->%s(%s);';
94
+
95
+		$default = $this->column->getDefault();
96
+
97
+		if (strtoupper($default) === 'CURRENT_TIMESTAMP') {
98
+			$defaultCode = 'new \DateTimeImmutable()';
99
+		} else {
100
+			$defaultCode = var_export($this->column->getDefault(), true);
101
+		}
102
+
103
+		return sprintf($str, $this->getSetterName(), $defaultCode);
104
+	}
105
+
106
+	/**
107
+	 * Returns true if the property is the primary key.
108
+	 *
109
+	 * @return bool
110
+	 */
111
+	public function isPrimaryKey()
112
+	{
113
+		return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns());
114
+	}
115
+
116
+	/**
117
+	 * Returns the PHP code for getters and setters.
118
+	 *
119
+	 * @return string
120
+	 */
121
+	public function getGetterSetterCode()
122
+	{
123
+		$type = $this->column->getType();
124
+		$normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type);
125
+
126
+		$columnGetterName = $this->getGetterName();
127
+		$columnSetterName = $this->getSetterName();
128
+
129
+		// A column type can be forced if it is not nullable and not auto-incrmentable (for auto-increment columns, we can get "null" as long as the bean is not saved).
130
+		$canForceGetterReturnType = $this->column->getNotnull() && !$this->column->getAutoincrement();
131
+
132
+		$getterAndSetterCode = '    /**
133 133
      * The getter for the "%s" column.
134 134
      *
135 135
      * @return %s
@@ -151,52 +151,52 @@  discard block
 block discarded – undo
151 151
 
152 152
 ';
153 153
 
154
-        return sprintf($getterAndSetterCode,
155
-            // Getter
156
-            $this->column->getName(),
157
-            $normalizedType.($canForceGetterReturnType ? '' : '|null'),
158
-            $columnGetterName,
159
-            ($canForceGetterReturnType ? ' : '.$normalizedType : ''),
160
-            var_export($this->column->getName(), true),
161
-            var_export($this->table->getName(), true),
162
-            // Setter
163
-            $this->column->getName(),
164
-            $normalizedType,
165
-            $this->column->getName(),
166
-            $columnSetterName,
167
-            $normalizedType,
168
-            //$castTo,
169
-            $this->column->getName().($this->column->getNotnull() ? '' : ' = null'),
170
-            var_export($this->column->getName(), true),
171
-            $this->column->getName(),
172
-            var_export($this->table->getName(), true)
173
-        );
174
-    }
175
-
176
-    /**
177
-     * Returns the part of code useful when doing json serialization.
178
-     *
179
-     * @return string
180
-     */
181
-    public function getJsonSerializeCode()
182
-    {
183
-        $type = $this->column->getType();
184
-        $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type);
185
-
186
-        if ($normalizedType == '\\DateTimeInterface') {
187
-            return '        $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n";
188
-        } else {
189
-            return '        $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n";
190
-        }
191
-    }
192
-
193
-    /**
194
-     * Returns the column name.
195
-     *
196
-     * @return string
197
-     */
198
-    public function getColumnName()
199
-    {
200
-        return $this->column->getName();
201
-    }
154
+		return sprintf($getterAndSetterCode,
155
+			// Getter
156
+			$this->column->getName(),
157
+			$normalizedType.($canForceGetterReturnType ? '' : '|null'),
158
+			$columnGetterName,
159
+			($canForceGetterReturnType ? ' : '.$normalizedType : ''),
160
+			var_export($this->column->getName(), true),
161
+			var_export($this->table->getName(), true),
162
+			// Setter
163
+			$this->column->getName(),
164
+			$normalizedType,
165
+			$this->column->getName(),
166
+			$columnSetterName,
167
+			$normalizedType,
168
+			//$castTo,
169
+			$this->column->getName().($this->column->getNotnull() ? '' : ' = null'),
170
+			var_export($this->column->getName(), true),
171
+			$this->column->getName(),
172
+			var_export($this->table->getName(), true)
173
+		);
174
+	}
175
+
176
+	/**
177
+	 * Returns the part of code useful when doing json serialization.
178
+	 *
179
+	 * @return string
180
+	 */
181
+	public function getJsonSerializeCode()
182
+	{
183
+		$type = $this->column->getType();
184
+		$normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type);
185
+
186
+		if ($normalizedType == '\\DateTimeInterface') {
187
+			return '        $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n";
188
+		} else {
189
+			return '        $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n";
190
+		}
191
+	}
192
+
193
+	/**
194
+	 * Returns the column name.
195
+	 *
196
+	 * @return string
197
+	 */
198
+	public function getColumnName()
199
+	{
200
+		return $this->column->getName();
201
+	}
202 202
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/QueryFactory/AbstractQueryFactory.php 1 patch
Indentation   +210 added lines, -210 removed lines patch added patch discarded remove patch
@@ -10,214 +10,214 @@
 block discarded – undo
10 10
 
11 11
 abstract class AbstractQueryFactory implements QueryFactory
12 12
 {
13
-    /**
14
-     * @var TDBMService
15
-     */
16
-    protected $tdbmService;
17
-
18
-    /**
19
-     * @var Schema
20
-     */
21
-    protected $schema;
22
-
23
-    /**
24
-     * @var OrderByAnalyzer
25
-     */
26
-    protected $orderByAnalyzer;
27
-
28
-    /**
29
-     * @var string|UncheckedOrderBy|null
30
-     */
31
-    protected $orderBy;
32
-
33
-    protected $magicSql;
34
-    protected $magicSqlCount;
35
-    protected $columnDescList;
36
-
37
-    /**
38
-     * @param TDBMService $tdbmService
39
-     */
40
-    public function __construct(TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, $orderBy)
41
-    {
42
-        $this->tdbmService = $tdbmService;
43
-        $this->schema = $schema;
44
-        $this->orderByAnalyzer = $orderByAnalyzer;
45
-        $this->orderBy = $orderBy;
46
-    }
47
-
48
-    /**
49
-     * Returns the column list that must be fetched for the SQL request.
50
-     *
51
-     * Note: MySQL dictates that ORDER BYed columns should appear in the SELECT clause.
52
-     *
53
-     * @param string                       $mainTable
54
-     * @param array                        $additionalTablesFetch
55
-     * @param string|UncheckedOrderBy|null $orderBy
56
-     *
57
-     * @return array
58
-     *
59
-     * @throws \Doctrine\DBAL\Schema\SchemaException
60
-     */
61
-    protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null)
62
-    {
63
-        // From the table name and the additional tables we want to fetch, let's build a list of all tables
64
-        // that must be part of the select columns.
65
-
66
-        $connection = $this->tdbmService->getConnection();
67
-
68
-        $tableGroups = [];
69
-        $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable);
70
-        $tableGroupName = $this->getTableGroupName($allFetchedTables);
71
-        foreach ($allFetchedTables as $table) {
72
-            $tableGroups[$table] = $tableGroupName;
73
-        }
74
-
75
-        $columnsList = [];
76
-        $columnDescList = [];
77
-        $sortColumn = 0;
78
-        $reconstructedOrderBy = null;
79
-
80
-        if (is_string($orderBy)) {
81
-            $orderBy = trim($orderBy);
82
-            if ($orderBy === '') {
83
-                $orderBy = null;
84
-            }
85
-        }
86
-
87
-        // Now, let's deal with "order by columns"
88
-        if ($orderBy !== null) {
89
-            if ($orderBy instanceof UncheckedOrderBy) {
90
-                $securedOrderBy = false;
91
-                $orderBy = $orderBy->getOrderBy();
92
-                $reconstructedOrderBy = $orderBy;
93
-            } else {
94
-                $securedOrderBy = true;
95
-                $reconstructedOrderBys = [];
96
-            }
97
-            $orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy);
98
-
99
-            // If we sort by a column, there is a high chance we will fetch the bean containing this column.
100
-            // Hence, we should add the table to the $additionalTablesFetch
101
-            foreach ($orderByColumns as $orderByColumn) {
102
-                if ($orderByColumn['type'] === 'colref') {
103
-                    if ($orderByColumn['table'] !== null) {
104
-                        $additionalTablesFetch[] = $orderByColumn['table'];
105
-                    }
106
-                    if ($securedOrderBy) {
107
-                        $reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $connection->quoteIdentifier($orderByColumn['table']).'.' : '').$connection->quoteIdentifier($orderByColumn['column']).' '.$orderByColumn['direction'];
108
-                    }
109
-                } elseif ($orderByColumn['type'] === 'expr') {
110
-                    $sortColumnName = 'sort_column_'.$sortColumn;
111
-                    $columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName;
112
-                    $columnDescList[] = [
113
-                        'tableGroup' => null,
114
-                    ];
115
-                    ++$sortColumn;
116
-
117
-                    if ($securedOrderBy) {
118
-                        throw new TDBMInvalidArgumentException('Invalid ORDER BY column: "'.$orderByColumn['expr'].'". If you want to use expression in your ORDER BY clause, you must wrap them in a UncheckedOrderBy object. For instance: new UncheckedOrderBy("col1 + col2 DESC")');
119
-                    }
120
-                }
121
-            }
122
-
123
-            if ($reconstructedOrderBy === null) {
124
-                $reconstructedOrderBy = implode(', ', $reconstructedOrderBys);
125
-            }
126
-        }
127
-
128
-        foreach ($additionalTablesFetch as $additionalTable) {
129
-            $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable);
130
-            $tableGroupName = $this->getTableGroupName($relatedTables);
131
-            foreach ($relatedTables as $table) {
132
-                $tableGroups[$table] = $tableGroupName;
133
-            }
134
-            $allFetchedTables = array_merge($allFetchedTables, $relatedTables);
135
-        }
136
-
137
-        // Let's remove any duplicate
138
-        $allFetchedTables = array_flip(array_flip($allFetchedTables));
139
-
140
-        // Now, let's build the column list
141
-        foreach ($allFetchedTables as $table) {
142
-            foreach ($this->schema->getTable($table)->getColumns() as $column) {
143
-                $columnName = $column->getName();
144
-                $columnDescList[] = [
145
-                    'as' => $table.'____'.$columnName,
146
-                    'table' => $table,
147
-                    'column' => $columnName,
148
-                    'type' => $column->getType(),
149
-                    'tableGroup' => $tableGroups[$table],
150
-                ];
151
-                $columnsList[] = $connection->quoteIdentifier($table).'.'.$connection->quoteIdentifier($columnName).' as '.
152
-                    $connection->quoteIdentifier($table.'____'.$columnName);
153
-            }
154
-        }
155
-
156
-        return [$columnDescList, $columnsList, $reconstructedOrderBy];
157
-    }
158
-
159
-    abstract protected function compute();
160
-
161
-    /**
162
-     * Returns an identifier for the group of tables passed in parameter.
163
-     *
164
-     * @param string[] $relatedTables
165
-     *
166
-     * @return string
167
-     */
168
-    protected function getTableGroupName(array $relatedTables)
169
-    {
170
-        sort($relatedTables);
171
-
172
-        return implode('_``_', $relatedTables);
173
-    }
174
-
175
-    public function getMagicSql() : string
176
-    {
177
-        if ($this->magicSql === null) {
178
-            $this->compute();
179
-        }
180
-
181
-        return $this->magicSql;
182
-    }
183
-
184
-    public function getMagicSqlCount() : string
185
-    {
186
-        if ($this->magicSqlCount === null) {
187
-            $this->compute();
188
-        }
189
-
190
-        return $this->magicSqlCount;
191
-    }
192
-
193
-    public function getColumnDescriptors() : array
194
-    {
195
-        if ($this->columnDescList === null) {
196
-            $this->compute();
197
-        }
198
-
199
-        return $this->columnDescList;
200
-    }
201
-
202
-    /**
203
-     * Sets the ORDER BY directive executed in SQL.
204
-     *
205
-     * For instance:
206
-     *
207
-     *  $queryFactory->sort('label ASC, status DESC');
208
-     *
209
-     * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter.
210
-     * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter:
211
-     *
212
-     *  $queryFactory->sort(new UncheckedOrderBy('RAND()'))
213
-     *
214
-     * @param string|UncheckedOrderBy|null $orderBy
215
-     */
216
-    public function sort($orderBy)
217
-    {
218
-        $this->orderBy = $orderBy;
219
-        $this->magicSql = null;
220
-        $this->magicSqlCount = null;
221
-        $this->columnDescList = null;
222
-    }
13
+	/**
14
+	 * @var TDBMService
15
+	 */
16
+	protected $tdbmService;
17
+
18
+	/**
19
+	 * @var Schema
20
+	 */
21
+	protected $schema;
22
+
23
+	/**
24
+	 * @var OrderByAnalyzer
25
+	 */
26
+	protected $orderByAnalyzer;
27
+
28
+	/**
29
+	 * @var string|UncheckedOrderBy|null
30
+	 */
31
+	protected $orderBy;
32
+
33
+	protected $magicSql;
34
+	protected $magicSqlCount;
35
+	protected $columnDescList;
36
+
37
+	/**
38
+	 * @param TDBMService $tdbmService
39
+	 */
40
+	public function __construct(TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, $orderBy)
41
+	{
42
+		$this->tdbmService = $tdbmService;
43
+		$this->schema = $schema;
44
+		$this->orderByAnalyzer = $orderByAnalyzer;
45
+		$this->orderBy = $orderBy;
46
+	}
47
+
48
+	/**
49
+	 * Returns the column list that must be fetched for the SQL request.
50
+	 *
51
+	 * Note: MySQL dictates that ORDER BYed columns should appear in the SELECT clause.
52
+	 *
53
+	 * @param string                       $mainTable
54
+	 * @param array                        $additionalTablesFetch
55
+	 * @param string|UncheckedOrderBy|null $orderBy
56
+	 *
57
+	 * @return array
58
+	 *
59
+	 * @throws \Doctrine\DBAL\Schema\SchemaException
60
+	 */
61
+	protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null)
62
+	{
63
+		// From the table name and the additional tables we want to fetch, let's build a list of all tables
64
+		// that must be part of the select columns.
65
+
66
+		$connection = $this->tdbmService->getConnection();
67
+
68
+		$tableGroups = [];
69
+		$allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable);
70
+		$tableGroupName = $this->getTableGroupName($allFetchedTables);
71
+		foreach ($allFetchedTables as $table) {
72
+			$tableGroups[$table] = $tableGroupName;
73
+		}
74
+
75
+		$columnsList = [];
76
+		$columnDescList = [];
77
+		$sortColumn = 0;
78
+		$reconstructedOrderBy = null;
79
+
80
+		if (is_string($orderBy)) {
81
+			$orderBy = trim($orderBy);
82
+			if ($orderBy === '') {
83
+				$orderBy = null;
84
+			}
85
+		}
86
+
87
+		// Now, let's deal with "order by columns"
88
+		if ($orderBy !== null) {
89
+			if ($orderBy instanceof UncheckedOrderBy) {
90
+				$securedOrderBy = false;
91
+				$orderBy = $orderBy->getOrderBy();
92
+				$reconstructedOrderBy = $orderBy;
93
+			} else {
94
+				$securedOrderBy = true;
95
+				$reconstructedOrderBys = [];
96
+			}
97
+			$orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy);
98
+
99
+			// If we sort by a column, there is a high chance we will fetch the bean containing this column.
100
+			// Hence, we should add the table to the $additionalTablesFetch
101
+			foreach ($orderByColumns as $orderByColumn) {
102
+				if ($orderByColumn['type'] === 'colref') {
103
+					if ($orderByColumn['table'] !== null) {
104
+						$additionalTablesFetch[] = $orderByColumn['table'];
105
+					}
106
+					if ($securedOrderBy) {
107
+						$reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $connection->quoteIdentifier($orderByColumn['table']).'.' : '').$connection->quoteIdentifier($orderByColumn['column']).' '.$orderByColumn['direction'];
108
+					}
109
+				} elseif ($orderByColumn['type'] === 'expr') {
110
+					$sortColumnName = 'sort_column_'.$sortColumn;
111
+					$columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName;
112
+					$columnDescList[] = [
113
+						'tableGroup' => null,
114
+					];
115
+					++$sortColumn;
116
+
117
+					if ($securedOrderBy) {
118
+						throw new TDBMInvalidArgumentException('Invalid ORDER BY column: "'.$orderByColumn['expr'].'". If you want to use expression in your ORDER BY clause, you must wrap them in a UncheckedOrderBy object. For instance: new UncheckedOrderBy("col1 + col2 DESC")');
119
+					}
120
+				}
121
+			}
122
+
123
+			if ($reconstructedOrderBy === null) {
124
+				$reconstructedOrderBy = implode(', ', $reconstructedOrderBys);
125
+			}
126
+		}
127
+
128
+		foreach ($additionalTablesFetch as $additionalTable) {
129
+			$relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable);
130
+			$tableGroupName = $this->getTableGroupName($relatedTables);
131
+			foreach ($relatedTables as $table) {
132
+				$tableGroups[$table] = $tableGroupName;
133
+			}
134
+			$allFetchedTables = array_merge($allFetchedTables, $relatedTables);
135
+		}
136
+
137
+		// Let's remove any duplicate
138
+		$allFetchedTables = array_flip(array_flip($allFetchedTables));
139
+
140
+		// Now, let's build the column list
141
+		foreach ($allFetchedTables as $table) {
142
+			foreach ($this->schema->getTable($table)->getColumns() as $column) {
143
+				$columnName = $column->getName();
144
+				$columnDescList[] = [
145
+					'as' => $table.'____'.$columnName,
146
+					'table' => $table,
147
+					'column' => $columnName,
148
+					'type' => $column->getType(),
149
+					'tableGroup' => $tableGroups[$table],
150
+				];
151
+				$columnsList[] = $connection->quoteIdentifier($table).'.'.$connection->quoteIdentifier($columnName).' as '.
152
+					$connection->quoteIdentifier($table.'____'.$columnName);
153
+			}
154
+		}
155
+
156
+		return [$columnDescList, $columnsList, $reconstructedOrderBy];
157
+	}
158
+
159
+	abstract protected function compute();
160
+
161
+	/**
162
+	 * Returns an identifier for the group of tables passed in parameter.
163
+	 *
164
+	 * @param string[] $relatedTables
165
+	 *
166
+	 * @return string
167
+	 */
168
+	protected function getTableGroupName(array $relatedTables)
169
+	{
170
+		sort($relatedTables);
171
+
172
+		return implode('_``_', $relatedTables);
173
+	}
174
+
175
+	public function getMagicSql() : string
176
+	{
177
+		if ($this->magicSql === null) {
178
+			$this->compute();
179
+		}
180
+
181
+		return $this->magicSql;
182
+	}
183
+
184
+	public function getMagicSqlCount() : string
185
+	{
186
+		if ($this->magicSqlCount === null) {
187
+			$this->compute();
188
+		}
189
+
190
+		return $this->magicSqlCount;
191
+	}
192
+
193
+	public function getColumnDescriptors() : array
194
+	{
195
+		if ($this->columnDescList === null) {
196
+			$this->compute();
197
+		}
198
+
199
+		return $this->columnDescList;
200
+	}
201
+
202
+	/**
203
+	 * Sets the ORDER BY directive executed in SQL.
204
+	 *
205
+	 * For instance:
206
+	 *
207
+	 *  $queryFactory->sort('label ASC, status DESC');
208
+	 *
209
+	 * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter.
210
+	 * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter:
211
+	 *
212
+	 *  $queryFactory->sort(new UncheckedOrderBy('RAND()'))
213
+	 *
214
+	 * @param string|UncheckedOrderBy|null $orderBy
215
+	 */
216
+	public function sort($orderBy)
217
+	{
218
+		$this->orderBy = $orderBy;
219
+		$this->magicSql = null;
220
+		$this->magicSqlCount = null;
221
+		$this->columnDescList = null;
222
+	}
223 223
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/QueryFactory/FindObjectsFromSqlQueryFactory.php 2 patches
Indentation   +187 added lines, -187 removed lines patch added patch discarded remove patch
@@ -15,191 +15,191 @@
 block discarded – undo
15 15
  */
16 16
 class FindObjectsFromSqlQueryFactory extends AbstractQueryFactory
17 17
 {
18
-    private $mainTable;
19
-    private $from;
20
-    private $filterString;
21
-    private $cache;
22
-    private $cachePrefix;
23
-
24
-    public function __construct(string $mainTable, string $from, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, SchemaAnalyzer $schemaAnalyzer, Cache $cache, string $cachePrefix)
25
-    {
26
-        parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy);
27
-        $this->mainTable = $mainTable;
28
-        $this->from = $from;
29
-        $this->filterString = $filterString;
30
-        $this->schemaAnalyzer = $schemaAnalyzer;
31
-        $this->cache = $cache;
32
-        $this->cachePrefix = $cachePrefix;
33
-    }
34
-
35
-    protected function compute()
36
-    {
37
-        $connection = $this->tdbmService->getConnection();
38
-
39
-        $columnsList = null;
40
-
41
-        $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable);
42
-
43
-        $columnDescList = [];
44
-
45
-        $tableGroupName = $this->getTableGroupName($allFetchedTables);
46
-
47
-        foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) {
48
-            $columnName = $column->getName();
49
-            $columnDescList[] = [
50
-                'as' => $columnName,
51
-                'table' => $this->mainTable,
52
-                'column' => $columnName,
53
-                'type' => $column->getType(),
54
-                'tableGroup' => $tableGroupName,
55
-            ];
56
-        }
57
-
58
-        $sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) {
59
-            return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']);
60
-        }, $columnDescList)).' FROM '.$this->from;
61
-
62
-        if (count($allFetchedTables) > 1) {
63
-            list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
64
-        } elseif ($this->orderBy) {
65
-            list(, , $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
66
-        }
67
-
68
-        // Let's compute the COUNT.
69
-        $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns();
70
-        $pkColumnNames = array_map(function ($pkColumn) {
71
-            return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn);
72
-        }, $pkColumnNames);
73
-
74
-        $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from;
75
-
76
-        if (!empty($this->filterString)) {
77
-            $sql .= ' WHERE '.$this->filterString;
78
-            $countSql .= ' WHERE '.$this->filterString;
79
-        }
80
-
81
-        if (!empty($orderString)) {
82
-            $sql .= ' ORDER BY '.$orderString;
83
-        }
84
-
85
-        if (stripos($countSql, 'GROUP BY') !== false) {
86
-            throw new TDBMException('Unsupported use of GROUP BY in SQL request.');
87
-        }
88
-
89
-        if ($columnsList !== null) {
90
-            $joinSql = '';
91
-            $parentFks = $this->getParentRelationshipForeignKeys($this->mainTable);
92
-            foreach ($parentFks as $fk) {
93
-                $joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)',
94
-                    $connection->quoteIdentifier($fk->getForeignTableName()),
95
-                    $connection->quoteIdentifier($fk->getLocalTableName()),
96
-                    $connection->quoteIdentifier($fk->getLocalColumns()[0]),
97
-                    $connection->quoteIdentifier($fk->getForeignTableName()),
98
-                    $connection->quoteIdentifier($fk->getForeignColumns()[0])
99
-                );
100
-            }
101
-
102
-            $childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable);
103
-            foreach ($childrenFks as $fk) {
104
-                $joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)',
105
-                    $connection->quoteIdentifier($fk->getLocalTableName()),
106
-                    $connection->quoteIdentifier($fk->getForeignTableName()),
107
-                    $connection->quoteIdentifier($fk->getForeignColumns()[0]),
108
-                    $connection->quoteIdentifier($fk->getLocalTableName()),
109
-                    $connection->quoteIdentifier($fk->getLocalColumns()[0])
110
-                );
111
-            }
112
-
113
-            $sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql;
114
-            if (!empty($orderString)) {
115
-                $sql .= ' ORDER BY '.$orderString;
116
-            }
117
-        }
118
-
119
-        $this->magicSql = $sql;
120
-        $this->magicSqlCount = $countSql;
121
-        $this->columnDescList = $columnDescList;
122
-    }
123
-
124
-    /**
125
-     * @param string $tableName
126
-     *
127
-     * @return ForeignKeyConstraint[]
128
-     */
129
-    private function getParentRelationshipForeignKeys($tableName)
130
-    {
131
-        return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) {
132
-            return $this->getParentRelationshipForeignKeysWithoutCache($tableName);
133
-        });
134
-    }
135
-
136
-    /**
137
-     * @param string $tableName
138
-     *
139
-     * @return ForeignKeyConstraint[]
140
-     */
141
-    private function getParentRelationshipForeignKeysWithoutCache($tableName)
142
-    {
143
-        $parentFks = [];
144
-        $currentTable = $tableName;
145
-        while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) {
146
-            $currentTable = $currentFk->getForeignTableName();
147
-            $parentFks[] = $currentFk;
148
-        }
149
-
150
-        return $parentFks;
151
-    }
152
-
153
-    /**
154
-     * @param string $tableName
155
-     *
156
-     * @return ForeignKeyConstraint[]
157
-     */
158
-    private function getChildrenRelationshipForeignKeys(string $tableName) : array
159
-    {
160
-        return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) {
161
-            return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName);
162
-        });
163
-    }
164
-
165
-    /**
166
-     * @param string $tableName
167
-     *
168
-     * @return ForeignKeyConstraint[]
169
-     */
170
-    private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array
171
-    {
172
-        $children = $this->schemaAnalyzer->getChildrenRelationships($tableName);
173
-
174
-        if (!empty($children)) {
175
-            $fksTables = array_map(function (ForeignKeyConstraint $fk) {
176
-                return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName());
177
-            }, $children);
178
-
179
-            $fks = array_merge($children, call_user_func_array('array_merge', $fksTables));
180
-
181
-            return $fks;
182
-        } else {
183
-            return [];
184
-        }
185
-    }
186
-
187
-    /**
188
-     * Returns an item from cache or computes it using $closure and puts it in cache.
189
-     *
190
-     * @param string   $key
191
-     * @param callable $closure
192
-     *
193
-     * @return mixed
194
-     */
195
-    protected function fromCache(string $key, callable $closure)
196
-    {
197
-        $item = $this->cache->fetch($key);
198
-        if ($item === false) {
199
-            $item = $closure();
200
-            $this->cache->save($key, $item);
201
-        }
202
-
203
-        return $item;
204
-    }
18
+	private $mainTable;
19
+	private $from;
20
+	private $filterString;
21
+	private $cache;
22
+	private $cachePrefix;
23
+
24
+	public function __construct(string $mainTable, string $from, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, SchemaAnalyzer $schemaAnalyzer, Cache $cache, string $cachePrefix)
25
+	{
26
+		parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy);
27
+		$this->mainTable = $mainTable;
28
+		$this->from = $from;
29
+		$this->filterString = $filterString;
30
+		$this->schemaAnalyzer = $schemaAnalyzer;
31
+		$this->cache = $cache;
32
+		$this->cachePrefix = $cachePrefix;
33
+	}
34
+
35
+	protected function compute()
36
+	{
37
+		$connection = $this->tdbmService->getConnection();
38
+
39
+		$columnsList = null;
40
+
41
+		$allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable);
42
+
43
+		$columnDescList = [];
44
+
45
+		$tableGroupName = $this->getTableGroupName($allFetchedTables);
46
+
47
+		foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) {
48
+			$columnName = $column->getName();
49
+			$columnDescList[] = [
50
+				'as' => $columnName,
51
+				'table' => $this->mainTable,
52
+				'column' => $columnName,
53
+				'type' => $column->getType(),
54
+				'tableGroup' => $tableGroupName,
55
+			];
56
+		}
57
+
58
+		$sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) {
59
+			return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']);
60
+		}, $columnDescList)).' FROM '.$this->from;
61
+
62
+		if (count($allFetchedTables) > 1) {
63
+			list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
64
+		} elseif ($this->orderBy) {
65
+			list(, , $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
66
+		}
67
+
68
+		// Let's compute the COUNT.
69
+		$pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns();
70
+		$pkColumnNames = array_map(function ($pkColumn) {
71
+			return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn);
72
+		}, $pkColumnNames);
73
+
74
+		$countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from;
75
+
76
+		if (!empty($this->filterString)) {
77
+			$sql .= ' WHERE '.$this->filterString;
78
+			$countSql .= ' WHERE '.$this->filterString;
79
+		}
80
+
81
+		if (!empty($orderString)) {
82
+			$sql .= ' ORDER BY '.$orderString;
83
+		}
84
+
85
+		if (stripos($countSql, 'GROUP BY') !== false) {
86
+			throw new TDBMException('Unsupported use of GROUP BY in SQL request.');
87
+		}
88
+
89
+		if ($columnsList !== null) {
90
+			$joinSql = '';
91
+			$parentFks = $this->getParentRelationshipForeignKeys($this->mainTable);
92
+			foreach ($parentFks as $fk) {
93
+				$joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)',
94
+					$connection->quoteIdentifier($fk->getForeignTableName()),
95
+					$connection->quoteIdentifier($fk->getLocalTableName()),
96
+					$connection->quoteIdentifier($fk->getLocalColumns()[0]),
97
+					$connection->quoteIdentifier($fk->getForeignTableName()),
98
+					$connection->quoteIdentifier($fk->getForeignColumns()[0])
99
+				);
100
+			}
101
+
102
+			$childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable);
103
+			foreach ($childrenFks as $fk) {
104
+				$joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)',
105
+					$connection->quoteIdentifier($fk->getLocalTableName()),
106
+					$connection->quoteIdentifier($fk->getForeignTableName()),
107
+					$connection->quoteIdentifier($fk->getForeignColumns()[0]),
108
+					$connection->quoteIdentifier($fk->getLocalTableName()),
109
+					$connection->quoteIdentifier($fk->getLocalColumns()[0])
110
+				);
111
+			}
112
+
113
+			$sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql;
114
+			if (!empty($orderString)) {
115
+				$sql .= ' ORDER BY '.$orderString;
116
+			}
117
+		}
118
+
119
+		$this->magicSql = $sql;
120
+		$this->magicSqlCount = $countSql;
121
+		$this->columnDescList = $columnDescList;
122
+	}
123
+
124
+	/**
125
+	 * @param string $tableName
126
+	 *
127
+	 * @return ForeignKeyConstraint[]
128
+	 */
129
+	private function getParentRelationshipForeignKeys($tableName)
130
+	{
131
+		return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) {
132
+			return $this->getParentRelationshipForeignKeysWithoutCache($tableName);
133
+		});
134
+	}
135
+
136
+	/**
137
+	 * @param string $tableName
138
+	 *
139
+	 * @return ForeignKeyConstraint[]
140
+	 */
141
+	private function getParentRelationshipForeignKeysWithoutCache($tableName)
142
+	{
143
+		$parentFks = [];
144
+		$currentTable = $tableName;
145
+		while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) {
146
+			$currentTable = $currentFk->getForeignTableName();
147
+			$parentFks[] = $currentFk;
148
+		}
149
+
150
+		return $parentFks;
151
+	}
152
+
153
+	/**
154
+	 * @param string $tableName
155
+	 *
156
+	 * @return ForeignKeyConstraint[]
157
+	 */
158
+	private function getChildrenRelationshipForeignKeys(string $tableName) : array
159
+	{
160
+		return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) {
161
+			return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName);
162
+		});
163
+	}
164
+
165
+	/**
166
+	 * @param string $tableName
167
+	 *
168
+	 * @return ForeignKeyConstraint[]
169
+	 */
170
+	private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array
171
+	{
172
+		$children = $this->schemaAnalyzer->getChildrenRelationships($tableName);
173
+
174
+		if (!empty($children)) {
175
+			$fksTables = array_map(function (ForeignKeyConstraint $fk) {
176
+				return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName());
177
+			}, $children);
178
+
179
+			$fks = array_merge($children, call_user_func_array('array_merge', $fksTables));
180
+
181
+			return $fks;
182
+		} else {
183
+			return [];
184
+		}
185
+	}
186
+
187
+	/**
188
+	 * Returns an item from cache or computes it using $closure and puts it in cache.
189
+	 *
190
+	 * @param string   $key
191
+	 * @param callable $closure
192
+	 *
193
+	 * @return mixed
194
+	 */
195
+	protected function fromCache(string $key, callable $closure)
196
+	{
197
+		$item = $this->cache->fetch($key);
198
+		if ($item === false) {
199
+			$item = $closure();
200
+			$this->cache->save($key, $item);
201
+		}
202
+
203
+		return $item;
204
+	}
205 205
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -55,19 +55,19 @@  discard block
 block discarded – undo
55 55
             ];
56 56
         }
57 57
 
58
-        $sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) {
58
+        $sql = 'SELECT DISTINCT '.implode(', ', array_map(function($columnDesc) {
59 59
             return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']);
60 60
         }, $columnDescList)).' FROM '.$this->from;
61 61
 
62
-        if (count($allFetchedTables) > 1) {
62
+        if (count($allFetchedTables)>1) {
63 63
             list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
64 64
         } elseif ($this->orderBy) {
65
-            list(, , $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
65
+            list(,, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
66 66
         }
67 67
 
68 68
         // Let's compute the COUNT.
69 69
         $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns();
70
-        $pkColumnNames = array_map(function ($pkColumn) {
70
+        $pkColumnNames = array_map(function($pkColumn) {
71 71
             return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn);
72 72
         }, $pkColumnNames);
73 73
 
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
      */
129 129
     private function getParentRelationshipForeignKeys($tableName)
130 130
     {
131
-        return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) {
131
+        return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function() use ($tableName) {
132 132
             return $this->getParentRelationshipForeignKeysWithoutCache($tableName);
133 133
         });
134 134
     }
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
      */
158 158
     private function getChildrenRelationshipForeignKeys(string $tableName) : array
159 159
     {
160
-        return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) {
160
+        return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function() use ($tableName) {
161 161
             return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName);
162 162
         });
163 163
     }
@@ -172,7 +172,7 @@  discard block
 block discarded – undo
172 172
         $children = $this->schemaAnalyzer->getChildrenRelationships($tableName);
173 173
 
174 174
         if (!empty($children)) {
175
-            $fksTables = array_map(function (ForeignKeyConstraint $fk) {
175
+            $fksTables = array_map(function(ForeignKeyConstraint $fk) {
176 176
                 return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName());
177 177
             }, $children);
178 178
 
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/AbstractTDBMObject.php 2 patches
Indentation   +606 added lines, -606 removed lines patch added patch discarded remove patch
@@ -31,615 +31,615 @@
 block discarded – undo
31 31
  */
32 32
 abstract class AbstractTDBMObject implements JsonSerializable
33 33
 {
34
-    /**
35
-     * The service this object is bound to.
36
-     *
37
-     * @var TDBMService
38
-     */
39
-    protected $tdbmService;
40
-
41
-    /**
42
-     * An array of DbRow, indexed by table name.
43
-     *
44
-     * @var DbRow[]
45
-     */
46
-    protected $dbRows = [];
47
-
48
-    /**
49
-     * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
50
-     * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
51
-     * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
52
-     * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
53
-     *
54
-     * @var string
55
-     */
56
-    private $status;
57
-
58
-    /**
59
-     * Array storing beans related via many to many relationships (pivot tables).
60
-     *
61
-     * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage
62
-     */
63
-    private $relationships = [];
64
-
65
-    /**
66
-     * @var bool[] Key: pivot table name, value: whether a query was performed to load the data
67
-     */
68
-    private $loadedRelationships = [];
69
-
70
-    /**
71
-     * Array storing beans related via many to one relationships (this bean is pointed by external beans).
72
-     *
73
-     * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage
74
-     */
75
-    private $manyToOneRelationships = [];
76
-
77
-    /**
78
-     * Used with $primaryKeys when we want to retrieve an existing object
79
-     * and $primaryKeys=[] if we want a new object.
80
-     *
81
-     * @param string      $tableName
82
-     * @param array       $primaryKeys
83
-     * @param TDBMService $tdbmService
84
-     *
85
-     * @throws TDBMException
86
-     * @throws TDBMInvalidOperationException
87
-     */
88
-    public function __construct($tableName = null, array $primaryKeys = [], TDBMService $tdbmService = null)
89
-    {
90
-        // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation...
91
-        if (!empty($tableName)) {
92
-            $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
93
-        }
94
-
95
-        if ($tdbmService === null) {
96
-            $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED);
97
-        } else {
98
-            $this->_attach($tdbmService);
99
-            if (!empty($primaryKeys)) {
100
-                $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
101
-            } else {
102
-                $this->_setStatus(TDBMObjectStateEnum::STATE_NEW);
103
-            }
104
-        }
105
-    }
106
-
107
-    /**
108
-     * Alternative constructor called when data is fetched from database via a SELECT.
109
-     *
110
-     * @param array       $beanData    array<table, array<column, value>>
111
-     * @param TDBMService $tdbmService
112
-     */
113
-    public function _constructFromData(array $beanData, TDBMService $tdbmService)
114
-    {
115
-        $this->tdbmService = $tdbmService;
116
-
117
-        foreach ($beanData as $table => $columns) {
118
-            $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns);
119
-        }
120
-
121
-        $this->status = TDBMObjectStateEnum::STATE_LOADED;
122
-    }
123
-
124
-    /**
125
-     * Alternative constructor called when bean is lazily loaded.
126
-     *
127
-     * @param string      $tableName
128
-     * @param array       $primaryKeys
129
-     * @param TDBMService $tdbmService
130
-     */
131
-    public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService)
132
-    {
133
-        $this->tdbmService = $tdbmService;
134
-
135
-        $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
136
-
137
-        $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED;
138
-    }
139
-
140
-    public function _attach(TDBMService $tdbmService)
141
-    {
142
-        if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) {
143
-            throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.');
144
-        }
145
-        $this->tdbmService = $tdbmService;
146
-
147
-        // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level)
148
-        $tableNames = $this->getUsedTables();
149
-
150
-        $newDbRows = [];
151
-
152
-        foreach ($tableNames as $table) {
153
-            if (!isset($this->dbRows[$table])) {
154
-                $this->registerTable($table);
155
-            }
156
-            $newDbRows[$table] = $this->dbRows[$table];
157
-        }
158
-        $this->dbRows = $newDbRows;
159
-
160
-        $this->status = TDBMObjectStateEnum::STATE_NEW;
161
-        foreach ($this->dbRows as $dbRow) {
162
-            $dbRow->_attach($tdbmService);
163
-        }
164
-    }
165
-
166
-    /**
167
-     * Sets the state of the TDBM Object
168
-     * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
169
-     * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
170
-     * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
171
-     * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
172
-     *
173
-     * @param string $state
174
-     */
175
-    public function _setStatus($state)
176
-    {
177
-        $this->status = $state;
178
-
179
-        // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself.
180
-        foreach ($this->dbRows as $dbRow) {
181
-            $dbRow->_setStatus($state);
182
-        }
183
-
184
-        if ($state === TDBMObjectStateEnum::STATE_DELETED) {
185
-            $this->onDelete();
186
-        }
187
-    }
188
-
189
-    /**
190
-     * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null"
191
-     * or throws an error.
192
-     *
193
-     * @param string $tableName
194
-     *
195
-     * @return string
196
-     */
197
-    private function checkTableName($tableName = null)
198
-    {
199
-        if ($tableName === null) {
200
-            if (count($this->dbRows) > 1) {
201
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
202
-            } elseif (count($this->dbRows) === 1) {
203
-                $tableName = array_keys($this->dbRows)[0];
204
-            }
205
-        }
206
-
207
-        return $tableName;
208
-    }
209
-
210
-    protected function get($var, $tableName = null)
211
-    {
212
-        $tableName = $this->checkTableName($tableName);
213
-
214
-        if (!isset($this->dbRows[$tableName])) {
215
-            return;
216
-        }
217
-
218
-        return $this->dbRows[$tableName]->get($var);
219
-    }
220
-
221
-    protected function set($var, $value, $tableName = null)
222
-    {
223
-        if ($tableName === null) {
224
-            if (count($this->dbRows) > 1) {
225
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
226
-            } elseif (count($this->dbRows) === 1) {
227
-                $tableName = array_keys($this->dbRows)[0];
228
-            } else {
229
-                throw new TDBMException('Please specify a table for this object.');
230
-            }
231
-        }
232
-
233
-        if (!isset($this->dbRows[$tableName])) {
234
-            $this->registerTable($tableName);
235
-        }
236
-
237
-        $this->dbRows[$tableName]->set($var, $value);
238
-        if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
239
-            $this->status = TDBMObjectStateEnum::STATE_DIRTY;
240
-        }
241
-    }
242
-
243
-    /**
244
-     * @param string             $foreignKeyName
245
-     * @param AbstractTDBMObject $bean
246
-     */
247
-    protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null)
248
-    {
249
-        if ($tableName === null) {
250
-            if (count($this->dbRows) > 1) {
251
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
252
-            } elseif (count($this->dbRows) === 1) {
253
-                $tableName = array_keys($this->dbRows)[0];
254
-            } else {
255
-                throw new TDBMException('Please specify a table for this object.');
256
-            }
257
-        }
258
-
259
-        if (!isset($this->dbRows[$tableName])) {
260
-            $this->registerTable($tableName);
261
-        }
262
-
263
-        $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName);
264
-        if ($oldLinkedBean !== null) {
265
-            $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this);
266
-        }
267
-
268
-        $this->dbRows[$tableName]->setRef($foreignKeyName, $bean);
269
-        if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
270
-            $this->status = TDBMObjectStateEnum::STATE_DIRTY;
271
-        }
272
-
273
-        if ($bean !== null) {
274
-            $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this);
275
-        }
276
-    }
277
-
278
-    /**
279
-     * @param string $foreignKeyName A unique name for this reference
280
-     *
281
-     * @return AbstractTDBMObject|null
282
-     */
283
-    protected function getRef($foreignKeyName, $tableName = null)
284
-    {
285
-        $tableName = $this->checkTableName($tableName);
286
-
287
-        if (!isset($this->dbRows[$tableName])) {
288
-            return;
289
-        }
290
-
291
-        return $this->dbRows[$tableName]->getRef($foreignKeyName);
292
-    }
293
-
294
-    /**
295
-     * Adds a many to many relationship to this bean.
296
-     *
297
-     * @param string             $pivotTableName
298
-     * @param AbstractTDBMObject $remoteBean
299
-     */
300
-    protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
301
-    {
302
-        $this->setRelationship($pivotTableName, $remoteBean, 'new');
303
-    }
304
-
305
-    /**
306
-     * Returns true if there is a relationship to this bean.
307
-     *
308
-     * @param string             $pivotTableName
309
-     * @param AbstractTDBMObject $remoteBean
310
-     *
311
-     * @return bool
312
-     */
313
-    protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
314
-    {
315
-        $storage = $this->retrieveRelationshipsStorage($pivotTableName);
316
-
317
-        if ($storage->contains($remoteBean)) {
318
-            if ($storage[$remoteBean]['status'] !== 'delete') {
319
-                return true;
320
-            }
321
-        }
322
-
323
-        return false;
324
-    }
325
-
326
-    /**
327
-     * Internal TDBM method. Removes a many to many relationship from this bean.
328
-     *
329
-     * @param string             $pivotTableName
330
-     * @param AbstractTDBMObject $remoteBean
331
-     */
332
-    public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
333
-    {
334
-        if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') {
335
-            unset($this->relationships[$pivotTableName][$remoteBean]);
336
-            unset($remoteBean->relationships[$pivotTableName][$this]);
337
-        } else {
338
-            $this->setRelationship($pivotTableName, $remoteBean, 'delete');
339
-        }
340
-    }
341
-
342
-    /**
343
-     * Sets many to many relationships for this bean.
344
-     * Adds new relationships and removes unused ones.
345
-     *
346
-     * @param $pivotTableName
347
-     * @param array $remoteBeans
348
-     */
349
-    protected function setRelationships($pivotTableName, array $remoteBeans)
350
-    {
351
-        $storage = $this->retrieveRelationshipsStorage($pivotTableName);
352
-
353
-        foreach ($storage as $oldRemoteBean) {
354
-            if (!in_array($oldRemoteBean, $remoteBeans, true)) {
355
-                // $oldRemoteBean must be removed
356
-                $this->_removeRelationship($pivotTableName, $oldRemoteBean);
357
-            }
358
-        }
359
-
360
-        foreach ($remoteBeans as $remoteBean) {
361
-            if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') {
362
-                // $remoteBean must be added
363
-                $this->addRelationship($pivotTableName, $remoteBean);
364
-            }
365
-        }
366
-    }
367
-
368
-    /**
369
-     * Returns the list of objects linked to this bean via $pivotTableName.
370
-     *
371
-     * @param $pivotTableName
372
-     *
373
-     * @return \SplObjectStorage
374
-     */
375
-    private function retrieveRelationshipsStorage($pivotTableName)
376
-    {
377
-        $storage = $this->getRelationshipStorage($pivotTableName);
378
-        if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) {
379
-            return $storage;
380
-        }
381
-
382
-        $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this);
383
-        $this->loadedRelationships[$pivotTableName] = true;
384
-
385
-        foreach ($beans as $bean) {
386
-            if (isset($storage[$bean])) {
387
-                $oldStatus = $storage[$bean]['status'];
388
-                if ($oldStatus === 'delete') {
389
-                    // Keep deleted things deleted
390
-                    continue;
391
-                }
392
-            }
393
-            $this->setRelationship($pivotTableName, $bean, 'loaded');
394
-        }
395
-
396
-        return $storage;
397
-    }
398
-
399
-    /**
400
-     * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName.
401
-     *
402
-     * @param $pivotTableName
403
-     *
404
-     * @return AbstractTDBMObject[]
405
-     */
406
-    public function _getRelationships($pivotTableName)
407
-    {
408
-        return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName));
409
-    }
410
-
411
-    private function relationshipStorageToArray(\SplObjectStorage $storage)
412
-    {
413
-        $beans = [];
414
-        foreach ($storage as $bean) {
415
-            $statusArr = $storage[$bean];
416
-            if ($statusArr['status'] !== 'delete') {
417
-                $beans[] = $bean;
418
-            }
419
-        }
420
-
421
-        return $beans;
422
-    }
423
-
424
-    /**
425
-     * Declares a relationship between.
426
-     *
427
-     * @param string             $pivotTableName
428
-     * @param AbstractTDBMObject $remoteBean
429
-     * @param string             $status
430
-     */
431
-    private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status)
432
-    {
433
-        $storage = $this->getRelationshipStorage($pivotTableName);
434
-        $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]);
435
-        if ($this->status === TDBMObjectStateEnum::STATE_LOADED) {
436
-            $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY);
437
-        }
438
-
439
-        $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName);
440
-        $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]);
441
-    }
442
-
443
-    /**
444
-     * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists).
445
-     *
446
-     * @param string $pivotTableName
447
-     *
448
-     * @return \SplObjectStorage
449
-     */
450
-    private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage
451
-    {
452
-        return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage();
453
-    }
454
-
455
-    /**
456
-     * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists).
457
-     *
458
-     * @param string $tableName
459
-     * @param string $foreignKeyName
460
-     *
461
-     * @return AlterableResultIterator
462
-     */
463
-    private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator
464
-    {
465
-        $key = $tableName.'___'.$foreignKeyName;
466
-
467
-        return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator();
468
-    }
469
-
470
-    /**
471
-     * Declares a relationship between this bean and the bean pointing to it.
472
-     *
473
-     * @param string             $tableName
474
-     * @param string             $foreignKeyName
475
-     * @param AbstractTDBMObject $remoteBean
476
-     */
477
-    private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean)
478
-    {
479
-        $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
480
-        $alterableResultIterator->add($remoteBean);
481
-    }
482
-
483
-    /**
484
-     * Declares a relationship between this bean and the bean pointing to it.
485
-     *
486
-     * @param string             $tableName
487
-     * @param string             $foreignKeyName
488
-     * @param AbstractTDBMObject $remoteBean
489
-     */
490
-    private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean)
491
-    {
492
-        $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
493
-        $alterableResultIterator->remove($remoteBean);
494
-    }
495
-
496
-    /**
497
-     * Returns the list of objects linked to this bean via a given foreign key.
498
-     *
499
-     * @param string $tableName
500
-     * @param string $foreignKeyName
501
-     * @param string $searchTableName
502
-     * @param array  $searchFilter
503
-     * @param string $orderString     The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object !
504
-     *
505
-     * @return AlterableResultIterator
506
-     */
507
-    protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator
508
-    {
509
-        $key = $tableName.'___'.$foreignKeyName;
510
-        $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
511
-        if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) {
512
-            return $alterableResultIterator;
513
-        }
514
-
515
-        $unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString);
516
-
517
-        $alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator());
518
-
519
-        return $alterableResultIterator;
520
-    }
521
-
522
-    /**
523
-     * Reverts any changes made to the object and resumes it to its DB state.
524
-     * This can only be called on objects that come from database and that have not been deleted.
525
-     * Otherwise, this will throw an exception.
526
-     *
527
-     * @throws TDBMException
528
-     */
529
-    public function discardChanges()
530
-    {
531
-        if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) {
532
-            throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved.");
533
-        }
534
-
535
-        if ($this->status === TDBMObjectStateEnum::STATE_DELETED) {
536
-            throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.');
537
-        }
538
-
539
-        $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
540
-    }
541
-
542
-    /**
543
-     * Method used internally by TDBM. You should not use it directly.
544
-     * This method returns the status of the TDBMObject.
545
-     * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
546
-     * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
547
-     * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
548
-     * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
549
-     *
550
-     * @return string
551
-     */
552
-    public function _getStatus()
553
-    {
554
-        return $this->status;
555
-    }
556
-
557
-    /**
558
-     * Override the native php clone function for TDBMObjects.
559
-     */
560
-    public function __clone()
561
-    {
562
-        // Let's clone the many to many relationships
563
-        if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) {
564
-            $pivotTableList = array_keys($this->relationships);
565
-        } else {
566
-            $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this);
567
-        }
568
-
569
-        foreach ($pivotTableList as $pivotTable) {
570
-            $storage = $this->retrieveRelationshipsStorage($pivotTable);
571
-
572
-            // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!!
573
-            /*foreach ($storage as $remoteBean) {
34
+	/**
35
+	 * The service this object is bound to.
36
+	 *
37
+	 * @var TDBMService
38
+	 */
39
+	protected $tdbmService;
40
+
41
+	/**
42
+	 * An array of DbRow, indexed by table name.
43
+	 *
44
+	 * @var DbRow[]
45
+	 */
46
+	protected $dbRows = [];
47
+
48
+	/**
49
+	 * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
50
+	 * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
51
+	 * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
52
+	 * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
53
+	 *
54
+	 * @var string
55
+	 */
56
+	private $status;
57
+
58
+	/**
59
+	 * Array storing beans related via many to many relationships (pivot tables).
60
+	 *
61
+	 * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage
62
+	 */
63
+	private $relationships = [];
64
+
65
+	/**
66
+	 * @var bool[] Key: pivot table name, value: whether a query was performed to load the data
67
+	 */
68
+	private $loadedRelationships = [];
69
+
70
+	/**
71
+	 * Array storing beans related via many to one relationships (this bean is pointed by external beans).
72
+	 *
73
+	 * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage
74
+	 */
75
+	private $manyToOneRelationships = [];
76
+
77
+	/**
78
+	 * Used with $primaryKeys when we want to retrieve an existing object
79
+	 * and $primaryKeys=[] if we want a new object.
80
+	 *
81
+	 * @param string      $tableName
82
+	 * @param array       $primaryKeys
83
+	 * @param TDBMService $tdbmService
84
+	 *
85
+	 * @throws TDBMException
86
+	 * @throws TDBMInvalidOperationException
87
+	 */
88
+	public function __construct($tableName = null, array $primaryKeys = [], TDBMService $tdbmService = null)
89
+	{
90
+		// FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation...
91
+		if (!empty($tableName)) {
92
+			$this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
93
+		}
94
+
95
+		if ($tdbmService === null) {
96
+			$this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED);
97
+		} else {
98
+			$this->_attach($tdbmService);
99
+			if (!empty($primaryKeys)) {
100
+				$this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
101
+			} else {
102
+				$this->_setStatus(TDBMObjectStateEnum::STATE_NEW);
103
+			}
104
+		}
105
+	}
106
+
107
+	/**
108
+	 * Alternative constructor called when data is fetched from database via a SELECT.
109
+	 *
110
+	 * @param array       $beanData    array<table, array<column, value>>
111
+	 * @param TDBMService $tdbmService
112
+	 */
113
+	public function _constructFromData(array $beanData, TDBMService $tdbmService)
114
+	{
115
+		$this->tdbmService = $tdbmService;
116
+
117
+		foreach ($beanData as $table => $columns) {
118
+			$this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns);
119
+		}
120
+
121
+		$this->status = TDBMObjectStateEnum::STATE_LOADED;
122
+	}
123
+
124
+	/**
125
+	 * Alternative constructor called when bean is lazily loaded.
126
+	 *
127
+	 * @param string      $tableName
128
+	 * @param array       $primaryKeys
129
+	 * @param TDBMService $tdbmService
130
+	 */
131
+	public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService)
132
+	{
133
+		$this->tdbmService = $tdbmService;
134
+
135
+		$this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
136
+
137
+		$this->status = TDBMObjectStateEnum::STATE_NOT_LOADED;
138
+	}
139
+
140
+	public function _attach(TDBMService $tdbmService)
141
+	{
142
+		if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) {
143
+			throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.');
144
+		}
145
+		$this->tdbmService = $tdbmService;
146
+
147
+		// If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level)
148
+		$tableNames = $this->getUsedTables();
149
+
150
+		$newDbRows = [];
151
+
152
+		foreach ($tableNames as $table) {
153
+			if (!isset($this->dbRows[$table])) {
154
+				$this->registerTable($table);
155
+			}
156
+			$newDbRows[$table] = $this->dbRows[$table];
157
+		}
158
+		$this->dbRows = $newDbRows;
159
+
160
+		$this->status = TDBMObjectStateEnum::STATE_NEW;
161
+		foreach ($this->dbRows as $dbRow) {
162
+			$dbRow->_attach($tdbmService);
163
+		}
164
+	}
165
+
166
+	/**
167
+	 * Sets the state of the TDBM Object
168
+	 * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
169
+	 * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
170
+	 * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
171
+	 * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
172
+	 *
173
+	 * @param string $state
174
+	 */
175
+	public function _setStatus($state)
176
+	{
177
+		$this->status = $state;
178
+
179
+		// TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself.
180
+		foreach ($this->dbRows as $dbRow) {
181
+			$dbRow->_setStatus($state);
182
+		}
183
+
184
+		if ($state === TDBMObjectStateEnum::STATE_DELETED) {
185
+			$this->onDelete();
186
+		}
187
+	}
188
+
189
+	/**
190
+	 * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null"
191
+	 * or throws an error.
192
+	 *
193
+	 * @param string $tableName
194
+	 *
195
+	 * @return string
196
+	 */
197
+	private function checkTableName($tableName = null)
198
+	{
199
+		if ($tableName === null) {
200
+			if (count($this->dbRows) > 1) {
201
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
202
+			} elseif (count($this->dbRows) === 1) {
203
+				$tableName = array_keys($this->dbRows)[0];
204
+			}
205
+		}
206
+
207
+		return $tableName;
208
+	}
209
+
210
+	protected function get($var, $tableName = null)
211
+	{
212
+		$tableName = $this->checkTableName($tableName);
213
+
214
+		if (!isset($this->dbRows[$tableName])) {
215
+			return;
216
+		}
217
+
218
+		return $this->dbRows[$tableName]->get($var);
219
+	}
220
+
221
+	protected function set($var, $value, $tableName = null)
222
+	{
223
+		if ($tableName === null) {
224
+			if (count($this->dbRows) > 1) {
225
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
226
+			} elseif (count($this->dbRows) === 1) {
227
+				$tableName = array_keys($this->dbRows)[0];
228
+			} else {
229
+				throw new TDBMException('Please specify a table for this object.');
230
+			}
231
+		}
232
+
233
+		if (!isset($this->dbRows[$tableName])) {
234
+			$this->registerTable($tableName);
235
+		}
236
+
237
+		$this->dbRows[$tableName]->set($var, $value);
238
+		if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
239
+			$this->status = TDBMObjectStateEnum::STATE_DIRTY;
240
+		}
241
+	}
242
+
243
+	/**
244
+	 * @param string             $foreignKeyName
245
+	 * @param AbstractTDBMObject $bean
246
+	 */
247
+	protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null)
248
+	{
249
+		if ($tableName === null) {
250
+			if (count($this->dbRows) > 1) {
251
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
252
+			} elseif (count($this->dbRows) === 1) {
253
+				$tableName = array_keys($this->dbRows)[0];
254
+			} else {
255
+				throw new TDBMException('Please specify a table for this object.');
256
+			}
257
+		}
258
+
259
+		if (!isset($this->dbRows[$tableName])) {
260
+			$this->registerTable($tableName);
261
+		}
262
+
263
+		$oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName);
264
+		if ($oldLinkedBean !== null) {
265
+			$oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this);
266
+		}
267
+
268
+		$this->dbRows[$tableName]->setRef($foreignKeyName, $bean);
269
+		if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
270
+			$this->status = TDBMObjectStateEnum::STATE_DIRTY;
271
+		}
272
+
273
+		if ($bean !== null) {
274
+			$bean->setManyToOneRelationship($tableName, $foreignKeyName, $this);
275
+		}
276
+	}
277
+
278
+	/**
279
+	 * @param string $foreignKeyName A unique name for this reference
280
+	 *
281
+	 * @return AbstractTDBMObject|null
282
+	 */
283
+	protected function getRef($foreignKeyName, $tableName = null)
284
+	{
285
+		$tableName = $this->checkTableName($tableName);
286
+
287
+		if (!isset($this->dbRows[$tableName])) {
288
+			return;
289
+		}
290
+
291
+		return $this->dbRows[$tableName]->getRef($foreignKeyName);
292
+	}
293
+
294
+	/**
295
+	 * Adds a many to many relationship to this bean.
296
+	 *
297
+	 * @param string             $pivotTableName
298
+	 * @param AbstractTDBMObject $remoteBean
299
+	 */
300
+	protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
301
+	{
302
+		$this->setRelationship($pivotTableName, $remoteBean, 'new');
303
+	}
304
+
305
+	/**
306
+	 * Returns true if there is a relationship to this bean.
307
+	 *
308
+	 * @param string             $pivotTableName
309
+	 * @param AbstractTDBMObject $remoteBean
310
+	 *
311
+	 * @return bool
312
+	 */
313
+	protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
314
+	{
315
+		$storage = $this->retrieveRelationshipsStorage($pivotTableName);
316
+
317
+		if ($storage->contains($remoteBean)) {
318
+			if ($storage[$remoteBean]['status'] !== 'delete') {
319
+				return true;
320
+			}
321
+		}
322
+
323
+		return false;
324
+	}
325
+
326
+	/**
327
+	 * Internal TDBM method. Removes a many to many relationship from this bean.
328
+	 *
329
+	 * @param string             $pivotTableName
330
+	 * @param AbstractTDBMObject $remoteBean
331
+	 */
332
+	public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
333
+	{
334
+		if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') {
335
+			unset($this->relationships[$pivotTableName][$remoteBean]);
336
+			unset($remoteBean->relationships[$pivotTableName][$this]);
337
+		} else {
338
+			$this->setRelationship($pivotTableName, $remoteBean, 'delete');
339
+		}
340
+	}
341
+
342
+	/**
343
+	 * Sets many to many relationships for this bean.
344
+	 * Adds new relationships and removes unused ones.
345
+	 *
346
+	 * @param $pivotTableName
347
+	 * @param array $remoteBeans
348
+	 */
349
+	protected function setRelationships($pivotTableName, array $remoteBeans)
350
+	{
351
+		$storage = $this->retrieveRelationshipsStorage($pivotTableName);
352
+
353
+		foreach ($storage as $oldRemoteBean) {
354
+			if (!in_array($oldRemoteBean, $remoteBeans, true)) {
355
+				// $oldRemoteBean must be removed
356
+				$this->_removeRelationship($pivotTableName, $oldRemoteBean);
357
+			}
358
+		}
359
+
360
+		foreach ($remoteBeans as $remoteBean) {
361
+			if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') {
362
+				// $remoteBean must be added
363
+				$this->addRelationship($pivotTableName, $remoteBean);
364
+			}
365
+		}
366
+	}
367
+
368
+	/**
369
+	 * Returns the list of objects linked to this bean via $pivotTableName.
370
+	 *
371
+	 * @param $pivotTableName
372
+	 *
373
+	 * @return \SplObjectStorage
374
+	 */
375
+	private function retrieveRelationshipsStorage($pivotTableName)
376
+	{
377
+		$storage = $this->getRelationshipStorage($pivotTableName);
378
+		if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) {
379
+			return $storage;
380
+		}
381
+
382
+		$beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this);
383
+		$this->loadedRelationships[$pivotTableName] = true;
384
+
385
+		foreach ($beans as $bean) {
386
+			if (isset($storage[$bean])) {
387
+				$oldStatus = $storage[$bean]['status'];
388
+				if ($oldStatus === 'delete') {
389
+					// Keep deleted things deleted
390
+					continue;
391
+				}
392
+			}
393
+			$this->setRelationship($pivotTableName, $bean, 'loaded');
394
+		}
395
+
396
+		return $storage;
397
+	}
398
+
399
+	/**
400
+	 * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName.
401
+	 *
402
+	 * @param $pivotTableName
403
+	 *
404
+	 * @return AbstractTDBMObject[]
405
+	 */
406
+	public function _getRelationships($pivotTableName)
407
+	{
408
+		return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName));
409
+	}
410
+
411
+	private function relationshipStorageToArray(\SplObjectStorage $storage)
412
+	{
413
+		$beans = [];
414
+		foreach ($storage as $bean) {
415
+			$statusArr = $storage[$bean];
416
+			if ($statusArr['status'] !== 'delete') {
417
+				$beans[] = $bean;
418
+			}
419
+		}
420
+
421
+		return $beans;
422
+	}
423
+
424
+	/**
425
+	 * Declares a relationship between.
426
+	 *
427
+	 * @param string             $pivotTableName
428
+	 * @param AbstractTDBMObject $remoteBean
429
+	 * @param string             $status
430
+	 */
431
+	private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status)
432
+	{
433
+		$storage = $this->getRelationshipStorage($pivotTableName);
434
+		$storage->attach($remoteBean, ['status' => $status, 'reverse' => false]);
435
+		if ($this->status === TDBMObjectStateEnum::STATE_LOADED) {
436
+			$this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY);
437
+		}
438
+
439
+		$remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName);
440
+		$remoteStorage->attach($this, ['status' => $status, 'reverse' => true]);
441
+	}
442
+
443
+	/**
444
+	 * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists).
445
+	 *
446
+	 * @param string $pivotTableName
447
+	 *
448
+	 * @return \SplObjectStorage
449
+	 */
450
+	private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage
451
+	{
452
+		return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage();
453
+	}
454
+
455
+	/**
456
+	 * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists).
457
+	 *
458
+	 * @param string $tableName
459
+	 * @param string $foreignKeyName
460
+	 *
461
+	 * @return AlterableResultIterator
462
+	 */
463
+	private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator
464
+	{
465
+		$key = $tableName.'___'.$foreignKeyName;
466
+
467
+		return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator();
468
+	}
469
+
470
+	/**
471
+	 * Declares a relationship between this bean and the bean pointing to it.
472
+	 *
473
+	 * @param string             $tableName
474
+	 * @param string             $foreignKeyName
475
+	 * @param AbstractTDBMObject $remoteBean
476
+	 */
477
+	private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean)
478
+	{
479
+		$alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
480
+		$alterableResultIterator->add($remoteBean);
481
+	}
482
+
483
+	/**
484
+	 * Declares a relationship between this bean and the bean pointing to it.
485
+	 *
486
+	 * @param string             $tableName
487
+	 * @param string             $foreignKeyName
488
+	 * @param AbstractTDBMObject $remoteBean
489
+	 */
490
+	private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean)
491
+	{
492
+		$alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
493
+		$alterableResultIterator->remove($remoteBean);
494
+	}
495
+
496
+	/**
497
+	 * Returns the list of objects linked to this bean via a given foreign key.
498
+	 *
499
+	 * @param string $tableName
500
+	 * @param string $foreignKeyName
501
+	 * @param string $searchTableName
502
+	 * @param array  $searchFilter
503
+	 * @param string $orderString     The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object !
504
+	 *
505
+	 * @return AlterableResultIterator
506
+	 */
507
+	protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator
508
+	{
509
+		$key = $tableName.'___'.$foreignKeyName;
510
+		$alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
511
+		if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) {
512
+			return $alterableResultIterator;
513
+		}
514
+
515
+		$unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString);
516
+
517
+		$alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator());
518
+
519
+		return $alterableResultIterator;
520
+	}
521
+
522
+	/**
523
+	 * Reverts any changes made to the object and resumes it to its DB state.
524
+	 * This can only be called on objects that come from database and that have not been deleted.
525
+	 * Otherwise, this will throw an exception.
526
+	 *
527
+	 * @throws TDBMException
528
+	 */
529
+	public function discardChanges()
530
+	{
531
+		if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) {
532
+			throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved.");
533
+		}
534
+
535
+		if ($this->status === TDBMObjectStateEnum::STATE_DELETED) {
536
+			throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.');
537
+		}
538
+
539
+		$this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
540
+	}
541
+
542
+	/**
543
+	 * Method used internally by TDBM. You should not use it directly.
544
+	 * This method returns the status of the TDBMObject.
545
+	 * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
546
+	 * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
547
+	 * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
548
+	 * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
549
+	 *
550
+	 * @return string
551
+	 */
552
+	public function _getStatus()
553
+	{
554
+		return $this->status;
555
+	}
556
+
557
+	/**
558
+	 * Override the native php clone function for TDBMObjects.
559
+	 */
560
+	public function __clone()
561
+	{
562
+		// Let's clone the many to many relationships
563
+		if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) {
564
+			$pivotTableList = array_keys($this->relationships);
565
+		} else {
566
+			$pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this);
567
+		}
568
+
569
+		foreach ($pivotTableList as $pivotTable) {
570
+			$storage = $this->retrieveRelationshipsStorage($pivotTable);
571
+
572
+			// Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!!
573
+			/*foreach ($storage as $remoteBean) {
574 574
                 $metadata = $storage[$remoteBean];
575 575
 
576 576
                 $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable);
577 577
                 $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]);
578 578
             }*/
579
-        }
580
-
581
-        // Let's clone each row
582
-        foreach ($this->dbRows as $key => &$dbRow) {
583
-            $dbRow = clone $dbRow;
584
-            $dbRow->setTDBMObject($this);
585
-        }
586
-
587
-        $this->manyToOneRelationships = [];
588
-
589
-        // Let's set the status to new (to enter the save function)
590
-        $this->status = TDBMObjectStateEnum::STATE_DETACHED;
591
-    }
592
-
593
-    /**
594
-     * Returns raw database rows.
595
-     *
596
-     * @return DbRow[] Key: table name, Value: DbRow object
597
-     */
598
-    public function _getDbRows()
599
-    {
600
-        return $this->dbRows;
601
-    }
602
-
603
-    private function registerTable($tableName)
604
-    {
605
-        $dbRow = new DbRow($this, $tableName);
606
-
607
-        if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) {
608
-            // Let's get the primary key for the new table
609
-            $anotherDbRow = array_values($this->dbRows)[0];
610
-            /* @var $anotherDbRow DbRow */
611
-            $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys());
612
-            $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys);
613
-            $dbRow->_setPrimaryKeys($primaryKeys);
614
-        }
615
-
616
-        $dbRow->_setStatus($this->status);
617
-
618
-        $this->dbRows[$tableName] = $dbRow;
619
-        // TODO: look at status (if not new)=> get primary key from tdbmservice
620
-    }
621
-
622
-    /**
623
-     * Internal function: return the list of relationships.
624
-     *
625
-     * @return \SplObjectStorage[]
626
-     */
627
-    public function _getCachedRelationships()
628
-    {
629
-        return $this->relationships;
630
-    }
631
-
632
-    /**
633
-     * Returns an array of used tables by this bean (from parent to child relationship).
634
-     *
635
-     * @return string[]
636
-     */
637
-    abstract protected function getUsedTables();
638
-
639
-    /**
640
-     * Method called when the bean is removed from database.
641
-     */
642
-    protected function onDelete()
643
-    {
644
-    }
579
+		}
580
+
581
+		// Let's clone each row
582
+		foreach ($this->dbRows as $key => &$dbRow) {
583
+			$dbRow = clone $dbRow;
584
+			$dbRow->setTDBMObject($this);
585
+		}
586
+
587
+		$this->manyToOneRelationships = [];
588
+
589
+		// Let's set the status to new (to enter the save function)
590
+		$this->status = TDBMObjectStateEnum::STATE_DETACHED;
591
+	}
592
+
593
+	/**
594
+	 * Returns raw database rows.
595
+	 *
596
+	 * @return DbRow[] Key: table name, Value: DbRow object
597
+	 */
598
+	public function _getDbRows()
599
+	{
600
+		return $this->dbRows;
601
+	}
602
+
603
+	private function registerTable($tableName)
604
+	{
605
+		$dbRow = new DbRow($this, $tableName);
606
+
607
+		if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) {
608
+			// Let's get the primary key for the new table
609
+			$anotherDbRow = array_values($this->dbRows)[0];
610
+			/* @var $anotherDbRow DbRow */
611
+			$indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys());
612
+			$primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys);
613
+			$dbRow->_setPrimaryKeys($primaryKeys);
614
+		}
615
+
616
+		$dbRow->_setStatus($this->status);
617
+
618
+		$this->dbRows[$tableName] = $dbRow;
619
+		// TODO: look at status (if not new)=> get primary key from tdbmservice
620
+	}
621
+
622
+	/**
623
+	 * Internal function: return the list of relationships.
624
+	 *
625
+	 * @return \SplObjectStorage[]
626
+	 */
627
+	public function _getCachedRelationships()
628
+	{
629
+		return $this->relationships;
630
+	}
631
+
632
+	/**
633
+	 * Returns an array of used tables by this bean (from parent to child relationship).
634
+	 *
635
+	 * @return string[]
636
+	 */
637
+	abstract protected function getUsedTables();
638
+
639
+	/**
640
+	 * Method called when the bean is removed from database.
641
+	 */
642
+	protected function onDelete()
643
+	{
644
+	}
645 645
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
     private function checkTableName($tableName = null)
198 198
     {
199 199
         if ($tableName === null) {
200
-            if (count($this->dbRows) > 1) {
200
+            if (count($this->dbRows)>1) {
201 201
                 throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
202 202
             } elseif (count($this->dbRows) === 1) {
203 203
                 $tableName = array_keys($this->dbRows)[0];
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
     protected function set($var, $value, $tableName = null)
222 222
     {
223 223
         if ($tableName === null) {
224
-            if (count($this->dbRows) > 1) {
224
+            if (count($this->dbRows)>1) {
225 225
                 throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
226 226
             } elseif (count($this->dbRows) === 1) {
227 227
                 $tableName = array_keys($this->dbRows)[0];
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
     protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null)
248 248
     {
249 249
         if ($tableName === null) {
250
-            if (count($this->dbRows) > 1) {
250
+            if (count($this->dbRows)>1) {
251 251
                 throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
252 252
             } elseif (count($this->dbRows) === 1) {
253 253
                 $tableName = array_keys($this->dbRows)[0];
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Utils/BeanDescriptor.php 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
      */
93 93
     public function getConstructorProperties()
94 94
     {
95
-        $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
95
+        $constructorProperties = array_filter($this->beanPropertyDescriptors, function(AbstractBeanPropertyDescriptor $property) {
96 96
             return $property->isCompulsory();
97 97
         });
98 98
 
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
     public function getPropertiesWithDefault()
108 108
     {
109 109
         $properties = $this->getPropertiesForTable($this->table);
110
-        $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) {
110
+        $defaultProperties = array_filter($properties, function(AbstractBeanPropertyDescriptor $property) {
111 111
             return $property->hasDefault();
112 112
         });
113 113
 
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
      */
122 122
     public function getExposedProperties()
123 123
     {
124
-        $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
124
+        $exposedProperties = array_filter($this->beanPropertyDescriptors, function(AbstractBeanPropertyDescriptor $property) {
125 125
             return $property->getTable()->getName() == $this->table->getName();
126 126
         });
127 127
 
@@ -331,7 +331,7 @@  discard block
 block discarded – undo
331 331
         }
332 332
 
333 333
         foreach ($descriptorsByMethodName as $descriptorsForMethodName) {
334
-            if (count($descriptorsForMethodName) > 1) {
334
+            if (count($descriptorsForMethodName)>1) {
335 335
                 foreach ($descriptorsForMethodName as $descriptor) {
336 336
                     $descriptor->useAlternativeName();
337 337
                 }
@@ -424,7 +424,7 @@  discard block
 block discarded – undo
424 424
 
425 425
         $classes = $this->generateExtendsAndUseStatements($parentFk);
426 426
 
427
-        $uses = array_map(function ($className) use ($beannamespace) {
427
+        $uses = array_map(function($className) use ($beannamespace) {
428 428
             return 'use '.$beannamespace.'\\'.$className.";\n";
429 429
         }, $classes);
430 430
         $use = implode('', $uses);
Please login to merge, or discard this patch.
Indentation   +543 added lines, -543 removed lines patch added patch discarded remove patch
@@ -16,228 +16,228 @@  discard block
 block discarded – undo
16 16
  */
17 17
 class BeanDescriptor
18 18
 {
19
-    /**
20
-     * @var Table
21
-     */
22
-    private $table;
23
-
24
-    /**
25
-     * @var SchemaAnalyzer
26
-     */
27
-    private $schemaAnalyzer;
28
-
29
-    /**
30
-     * @var Schema
31
-     */
32
-    private $schema;
33
-
34
-    /**
35
-     * @var AbstractBeanPropertyDescriptor[]
36
-     */
37
-    private $beanPropertyDescriptors = [];
38
-
39
-    /**
40
-     * @var TDBMSchemaAnalyzer
41
-     */
42
-    private $tdbmSchemaAnalyzer;
43
-
44
-    public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer)
45
-    {
46
-        $this->table = $table;
47
-        $this->schemaAnalyzer = $schemaAnalyzer;
48
-        $this->schema = $schema;
49
-        $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer;
50
-        $this->initBeanPropertyDescriptors();
51
-    }
52
-
53
-    private function initBeanPropertyDescriptors()
54
-    {
55
-        $this->beanPropertyDescriptors = $this->getProperties($this->table);
56
-    }
57
-
58
-    /**
59
-     * Returns the foreign-key the column is part of, if any. null otherwise.
60
-     *
61
-     * @param Table  $table
62
-     * @param Column $column
63
-     *
64
-     * @return ForeignKeyConstraint|null
65
-     */
66
-    private function isPartOfForeignKey(Table $table, Column $column)
67
-    {
68
-        $localColumnName = $column->getName();
69
-        foreach ($table->getForeignKeys() as $foreignKey) {
70
-            foreach ($foreignKey->getColumns() as $columnName) {
71
-                if ($columnName === $localColumnName) {
72
-                    return $foreignKey;
73
-                }
74
-            }
75
-        }
76
-
77
-        return;
78
-    }
79
-
80
-    /**
81
-     * @return AbstractBeanPropertyDescriptor[]
82
-     */
83
-    public function getBeanPropertyDescriptors()
84
-    {
85
-        return $this->beanPropertyDescriptors;
86
-    }
87
-
88
-    /**
89
-     * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent.
90
-     *
91
-     * @return AbstractBeanPropertyDescriptor[]
92
-     */
93
-    public function getConstructorProperties()
94
-    {
95
-        $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
96
-            return $property->isCompulsory();
97
-        });
98
-
99
-        return $constructorProperties;
100
-    }
101
-
102
-    /**
103
-     * Returns the list of columns that have default values for a given table.
104
-     *
105
-     * @return AbstractBeanPropertyDescriptor[]
106
-     */
107
-    public function getPropertiesWithDefault()
108
-    {
109
-        $properties = $this->getPropertiesForTable($this->table);
110
-        $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) {
111
-            return $property->hasDefault();
112
-        });
113
-
114
-        return $defaultProperties;
115
-    }
116
-
117
-    /**
118
-     * Returns the list of properties exposed as getters and setters in this class.
119
-     *
120
-     * @return AbstractBeanPropertyDescriptor[]
121
-     */
122
-    public function getExposedProperties()
123
-    {
124
-        $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
125
-            return $property->getTable()->getName() == $this->table->getName();
126
-        });
127
-
128
-        return $exposedProperties;
129
-    }
130
-
131
-    /**
132
-     * Returns the list of properties for this table (including parent tables).
133
-     *
134
-     * @param Table $table
135
-     *
136
-     * @return AbstractBeanPropertyDescriptor[]
137
-     */
138
-    private function getProperties(Table $table)
139
-    {
140
-        $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
141
-        if ($parentRelationship) {
142
-            $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName());
143
-            $properties = $this->getProperties($parentTable);
144
-            // we merge properties by overriding property names.
145
-            $localProperties = $this->getPropertiesForTable($table);
146
-            foreach ($localProperties as $name => $property) {
147
-                // We do not override properties if this is a primary key!
148
-                if ($property->isPrimaryKey()) {
149
-                    continue;
150
-                }
151
-                $properties[$name] = $property;
152
-            }
153
-        } else {
154
-            $properties = $this->getPropertiesForTable($table);
155
-        }
156
-
157
-        return $properties;
158
-    }
159
-
160
-    /**
161
-     * Returns the list of properties for this table (ignoring parent tables).
162
-     *
163
-     * @param Table $table
164
-     *
165
-     * @return AbstractBeanPropertyDescriptor[]
166
-     */
167
-    private function getPropertiesForTable(Table $table)
168
-    {
169
-        $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
170
-        if ($parentRelationship) {
171
-            $ignoreColumns = $parentRelationship->getLocalColumns();
172
-        } else {
173
-            $ignoreColumns = [];
174
-        }
175
-
176
-        $beanPropertyDescriptors = [];
177
-
178
-        foreach ($table->getColumns() as $column) {
179
-            if (array_search($column->getName(), $ignoreColumns) !== false) {
180
-                continue;
181
-            }
182
-
183
-            $fk = $this->isPartOfForeignKey($table, $column);
184
-            if ($fk !== null) {
185
-                // Check that previously added descriptors are not added on same FK (can happen with multi key FK).
186
-                foreach ($beanPropertyDescriptors as $beanDescriptor) {
187
-                    if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) {
188
-                        continue 2;
189
-                    }
190
-                }
191
-                // Check that this property is not an inheritance relationship
192
-                $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
193
-                if ($parentRelationship === $fk) {
194
-                    continue;
195
-                }
196
-
197
-                $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer);
198
-            } else {
199
-                $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column);
200
-            }
201
-        }
202
-
203
-        // Now, let's get the name of all properties and let's check there is no duplicate.
204
-        /** @var $names AbstractBeanPropertyDescriptor[] */
205
-        $names = [];
206
-        foreach ($beanPropertyDescriptors as $beanDescriptor) {
207
-            $name = $beanDescriptor->getUpperCamelCaseName();
208
-            if (isset($names[$name])) {
209
-                $names[$name]->useAlternativeName();
210
-                $beanDescriptor->useAlternativeName();
211
-            } else {
212
-                $names[$name] = $beanDescriptor;
213
-            }
214
-        }
215
-
216
-        // Final check (throw exceptions if problem arises)
217
-        $names = [];
218
-        foreach ($beanPropertyDescriptors as $beanDescriptor) {
219
-            $name = $beanDescriptor->getUpperCamelCaseName();
220
-            if (isset($names[$name])) {
221
-                throw new TDBMException('Unsolvable name conflict while generating method name');
222
-            } else {
223
-                $names[$name] = $beanDescriptor;
224
-            }
225
-        }
226
-
227
-        // Last step, let's rebuild the list with a map:
228
-        $beanPropertyDescriptorsMap = [];
229
-        foreach ($beanPropertyDescriptors as $beanDescriptor) {
230
-            $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor;
231
-        }
232
-
233
-        return $beanPropertyDescriptorsMap;
234
-    }
235
-
236
-    public function generateBeanConstructor()
237
-    {
238
-        $constructorProperties = $this->getConstructorProperties();
239
-
240
-        $constructorCode = '    /**
19
+	/**
20
+	 * @var Table
21
+	 */
22
+	private $table;
23
+
24
+	/**
25
+	 * @var SchemaAnalyzer
26
+	 */
27
+	private $schemaAnalyzer;
28
+
29
+	/**
30
+	 * @var Schema
31
+	 */
32
+	private $schema;
33
+
34
+	/**
35
+	 * @var AbstractBeanPropertyDescriptor[]
36
+	 */
37
+	private $beanPropertyDescriptors = [];
38
+
39
+	/**
40
+	 * @var TDBMSchemaAnalyzer
41
+	 */
42
+	private $tdbmSchemaAnalyzer;
43
+
44
+	public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer)
45
+	{
46
+		$this->table = $table;
47
+		$this->schemaAnalyzer = $schemaAnalyzer;
48
+		$this->schema = $schema;
49
+		$this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer;
50
+		$this->initBeanPropertyDescriptors();
51
+	}
52
+
53
+	private function initBeanPropertyDescriptors()
54
+	{
55
+		$this->beanPropertyDescriptors = $this->getProperties($this->table);
56
+	}
57
+
58
+	/**
59
+	 * Returns the foreign-key the column is part of, if any. null otherwise.
60
+	 *
61
+	 * @param Table  $table
62
+	 * @param Column $column
63
+	 *
64
+	 * @return ForeignKeyConstraint|null
65
+	 */
66
+	private function isPartOfForeignKey(Table $table, Column $column)
67
+	{
68
+		$localColumnName = $column->getName();
69
+		foreach ($table->getForeignKeys() as $foreignKey) {
70
+			foreach ($foreignKey->getColumns() as $columnName) {
71
+				if ($columnName === $localColumnName) {
72
+					return $foreignKey;
73
+				}
74
+			}
75
+		}
76
+
77
+		return;
78
+	}
79
+
80
+	/**
81
+	 * @return AbstractBeanPropertyDescriptor[]
82
+	 */
83
+	public function getBeanPropertyDescriptors()
84
+	{
85
+		return $this->beanPropertyDescriptors;
86
+	}
87
+
88
+	/**
89
+	 * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent.
90
+	 *
91
+	 * @return AbstractBeanPropertyDescriptor[]
92
+	 */
93
+	public function getConstructorProperties()
94
+	{
95
+		$constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
96
+			return $property->isCompulsory();
97
+		});
98
+
99
+		return $constructorProperties;
100
+	}
101
+
102
+	/**
103
+	 * Returns the list of columns that have default values for a given table.
104
+	 *
105
+	 * @return AbstractBeanPropertyDescriptor[]
106
+	 */
107
+	public function getPropertiesWithDefault()
108
+	{
109
+		$properties = $this->getPropertiesForTable($this->table);
110
+		$defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) {
111
+			return $property->hasDefault();
112
+		});
113
+
114
+		return $defaultProperties;
115
+	}
116
+
117
+	/**
118
+	 * Returns the list of properties exposed as getters and setters in this class.
119
+	 *
120
+	 * @return AbstractBeanPropertyDescriptor[]
121
+	 */
122
+	public function getExposedProperties()
123
+	{
124
+		$exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
125
+			return $property->getTable()->getName() == $this->table->getName();
126
+		});
127
+
128
+		return $exposedProperties;
129
+	}
130
+
131
+	/**
132
+	 * Returns the list of properties for this table (including parent tables).
133
+	 *
134
+	 * @param Table $table
135
+	 *
136
+	 * @return AbstractBeanPropertyDescriptor[]
137
+	 */
138
+	private function getProperties(Table $table)
139
+	{
140
+		$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
141
+		if ($parentRelationship) {
142
+			$parentTable = $this->schema->getTable($parentRelationship->getForeignTableName());
143
+			$properties = $this->getProperties($parentTable);
144
+			// we merge properties by overriding property names.
145
+			$localProperties = $this->getPropertiesForTable($table);
146
+			foreach ($localProperties as $name => $property) {
147
+				// We do not override properties if this is a primary key!
148
+				if ($property->isPrimaryKey()) {
149
+					continue;
150
+				}
151
+				$properties[$name] = $property;
152
+			}
153
+		} else {
154
+			$properties = $this->getPropertiesForTable($table);
155
+		}
156
+
157
+		return $properties;
158
+	}
159
+
160
+	/**
161
+	 * Returns the list of properties for this table (ignoring parent tables).
162
+	 *
163
+	 * @param Table $table
164
+	 *
165
+	 * @return AbstractBeanPropertyDescriptor[]
166
+	 */
167
+	private function getPropertiesForTable(Table $table)
168
+	{
169
+		$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
170
+		if ($parentRelationship) {
171
+			$ignoreColumns = $parentRelationship->getLocalColumns();
172
+		} else {
173
+			$ignoreColumns = [];
174
+		}
175
+
176
+		$beanPropertyDescriptors = [];
177
+
178
+		foreach ($table->getColumns() as $column) {
179
+			if (array_search($column->getName(), $ignoreColumns) !== false) {
180
+				continue;
181
+			}
182
+
183
+			$fk = $this->isPartOfForeignKey($table, $column);
184
+			if ($fk !== null) {
185
+				// Check that previously added descriptors are not added on same FK (can happen with multi key FK).
186
+				foreach ($beanPropertyDescriptors as $beanDescriptor) {
187
+					if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) {
188
+						continue 2;
189
+					}
190
+				}
191
+				// Check that this property is not an inheritance relationship
192
+				$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
193
+				if ($parentRelationship === $fk) {
194
+					continue;
195
+				}
196
+
197
+				$beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer);
198
+			} else {
199
+				$beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column);
200
+			}
201
+		}
202
+
203
+		// Now, let's get the name of all properties and let's check there is no duplicate.
204
+		/** @var $names AbstractBeanPropertyDescriptor[] */
205
+		$names = [];
206
+		foreach ($beanPropertyDescriptors as $beanDescriptor) {
207
+			$name = $beanDescriptor->getUpperCamelCaseName();
208
+			if (isset($names[$name])) {
209
+				$names[$name]->useAlternativeName();
210
+				$beanDescriptor->useAlternativeName();
211
+			} else {
212
+				$names[$name] = $beanDescriptor;
213
+			}
214
+		}
215
+
216
+		// Final check (throw exceptions if problem arises)
217
+		$names = [];
218
+		foreach ($beanPropertyDescriptors as $beanDescriptor) {
219
+			$name = $beanDescriptor->getUpperCamelCaseName();
220
+			if (isset($names[$name])) {
221
+				throw new TDBMException('Unsolvable name conflict while generating method name');
222
+			} else {
223
+				$names[$name] = $beanDescriptor;
224
+			}
225
+		}
226
+
227
+		// Last step, let's rebuild the list with a map:
228
+		$beanPropertyDescriptorsMap = [];
229
+		foreach ($beanPropertyDescriptors as $beanDescriptor) {
230
+			$beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor;
231
+		}
232
+
233
+		return $beanPropertyDescriptorsMap;
234
+	}
235
+
236
+	public function generateBeanConstructor()
237
+	{
238
+		$constructorProperties = $this->getConstructorProperties();
239
+
240
+		$constructorCode = '    /**
241 241
      * The constructor takes all compulsory arguments.
242 242
      *
243 243
 %s
@@ -247,110 +247,110 @@  discard block
 block discarded – undo
247 247
 %s%s    }
248 248
     ';
249 249
 
250
-        $paramAnnotations = [];
251
-        $arguments = [];
252
-        $assigns = [];
253
-        $parentConstructorArguments = [];
254
-
255
-        foreach ($constructorProperties as $property) {
256
-            $className = $property->getClassName();
257
-            if ($className) {
258
-                $arguments[] = $className.' '.$property->getVariableName();
259
-            } else {
260
-                $arguments[] = $property->getVariableName();
261
-            }
262
-            $paramAnnotations[] = $property->getParamAnnotation();
263
-            if ($property->getTable()->getName() === $this->table->getName()) {
264
-                $assigns[] = $property->getConstructorAssignCode()."\n";
265
-            } else {
266
-                $parentConstructorArguments[] = $property->getVariableName();
267
-            }
268
-        }
269
-
270
-        $parentConstructorCode = sprintf("        parent::__construct(%s);\n", implode(', ', $parentConstructorArguments));
271
-
272
-        foreach ($this->getPropertiesWithDefault() as $property) {
273
-            $assigns[] = $property->assignToDefaultCode()."\n";
274
-        }
275
-
276
-        return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode('', $assigns));
277
-    }
278
-
279
-    public function getDirectForeignKeysDescriptors()
280
-    {
281
-        $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName());
282
-
283
-        $descriptors = [];
284
-
285
-        foreach ($fks as $fk) {
286
-            $descriptors[] = new DirectForeignKeyMethodDescriptor($fk, $this->table);
287
-        }
288
-
289
-        return $descriptors;
290
-    }
291
-
292
-    private function getPivotTableDescriptors()
293
-    {
294
-        $descs = [];
295
-        foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) {
296
-            // There are exactly 2 FKs since this is a pivot table.
297
-            $fks = array_values($table->getForeignKeys());
298
-
299
-            if ($fks[0]->getForeignTableName() === $this->table->getName()) {
300
-                list($localFk, $remoteFk) = $fks;
301
-            } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) {
302
-                list($remoteFk, $localFk) = $fks;
303
-            } else {
304
-                continue;
305
-            }
306
-
307
-            $descs[] = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk);
308
-        }
309
-
310
-        return $descs;
311
-    }
312
-
313
-    /**
314
-     * Returns the list of method descriptors (and applies the alternative name if needed).
315
-     *
316
-     * @return MethodDescriptorInterface[]
317
-     */
318
-    private function getMethodDescriptors()
319
-    {
320
-        $directForeignKeyDescriptors = $this->getDirectForeignKeysDescriptors();
321
-        $pivotTableDescriptors = $this->getPivotTableDescriptors();
322
-
323
-        $descriptors = array_merge($directForeignKeyDescriptors, $pivotTableDescriptors);
324
-
325
-        // Descriptors by method names
326
-        $descriptorsByMethodName = [];
327
-
328
-        foreach ($descriptors as $descriptor) {
329
-            $descriptorsByMethodName[$descriptor->getName()][] = $descriptor;
330
-        }
331
-
332
-        foreach ($descriptorsByMethodName as $descriptorsForMethodName) {
333
-            if (count($descriptorsForMethodName) > 1) {
334
-                foreach ($descriptorsForMethodName as $descriptor) {
335
-                    $descriptor->useAlternativeName();
336
-                }
337
-            }
338
-        }
339
-
340
-        return $descriptors;
341
-    }
342
-
343
-    public function generateJsonSerialize()
344
-    {
345
-        $tableName = $this->table->getName();
346
-        $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName);
347
-        if ($parentFk !== null) {
348
-            $initializer = '$array = parent::jsonSerialize($stopRecursion);';
349
-        } else {
350
-            $initializer = '$array = [];';
351
-        }
352
-
353
-        $str = '
250
+		$paramAnnotations = [];
251
+		$arguments = [];
252
+		$assigns = [];
253
+		$parentConstructorArguments = [];
254
+
255
+		foreach ($constructorProperties as $property) {
256
+			$className = $property->getClassName();
257
+			if ($className) {
258
+				$arguments[] = $className.' '.$property->getVariableName();
259
+			} else {
260
+				$arguments[] = $property->getVariableName();
261
+			}
262
+			$paramAnnotations[] = $property->getParamAnnotation();
263
+			if ($property->getTable()->getName() === $this->table->getName()) {
264
+				$assigns[] = $property->getConstructorAssignCode()."\n";
265
+			} else {
266
+				$parentConstructorArguments[] = $property->getVariableName();
267
+			}
268
+		}
269
+
270
+		$parentConstructorCode = sprintf("        parent::__construct(%s);\n", implode(', ', $parentConstructorArguments));
271
+
272
+		foreach ($this->getPropertiesWithDefault() as $property) {
273
+			$assigns[] = $property->assignToDefaultCode()."\n";
274
+		}
275
+
276
+		return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode('', $assigns));
277
+	}
278
+
279
+	public function getDirectForeignKeysDescriptors()
280
+	{
281
+		$fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName());
282
+
283
+		$descriptors = [];
284
+
285
+		foreach ($fks as $fk) {
286
+			$descriptors[] = new DirectForeignKeyMethodDescriptor($fk, $this->table);
287
+		}
288
+
289
+		return $descriptors;
290
+	}
291
+
292
+	private function getPivotTableDescriptors()
293
+	{
294
+		$descs = [];
295
+		foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) {
296
+			// There are exactly 2 FKs since this is a pivot table.
297
+			$fks = array_values($table->getForeignKeys());
298
+
299
+			if ($fks[0]->getForeignTableName() === $this->table->getName()) {
300
+				list($localFk, $remoteFk) = $fks;
301
+			} elseif ($fks[1]->getForeignTableName() === $this->table->getName()) {
302
+				list($remoteFk, $localFk) = $fks;
303
+			} else {
304
+				continue;
305
+			}
306
+
307
+			$descs[] = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk);
308
+		}
309
+
310
+		return $descs;
311
+	}
312
+
313
+	/**
314
+	 * Returns the list of method descriptors (and applies the alternative name if needed).
315
+	 *
316
+	 * @return MethodDescriptorInterface[]
317
+	 */
318
+	private function getMethodDescriptors()
319
+	{
320
+		$directForeignKeyDescriptors = $this->getDirectForeignKeysDescriptors();
321
+		$pivotTableDescriptors = $this->getPivotTableDescriptors();
322
+
323
+		$descriptors = array_merge($directForeignKeyDescriptors, $pivotTableDescriptors);
324
+
325
+		// Descriptors by method names
326
+		$descriptorsByMethodName = [];
327
+
328
+		foreach ($descriptors as $descriptor) {
329
+			$descriptorsByMethodName[$descriptor->getName()][] = $descriptor;
330
+		}
331
+
332
+		foreach ($descriptorsByMethodName as $descriptorsForMethodName) {
333
+			if (count($descriptorsForMethodName) > 1) {
334
+				foreach ($descriptorsForMethodName as $descriptor) {
335
+					$descriptor->useAlternativeName();
336
+				}
337
+			}
338
+		}
339
+
340
+		return $descriptors;
341
+	}
342
+
343
+	public function generateJsonSerialize()
344
+	{
345
+		$tableName = $this->table->getName();
346
+		$parentFk = $this->schemaAnalyzer->getParentRelationship($tableName);
347
+		if ($parentFk !== null) {
348
+			$initializer = '$array = parent::jsonSerialize($stopRecursion);';
349
+		} else {
350
+			$initializer = '$array = [];';
351
+		}
352
+
353
+		$str = '
354 354
     /**
355 355
      * Serializes the object for JSON encoding.
356 356
      *
@@ -366,76 +366,76 @@  discard block
 block discarded – undo
366 366
     }
367 367
 ';
368 368
 
369
-        $propertiesCode = '';
370
-        foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) {
371
-            $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode();
372
-        }
373
-
374
-        // Many2many relationships
375
-        $methodsCode = '';
376
-        foreach ($this->getMethodDescriptors() as $methodDescriptor) {
377
-            $methodsCode .= $methodDescriptor->getJsonSerializeCode();
378
-        }
379
-
380
-        return sprintf($str, $initializer, $propertiesCode, $methodsCode);
381
-    }
382
-
383
-    /**
384
-     * Returns as an array the class we need to extend from and the list of use statements.
385
-     *
386
-     * @return array
387
-     */
388
-    private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null)
389
-    {
390
-        $classes = [];
391
-        if ($parentFk !== null) {
392
-            $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName());
393
-            $classes[] = $extends;
394
-        }
395
-
396
-        foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) {
397
-            $className = $beanPropertyDescriptor->getClassName();
398
-            if (null !== $className) {
399
-                $classes[] = $beanPropertyDescriptor->getClassName();
400
-            }
401
-        }
402
-
403
-        foreach ($this->getMethodDescriptors() as $descriptor) {
404
-            $classes = array_merge($classes, $descriptor->getUsedClasses());
405
-        }
406
-
407
-        $classes = array_unique($classes);
408
-
409
-        return $classes;
410
-    }
411
-
412
-    /**
413
-     * Writes the PHP bean file with all getters and setters from the table passed in parameter.
414
-     *
415
-     * @param string $beannamespace The namespace of the bean
416
-     */
417
-    public function generatePhpCode($beannamespace)
418
-    {
419
-        $tableName = $this->table->getName();
420
-        $baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($tableName);
421
-        $className = TDBMDaoGenerator::getBeanNameFromTableName($tableName);
422
-        $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName);
423
-
424
-        $classes = $this->generateExtendsAndUseStatements($parentFk);
425
-
426
-        $uses = array_map(function ($className) use ($beannamespace) {
427
-            return 'use '.$beannamespace.'\\'.$className.";\n";
428
-        }, $classes);
429
-        $use = implode('', $uses);
430
-
431
-        if ($parentFk !== null) {
432
-            $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName());
433
-        } else {
434
-            $extends = 'AbstractTDBMObject';
435
-            $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n";
436
-        }
437
-
438
-        $str = "<?php
369
+		$propertiesCode = '';
370
+		foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) {
371
+			$propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode();
372
+		}
373
+
374
+		// Many2many relationships
375
+		$methodsCode = '';
376
+		foreach ($this->getMethodDescriptors() as $methodDescriptor) {
377
+			$methodsCode .= $methodDescriptor->getJsonSerializeCode();
378
+		}
379
+
380
+		return sprintf($str, $initializer, $propertiesCode, $methodsCode);
381
+	}
382
+
383
+	/**
384
+	 * Returns as an array the class we need to extend from and the list of use statements.
385
+	 *
386
+	 * @return array
387
+	 */
388
+	private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null)
389
+	{
390
+		$classes = [];
391
+		if ($parentFk !== null) {
392
+			$extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName());
393
+			$classes[] = $extends;
394
+		}
395
+
396
+		foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) {
397
+			$className = $beanPropertyDescriptor->getClassName();
398
+			if (null !== $className) {
399
+				$classes[] = $beanPropertyDescriptor->getClassName();
400
+			}
401
+		}
402
+
403
+		foreach ($this->getMethodDescriptors() as $descriptor) {
404
+			$classes = array_merge($classes, $descriptor->getUsedClasses());
405
+		}
406
+
407
+		$classes = array_unique($classes);
408
+
409
+		return $classes;
410
+	}
411
+
412
+	/**
413
+	 * Writes the PHP bean file with all getters and setters from the table passed in parameter.
414
+	 *
415
+	 * @param string $beannamespace The namespace of the bean
416
+	 */
417
+	public function generatePhpCode($beannamespace)
418
+	{
419
+		$tableName = $this->table->getName();
420
+		$baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($tableName);
421
+		$className = TDBMDaoGenerator::getBeanNameFromTableName($tableName);
422
+		$parentFk = $this->schemaAnalyzer->getParentRelationship($tableName);
423
+
424
+		$classes = $this->generateExtendsAndUseStatements($parentFk);
425
+
426
+		$uses = array_map(function ($className) use ($beannamespace) {
427
+			return 'use '.$beannamespace.'\\'.$className.";\n";
428
+		}, $classes);
429
+		$use = implode('', $uses);
430
+
431
+		if ($parentFk !== null) {
432
+			$extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName());
433
+		} else {
434
+			$extends = 'AbstractTDBMObject';
435
+			$use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n";
436
+		}
437
+
438
+		$str = "<?php
439 439
 namespace {$beannamespace}\\Generated;
440 440
 
441 441
 use Mouf\\Database\\TDBM\\ResultIterator;
@@ -455,129 +455,129 @@  discard block
 block discarded – undo
455 455
 {
456 456
 ";
457 457
 
458
-        $str .= $this->generateBeanConstructor();
458
+		$str .= $this->generateBeanConstructor();
459 459
 
460
-        foreach ($this->getExposedProperties() as $property) {
461
-            $str .= $property->getGetterSetterCode();
462
-        }
460
+		foreach ($this->getExposedProperties() as $property) {
461
+			$str .= $property->getGetterSetterCode();
462
+		}
463 463
 
464
-        foreach ($this->getMethodDescriptors() as $methodDescriptor) {
465
-            $str .= $methodDescriptor->getCode();
466
-        }
467
-        $str .= $this->generateJsonSerialize();
464
+		foreach ($this->getMethodDescriptors() as $methodDescriptor) {
465
+			$str .= $methodDescriptor->getCode();
466
+		}
467
+		$str .= $this->generateJsonSerialize();
468 468
 
469
-        $str .= $this->generateGetUsedTablesCode();
469
+		$str .= $this->generateGetUsedTablesCode();
470 470
 
471
-        $str .= $this->generateOnDeleteCode();
471
+		$str .= $this->generateOnDeleteCode();
472 472
 
473
-        $str .= '}
473
+		$str .= '}
474 474
 ';
475 475
 
476
-        return $str;
477
-    }
478
-
479
-    /**
480
-     * @param string $beanNamespace
481
-     * @param string $beanClassName
482
-     *
483
-     * @return array first element: list of used beans, second item: PHP code as a string
484
-     */
485
-    public function generateFindByDaoCode($beanNamespace, $beanClassName)
486
-    {
487
-        $code = '';
488
-        $usedBeans = [];
489
-        foreach ($this->table->getIndexes() as $index) {
490
-            if (!$index->isPrimary()) {
491
-                list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName);
492
-                $code .= $codeForIndex;
493
-                $usedBeans = array_merge($usedBeans, $usedBeansForIndex);
494
-            }
495
-        }
496
-
497
-        return [$usedBeans, $code];
498
-    }
499
-
500
-    /**
501
-     * @param Index  $index
502
-     * @param string $beanNamespace
503
-     * @param string $beanClassName
504
-     *
505
-     * @return array first element: list of used beans, second item: PHP code as a string
506
-     */
507
-    private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName)
508
-    {
509
-        $columns = $index->getColumns();
510
-        $usedBeans = [];
511
-
512
-        /*
476
+		return $str;
477
+	}
478
+
479
+	/**
480
+	 * @param string $beanNamespace
481
+	 * @param string $beanClassName
482
+	 *
483
+	 * @return array first element: list of used beans, second item: PHP code as a string
484
+	 */
485
+	public function generateFindByDaoCode($beanNamespace, $beanClassName)
486
+	{
487
+		$code = '';
488
+		$usedBeans = [];
489
+		foreach ($this->table->getIndexes() as $index) {
490
+			if (!$index->isPrimary()) {
491
+				list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName);
492
+				$code .= $codeForIndex;
493
+				$usedBeans = array_merge($usedBeans, $usedBeansForIndex);
494
+			}
495
+		}
496
+
497
+		return [$usedBeans, $code];
498
+	}
499
+
500
+	/**
501
+	 * @param Index  $index
502
+	 * @param string $beanNamespace
503
+	 * @param string $beanClassName
504
+	 *
505
+	 * @return array first element: list of used beans, second item: PHP code as a string
506
+	 */
507
+	private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName)
508
+	{
509
+		$columns = $index->getColumns();
510
+		$usedBeans = [];
511
+
512
+		/*
513 513
          * The list of elements building this index (expressed as columns or foreign keys)
514 514
          * @var AbstractBeanPropertyDescriptor[]
515 515
          */
516
-        $elements = [];
517
-
518
-        foreach ($columns as $column) {
519
-            $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column));
520
-            if ($fk !== null) {
521
-                if (!in_array($fk, $elements)) {
522
-                    $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer);
523
-                }
524
-            } else {
525
-                $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column));
526
-            }
527
-        }
528
-
529
-        // If the index is actually only a foreign key, let's bypass it entirely.
530
-        if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) {
531
-            return [[], ''];
532
-        }
533
-
534
-        $methodNameComponent = [];
535
-        $functionParameters = [];
536
-        $first = true;
537
-        foreach ($elements as $element) {
538
-            $methodNameComponent[] = $element->getUpperCamelCaseName();
539
-            $functionParameter = $element->getClassName();
540
-            if ($functionParameter) {
541
-                $usedBeans[] = $beanNamespace.'\\'.$functionParameter;
542
-                $functionParameter .= ' ';
543
-            }
544
-            $functionParameter .= $element->getVariableName();
545
-            if ($first) {
546
-                $first = false;
547
-            } else {
548
-                $functionParameter .= ' = null';
549
-            }
550
-            $functionParameters[] = $functionParameter;
551
-        }
552
-        if ($index->isUnique()) {
553
-            $methodName = 'findOneBy'.implode('And', $methodNameComponent);
554
-            $calledMethod = 'findOne';
555
-            $returnType = "{$beanClassName}";
556
-        } else {
557
-            $methodName = 'findBy'.implode('And', $methodNameComponent);
558
-            $returnType = "{$beanClassName}[]|ResultIterator|ResultArray";
559
-            $calledMethod = 'find';
560
-        }
561
-        $functionParametersString = implode(', ', $functionParameters);
562
-
563
-        $count = 0;
564
-
565
-        $params = [];
566
-        $filterArrayCode = '';
567
-        $commentArguments = [];
568
-        foreach ($elements as $element) {
569
-            $params[] = $element->getParamAnnotation();
570
-            if ($element instanceof ScalarBeanPropertyDescriptor) {
571
-                $filterArrayCode .= '            '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n";
572
-            } else {
573
-                ++$count;
574
-                $filterArrayCode .= '            '.$count.' => '.$element->getVariableName().",\n";
575
-            }
576
-            $commentArguments[] = substr($element->getVariableName(), 1);
577
-        }
578
-        $paramsString = implode("\n", $params);
579
-
580
-        $code = "
516
+		$elements = [];
517
+
518
+		foreach ($columns as $column) {
519
+			$fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column));
520
+			if ($fk !== null) {
521
+				if (!in_array($fk, $elements)) {
522
+					$elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer);
523
+				}
524
+			} else {
525
+				$elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column));
526
+			}
527
+		}
528
+
529
+		// If the index is actually only a foreign key, let's bypass it entirely.
530
+		if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) {
531
+			return [[], ''];
532
+		}
533
+
534
+		$methodNameComponent = [];
535
+		$functionParameters = [];
536
+		$first = true;
537
+		foreach ($elements as $element) {
538
+			$methodNameComponent[] = $element->getUpperCamelCaseName();
539
+			$functionParameter = $element->getClassName();
540
+			if ($functionParameter) {
541
+				$usedBeans[] = $beanNamespace.'\\'.$functionParameter;
542
+				$functionParameter .= ' ';
543
+			}
544
+			$functionParameter .= $element->getVariableName();
545
+			if ($first) {
546
+				$first = false;
547
+			} else {
548
+				$functionParameter .= ' = null';
549
+			}
550
+			$functionParameters[] = $functionParameter;
551
+		}
552
+		if ($index->isUnique()) {
553
+			$methodName = 'findOneBy'.implode('And', $methodNameComponent);
554
+			$calledMethod = 'findOne';
555
+			$returnType = "{$beanClassName}";
556
+		} else {
557
+			$methodName = 'findBy'.implode('And', $methodNameComponent);
558
+			$returnType = "{$beanClassName}[]|ResultIterator|ResultArray";
559
+			$calledMethod = 'find';
560
+		}
561
+		$functionParametersString = implode(', ', $functionParameters);
562
+
563
+		$count = 0;
564
+
565
+		$params = [];
566
+		$filterArrayCode = '';
567
+		$commentArguments = [];
568
+		foreach ($elements as $element) {
569
+			$params[] = $element->getParamAnnotation();
570
+			if ($element instanceof ScalarBeanPropertyDescriptor) {
571
+				$filterArrayCode .= '            '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n";
572
+			} else {
573
+				++$count;
574
+				$filterArrayCode .= '            '.$count.' => '.$element->getVariableName().",\n";
575
+			}
576
+			$commentArguments[] = substr($element->getVariableName(), 1);
577
+		}
578
+		$paramsString = implode("\n", $params);
579
+
580
+		$code = "
581 581
     /**
582 582
      * Get a list of $beanClassName filtered by ".implode(', ', $commentArguments).".
583 583
      *
@@ -595,27 +595,27 @@  discard block
 block discarded – undo
595 595
     }
596 596
 ";
597 597
 
598
-        return [$usedBeans, $code];
599
-    }
600
-
601
-    /**
602
-     * Generates the code for the getUsedTable protected method.
603
-     *
604
-     * @return string
605
-     */
606
-    private function generateGetUsedTablesCode()
607
-    {
608
-        $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null;
609
-        if ($hasParentRelationship) {
610
-            $code = sprintf('        $tables = parent::getUsedTables();
598
+		return [$usedBeans, $code];
599
+	}
600
+
601
+	/**
602
+	 * Generates the code for the getUsedTable protected method.
603
+	 *
604
+	 * @return string
605
+	 */
606
+	private function generateGetUsedTablesCode()
607
+	{
608
+		$hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null;
609
+		if ($hasParentRelationship) {
610
+			$code = sprintf('        $tables = parent::getUsedTables();
611 611
         $tables[] = %s;
612 612
 
613 613
         return $tables;', var_export($this->table->getName(), true));
614
-        } else {
615
-            $code = sprintf('        return [ %s ];', var_export($this->table->getName(), true));
616
-        }
614
+		} else {
615
+			$code = sprintf('        return [ %s ];', var_export($this->table->getName(), true));
616
+		}
617 617
 
618
-        return sprintf('
618
+		return sprintf('
619 619
     /**
620 620
      * Returns an array of used tables by this bean (from parent to child relationship).
621 621
      *
@@ -626,20 +626,20 @@  discard block
 block discarded – undo
626 626
 %s
627 627
     }
628 628
 ', $code);
629
-    }
630
-
631
-    private function generateOnDeleteCode()
632
-    {
633
-        $code = '';
634
-        $relationships = $this->getPropertiesForTable($this->table);
635
-        foreach ($relationships as $relationship) {
636
-            if ($relationship instanceof ObjectBeanPropertyDescriptor) {
637
-                $code .= sprintf('        $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).");\n");
638
-            }
639
-        }
640
-
641
-        if ($code) {
642
-            return sprintf('
629
+	}
630
+
631
+	private function generateOnDeleteCode()
632
+	{
633
+		$code = '';
634
+		$relationships = $this->getPropertiesForTable($this->table);
635
+		foreach ($relationships as $relationship) {
636
+			if ($relationship instanceof ObjectBeanPropertyDescriptor) {
637
+				$code .= sprintf('        $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).");\n");
638
+			}
639
+		}
640
+
641
+		if ($code) {
642
+			return sprintf('
643 643
     /**
644 644
      * Method called when the bean is removed from database.
645 645
      *
@@ -649,8 +649,8 @@  discard block
 block discarded – undo
649 649
         parent::onDelete();
650 650
 %s    }
651 651
 ', $code);
652
-        }
652
+		}
653 653
 
654
-        return '';
655
-    }
654
+		return '';
655
+	}
656 656
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Utils/MethodDescriptorInterface.php 1 patch
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -4,36 +4,36 @@
 block discarded – undo
4 4
 
5 5
 interface MethodDescriptorInterface
6 6
 {
7
-    /**
8
-     * Returns the name of the method to be generated.
9
-     *
10
-     * @return string
11
-     */
12
-    public function getName() : string;
7
+	/**
8
+	 * Returns the name of the method to be generated.
9
+	 *
10
+	 * @return string
11
+	 */
12
+	public function getName() : string;
13 13
 
14
-    /**
15
-     * Requests the use of an alternative name for this method.
16
-     */
17
-    public function useAlternativeName();
14
+	/**
15
+	 * Requests the use of an alternative name for this method.
16
+	 */
17
+	public function useAlternativeName();
18 18
 
19
-    /**
20
-     * Returns the code of the method.
21
-     *
22
-     * @return string
23
-     */
24
-    public function getCode() : string;
19
+	/**
20
+	 * Returns the code of the method.
21
+	 *
22
+	 * @return string
23
+	 */
24
+	public function getCode() : string;
25 25
 
26
-    /**
27
-     * Returns an array of classes that needs a "use" for this method.
28
-     *
29
-     * @return string[]
30
-     */
31
-    public function getUsedClasses() : array;
26
+	/**
27
+	 * Returns an array of classes that needs a "use" for this method.
28
+	 *
29
+	 * @return string[]
30
+	 */
31
+	public function getUsedClasses() : array;
32 32
 
33
-    /**
34
-     * Returns the code to past in jsonSerialize.
35
-     *
36
-     * @return string
37
-     */
38
-    public function getJsonSerializeCode() : string;
33
+	/**
34
+	 * Returns the code to past in jsonSerialize.
35
+	 *
36
+	 * @return string
37
+	 */
38
+	public function getJsonSerializeCode() : string;
39 39
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Utils/DirectForeignKeyMethodDescriptor.php 2 patches
Indentation   +110 added lines, -110 removed lines patch added patch discarded remove patch
@@ -12,65 +12,65 @@  discard block
 block discarded – undo
12 12
  */
13 13
 class DirectForeignKeyMethodDescriptor implements MethodDescriptorInterface
14 14
 {
15
-    /**
16
-     * @var ForeignKeyConstraint
17
-     */
18
-    private $fk;
19
-
20
-    private $useAlternateName = false;
21
-    /**
22
-     * @var Table
23
-     */
24
-    private $mainTable;
25
-
26
-    /**
27
-     * @param ForeignKeyConstraint $fk        The foreign key pointing to our bean
28
-     * @param Table                $mainTable The main table that is pointed to
29
-     */
30
-    public function __construct(ForeignKeyConstraint $fk, Table $mainTable)
31
-    {
32
-        $this->fk = $fk;
33
-        $this->mainTable = $mainTable;
34
-    }
35
-
36
-    /**
37
-     * Returns the name of the method to be generated.
38
-     *
39
-     * @return string
40
-     */
41
-    public function getName() : string
42
-    {
43
-        if (!$this->useAlternateName) {
44
-            return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName());
45
-        } else {
46
-            $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By';
47
-
48
-            $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns());
49
-
50
-            $methodName .= implode('And', $camelizedColumns);
51
-
52
-            return $methodName;
53
-        }
54
-    }
55
-
56
-    /**
57
-     * Requests the use of an alternative name for this method.
58
-     */
59
-    public function useAlternativeName()
60
-    {
61
-        $this->useAlternateName = true;
62
-    }
63
-
64
-    /**
65
-     * Returns the code of the method.
66
-     *
67
-     * @return string
68
-     */
69
-    public function getCode() : string
70
-    {
71
-        $code = '';
72
-
73
-        $getterCode = '    /**
15
+	/**
16
+	 * @var ForeignKeyConstraint
17
+	 */
18
+	private $fk;
19
+
20
+	private $useAlternateName = false;
21
+	/**
22
+	 * @var Table
23
+	 */
24
+	private $mainTable;
25
+
26
+	/**
27
+	 * @param ForeignKeyConstraint $fk        The foreign key pointing to our bean
28
+	 * @param Table                $mainTable The main table that is pointed to
29
+	 */
30
+	public function __construct(ForeignKeyConstraint $fk, Table $mainTable)
31
+	{
32
+		$this->fk = $fk;
33
+		$this->mainTable = $mainTable;
34
+	}
35
+
36
+	/**
37
+	 * Returns the name of the method to be generated.
38
+	 *
39
+	 * @return string
40
+	 */
41
+	public function getName() : string
42
+	{
43
+		if (!$this->useAlternateName) {
44
+			return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName());
45
+		} else {
46
+			$methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By';
47
+
48
+			$camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns());
49
+
50
+			$methodName .= implode('And', $camelizedColumns);
51
+
52
+			return $methodName;
53
+		}
54
+	}
55
+
56
+	/**
57
+	 * Requests the use of an alternative name for this method.
58
+	 */
59
+	public function useAlternativeName()
60
+	{
61
+		$this->useAlternateName = true;
62
+	}
63
+
64
+	/**
65
+	 * Returns the code of the method.
66
+	 *
67
+	 * @return string
68
+	 */
69
+	public function getCode() : string
70
+	{
71
+		$code = '';
72
+
73
+		$getterCode = '    /**
74 74
      * Returns the list of %s pointing to this bean via the %s column.
75 75
      *
76 76
      * @return %s[]|AlterableResultIterator
@@ -82,55 +82,55 @@  discard block
 block discarded – undo
82 82
 
83 83
 ';
84 84
 
85
-        $beanClass = TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getLocalTableName());
86
-        $code .= sprintf($getterCode,
87
-            $beanClass,
88
-            implode(', ', $this->fk->getColumns()),
89
-            $beanClass,
90
-            $this->getName(),
91
-            var_export($this->fk->getLocalTableName(), true),
92
-            var_export($this->fk->getName(), true),
93
-            var_export($this->fk->getLocalTableName(), true),
94
-            $this->getFilters($this->fk)
95
-        );
96
-
97
-        return $code;
98
-    }
99
-
100
-    private function getFilters(ForeignKeyConstraint $fk) : string
101
-    {
102
-        $counter = 0;
103
-        $parameters = [];
104
-
105
-        $pkColumns = $this->mainTable->getPrimaryKeyColumns();
106
-
107
-        foreach ($fk->getLocalColumns() as $columnName) {
108
-            $pkColumn = $pkColumns[$counter];
109
-            $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true));
110
-            ++$counter;
111
-        }
112
-        $parametersCode = '['.implode(', ', $parameters).']';
113
-
114
-        return $parametersCode;
115
-    }
116
-
117
-    /**
118
-     * Returns an array of classes that needs a "use" for this method.
119
-     *
120
-     * @return string[]
121
-     */
122
-    public function getUsedClasses() : array
123
-    {
124
-        return [TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getForeignTableName())];
125
-    }
126
-
127
-    /**
128
-     * Returns the code to past in jsonSerialize.
129
-     *
130
-     * @return string
131
-     */
132
-    public function getJsonSerializeCode() : string
133
-    {
134
-        return '';
135
-    }
85
+		$beanClass = TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getLocalTableName());
86
+		$code .= sprintf($getterCode,
87
+			$beanClass,
88
+			implode(', ', $this->fk->getColumns()),
89
+			$beanClass,
90
+			$this->getName(),
91
+			var_export($this->fk->getLocalTableName(), true),
92
+			var_export($this->fk->getName(), true),
93
+			var_export($this->fk->getLocalTableName(), true),
94
+			$this->getFilters($this->fk)
95
+		);
96
+
97
+		return $code;
98
+	}
99
+
100
+	private function getFilters(ForeignKeyConstraint $fk) : string
101
+	{
102
+		$counter = 0;
103
+		$parameters = [];
104
+
105
+		$pkColumns = $this->mainTable->getPrimaryKeyColumns();
106
+
107
+		foreach ($fk->getLocalColumns() as $columnName) {
108
+			$pkColumn = $pkColumns[$counter];
109
+			$parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true));
110
+			++$counter;
111
+		}
112
+		$parametersCode = '['.implode(', ', $parameters).']';
113
+
114
+		return $parametersCode;
115
+	}
116
+
117
+	/**
118
+	 * Returns an array of classes that needs a "use" for this method.
119
+	 *
120
+	 * @return string[]
121
+	 */
122
+	public function getUsedClasses() : array
123
+	{
124
+		return [TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getForeignTableName())];
125
+	}
126
+
127
+	/**
128
+	 * Returns the code to past in jsonSerialize.
129
+	 *
130
+	 * @return string
131
+	 */
132
+	public function getJsonSerializeCode() : string
133
+	{
134
+		return '';
135
+	}
136 136
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types=1);
3
+declare(strict_types = 1);
4 4
 
5 5
 namespace Mouf\Database\TDBM\Utils;
6 6
 
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Utils/PivotTableMethodsDescriptor.php 1 patch
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -7,99 +7,99 @@  discard block
 block discarded – undo
7 7
 
8 8
 class PivotTableMethodsDescriptor implements MethodDescriptorInterface
9 9
 {
10
-    /**
11
-     * @var Table
12
-     */
13
-    private $pivotTable;
14
-
15
-    private $useAlternateName = false;
16
-
17
-    /**
18
-     * @var ForeignKeyConstraint
19
-     */
20
-    private $localFk;
21
-
22
-    /**
23
-     * @var ForeignKeyConstraint
24
-     */
25
-    private $remoteFk;
26
-
27
-    /**
28
-     * @param Table                $pivotTable The pivot table
29
-     * @param ForeignKeyConstraint $localFk
30
-     * @param ForeignKeyConstraint $remoteFk
31
-     */
32
-    public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk)
33
-    {
34
-        $this->pivotTable = $pivotTable;
35
-        $this->localFk = $localFk;
36
-        $this->remoteFk = $remoteFk;
37
-    }
38
-
39
-    /**
40
-     * Requests the use of an alternative name for this method.
41
-     */
42
-    public function useAlternativeName()
43
-    {
44
-        $this->useAlternateName = true;
45
-    }
46
-
47
-    /**
48
-     * Returns the name of the method to be generated.
49
-     *
50
-     * @return string
51
-     */
52
-    public function getName() : string
53
-    {
54
-        if (!$this->useAlternateName) {
55
-            return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
56
-        } else {
57
-            return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
58
-        }
59
-    }
60
-
61
-    /**
62
-     * Returns the plural name.
63
-     *
64
-     * @return string
65
-     */
66
-    private function getPluralName() : string
67
-    {
68
-        if (!$this->useAlternateName) {
69
-            return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
70
-        } else {
71
-            return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
72
-        }
73
-    }
74
-
75
-    /**
76
-     * Returns the singular name.
77
-     *
78
-     * @return string
79
-     */
80
-    private function getSingularName() : string
81
-    {
82
-        if (!$this->useAlternateName) {
83
-            return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName()));
84
-        } else {
85
-            return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
86
-        }
87
-    }
88
-
89
-    /**
90
-     * Returns the code of the method.
91
-     *
92
-     * @return string
93
-     */
94
-    public function getCode() : string
95
-    {
96
-        $singularName = $this->getSingularName();
97
-        $pluralName = $this->getPluralName();
98
-        $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName());
99
-        $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
100
-        $pluralVariableName = $variableName.'s';
101
-
102
-        $str = '    /**
10
+	/**
11
+	 * @var Table
12
+	 */
13
+	private $pivotTable;
14
+
15
+	private $useAlternateName = false;
16
+
17
+	/**
18
+	 * @var ForeignKeyConstraint
19
+	 */
20
+	private $localFk;
21
+
22
+	/**
23
+	 * @var ForeignKeyConstraint
24
+	 */
25
+	private $remoteFk;
26
+
27
+	/**
28
+	 * @param Table                $pivotTable The pivot table
29
+	 * @param ForeignKeyConstraint $localFk
30
+	 * @param ForeignKeyConstraint $remoteFk
31
+	 */
32
+	public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk)
33
+	{
34
+		$this->pivotTable = $pivotTable;
35
+		$this->localFk = $localFk;
36
+		$this->remoteFk = $remoteFk;
37
+	}
38
+
39
+	/**
40
+	 * Requests the use of an alternative name for this method.
41
+	 */
42
+	public function useAlternativeName()
43
+	{
44
+		$this->useAlternateName = true;
45
+	}
46
+
47
+	/**
48
+	 * Returns the name of the method to be generated.
49
+	 *
50
+	 * @return string
51
+	 */
52
+	public function getName() : string
53
+	{
54
+		if (!$this->useAlternateName) {
55
+			return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
56
+		} else {
57
+			return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
58
+		}
59
+	}
60
+
61
+	/**
62
+	 * Returns the plural name.
63
+	 *
64
+	 * @return string
65
+	 */
66
+	private function getPluralName() : string
67
+	{
68
+		if (!$this->useAlternateName) {
69
+			return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
70
+		} else {
71
+			return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
72
+		}
73
+	}
74
+
75
+	/**
76
+	 * Returns the singular name.
77
+	 *
78
+	 * @return string
79
+	 */
80
+	private function getSingularName() : string
81
+	{
82
+		if (!$this->useAlternateName) {
83
+			return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName()));
84
+		} else {
85
+			return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
86
+		}
87
+	}
88
+
89
+	/**
90
+	 * Returns the code of the method.
91
+	 *
92
+	 * @return string
93
+	 */
94
+	public function getCode() : string
95
+	{
96
+		$singularName = $this->getSingularName();
97
+		$pluralName = $this->getPluralName();
98
+		$remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName());
99
+		$variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
100
+		$pluralVariableName = $variableName.'s';
101
+
102
+		$str = '    /**
103 103
      * Returns the list of %s associated to this bean via the %s pivot table.
104 104
      *
105 105
      * @return %s[]
@@ -110,9 +110,9 @@  discard block
 block discarded – undo
110 110
     }
111 111
 ';
112 112
 
113
-        $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true));
113
+		$getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true));
114 114
 
115
-        $str = '    /**
115
+		$str = '    /**
116 116
      * Adds a relationship with %s associated to this bean via the %s pivot table.
117 117
      *
118 118
      * @param %s %s
@@ -123,9 +123,9 @@  discard block
 block discarded – undo
123 123
     }
124 124
 ';
125 125
 
126
-        $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
126
+		$adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
127 127
 
128
-        $str = '    /**
128
+		$str = '    /**
129 129
      * Deletes the relationship with %s associated to this bean via the %s pivot table.
130 130
      *
131 131
      * @param %s %s
@@ -136,9 +136,9 @@  discard block
 block discarded – undo
136 136
     }
137 137
 ';
138 138
 
139
-        $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
139
+		$removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
140 140
 
141
-        $str = '    /**
141
+		$str = '    /**
142 142
      * Returns whether this bean is associated with %s via the %s pivot table.
143 143
      *
144 144
      * @param %s %s
@@ -150,9 +150,9 @@  discard block
 block discarded – undo
150 150
     }
151 151
 ';
152 152
 
153
-        $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
153
+		$hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
154 154
 
155
-        $str = '    /**
155
+		$str = '    /**
156 156
      * Sets all relationships with %s associated to this bean via the %s pivot table.
157 157
      * Exiting relationships will be removed and replaced by the provided relationships.
158 158
      *
@@ -164,38 +164,38 @@  discard block
 block discarded – undo
164 164
     }
165 165
 ';
166 166
 
167
-        $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName);
168
-
169
-        $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode;
170
-
171
-        return $code;
172
-    }
173
-
174
-    /**
175
-     * Returns an array of classes that needs a "use" for this method.
176
-     *
177
-     * @return string[]
178
-     */
179
-    public function getUsedClasses() : array
180
-    {
181
-        return [TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName())];
182
-    }
183
-
184
-    /**
185
-     * Returns the code to past in jsonSerialize.
186
-     *
187
-     * @return string
188
-     */
189
-    public function getJsonSerializeCode() : string
190
-    {
191
-        $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName());
192
-        $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
193
-
194
-        return '        if (!$stopRecursion) {
167
+		$setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName);
168
+
169
+		$code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode;
170
+
171
+		return $code;
172
+	}
173
+
174
+	/**
175
+	 * Returns an array of classes that needs a "use" for this method.
176
+	 *
177
+	 * @return string[]
178
+	 */
179
+	public function getUsedClasses() : array
180
+	{
181
+		return [TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName())];
182
+	}
183
+
184
+	/**
185
+	 * Returns the code to past in jsonSerialize.
186
+	 *
187
+	 * @return string
188
+	 */
189
+	public function getJsonSerializeCode() : string
190
+	{
191
+		$remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName());
192
+		$variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
193
+
194
+		return '        if (!$stopRecursion) {
195 195
             $array[\''.lcfirst($this->getPluralName()).'\'] = array_map(function ('.$remoteBeanName.' '.$variableName.') {
196 196
                 return '.$variableName.'->jsonSerialize(true);
197 197
             }, $this->'.$this->getName().'());
198 198
         }
199 199
 ';
200
-    }
200
+	}
201 201
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/TDBMInheritanceException.php 1 patch
Indentation   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -7,23 +7,23 @@
 block discarded – undo
7 7
  */
8 8
 class TDBMInheritanceException extends TDBMException
9 9
 {
10
-    public static function create(array $tables) : TDBMInheritanceException
11
-    {
12
-        return new self(sprintf('The tables (%s) cannot be linked by an inheritance relationship. Does your data set contains multiple children for one parent row? (multiple inheritance is not supported by TDBM)', implode(', ', $tables)));
13
-    }
10
+	public static function create(array $tables) : TDBMInheritanceException
11
+	{
12
+		return new self(sprintf('The tables (%s) cannot be linked by an inheritance relationship. Does your data set contains multiple children for one parent row? (multiple inheritance is not supported by TDBM)', implode(', ', $tables)));
13
+	}
14 14
 
15
-    public static function extendException(TDBMInheritanceException $e, TDBMService $tdbmService, array $beanData) : TDBMInheritanceException
16
-    {
17
-        $pks = [];
18
-        foreach ($beanData as $table => $row) {
19
-            $primaryKeyColumns = $tdbmService->getPrimaryKeyColumns($table);
20
-            foreach ($primaryKeyColumns as $columnName) {
21
-                if ($row[$columnName] !== null) {
22
-                    $pks[] = $table.'.'.$columnName.' => '.var_export($row[$columnName], true);
23
-                }
24
-            }
25
-        }
15
+	public static function extendException(TDBMInheritanceException $e, TDBMService $tdbmService, array $beanData) : TDBMInheritanceException
16
+	{
17
+		$pks = [];
18
+		foreach ($beanData as $table => $row) {
19
+			$primaryKeyColumns = $tdbmService->getPrimaryKeyColumns($table);
20
+			foreach ($primaryKeyColumns as $columnName) {
21
+				if ($row[$columnName] !== null) {
22
+					$pks[] = $table.'.'.$columnName.' => '.var_export($row[$columnName], true);
23
+				}
24
+			}
25
+		}
26 26
 
27
-        throw new self($e->getMessage().' (row in error: '.implode(', ', $pks).')', 0, $e);
28
-    }
27
+		throw new self($e->getMessage().' (row in error: '.implode(', ', $pks).')', 0, $e);
28
+	}
29 29
 }
Please login to merge, or discard this patch.