@@ -8,7 +8,7 @@ |
||
8 | 8 | <input type="hidden" id="selfedit" name="selfedit" value="<?php echo plainstring_to_htmlprotected($this->selfedit) ?>" /> |
9 | 9 | |
10 | 10 | <?php if (!$this->autoloadDetected) { |
11 | - ?> |
|
11 | + ?> |
|
12 | 12 | <div class="alert">Warning! TDBM could not detect the autoload section of your composer.json file. |
13 | 13 | Unless you are developing your own autoload system, you should configure <strong>composer.json</strong> to <a href="http://getcomposer.org/doc/01-basic-usage.md#autoloading" target="_blank">define a source directory and a root namespace using PSR-0</a>.</div> |
14 | 14 | <?php |
@@ -31,117 +31,117 @@ |
||
31 | 31 | */ |
32 | 32 | class WeakrefObjectStorage |
33 | 33 | { |
34 | - /** |
|
35 | - * An array of fetched object, accessible via table name and primary key. |
|
36 | - * If the primary key is split on several columns, access is done by an array of columns, serialized. |
|
37 | - * |
|
38 | - * @var array<string, WeakMap<string, TDBMObject>> |
|
39 | - */ |
|
40 | - private $objects = array(); |
|
34 | + /** |
|
35 | + * An array of fetched object, accessible via table name and primary key. |
|
36 | + * If the primary key is split on several columns, access is done by an array of columns, serialized. |
|
37 | + * |
|
38 | + * @var array<string, WeakMap<string, TDBMObject>> |
|
39 | + */ |
|
40 | + private $objects = array(); |
|
41 | 41 | |
42 | - /** |
|
43 | - * Every 10000 set in the dataset, we perform a cleanup to ensure the WeakRef instances |
|
44 | - * are removed if they are no more valid. |
|
45 | - * This is to avoid having memory used by dangling WeakRef instances. |
|
46 | - * |
|
47 | - * @var int |
|
48 | - */ |
|
49 | - private $garbageCollectorCount = 0; |
|
42 | + /** |
|
43 | + * Every 10000 set in the dataset, we perform a cleanup to ensure the WeakRef instances |
|
44 | + * are removed if they are no more valid. |
|
45 | + * This is to avoid having memory used by dangling WeakRef instances. |
|
46 | + * |
|
47 | + * @var int |
|
48 | + */ |
|
49 | + private $garbageCollectorCount = 0; |
|
50 | 50 | |
51 | - /** |
|
52 | - * Sets an object in the storage. |
|
53 | - * |
|
54 | - * @param string $tableName |
|
55 | - * @param string $id |
|
56 | - * @param DbRow $dbRow |
|
57 | - */ |
|
58 | - public function set($tableName, $id, DbRow $dbRow) |
|
59 | - { |
|
60 | - $this->objects[$tableName][$id] = new \WeakRef($dbRow); |
|
61 | - ++$this->garbageCollectorCount; |
|
62 | - if ($this->garbageCollectorCount == 10000) { |
|
63 | - $this->garbageCollectorCount = 0; |
|
64 | - $this->cleanupDanglingWeakRefs(); |
|
65 | - } |
|
66 | - } |
|
51 | + /** |
|
52 | + * Sets an object in the storage. |
|
53 | + * |
|
54 | + * @param string $tableName |
|
55 | + * @param string $id |
|
56 | + * @param DbRow $dbRow |
|
57 | + */ |
|
58 | + public function set($tableName, $id, DbRow $dbRow) |
|
59 | + { |
|
60 | + $this->objects[$tableName][$id] = new \WeakRef($dbRow); |
|
61 | + ++$this->garbageCollectorCount; |
|
62 | + if ($this->garbageCollectorCount == 10000) { |
|
63 | + $this->garbageCollectorCount = 0; |
|
64 | + $this->cleanupDanglingWeakRefs(); |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Checks if an object is in the storage. |
|
70 | - * |
|
71 | - * @param string $tableName |
|
72 | - * @param string $id |
|
73 | - * |
|
74 | - * @return bool |
|
75 | - */ |
|
76 | - public function has($tableName, $id) |
|
77 | - { |
|
78 | - if (isset($this->objects[$tableName][$id])) { |
|
79 | - if ($this->objects[$tableName][$id]->valid()) { |
|
80 | - return true; |
|
81 | - } else { |
|
82 | - unset($this->objects[$tableName][$id]); |
|
83 | - } |
|
84 | - } |
|
68 | + /** |
|
69 | + * Checks if an object is in the storage. |
|
70 | + * |
|
71 | + * @param string $tableName |
|
72 | + * @param string $id |
|
73 | + * |
|
74 | + * @return bool |
|
75 | + */ |
|
76 | + public function has($tableName, $id) |
|
77 | + { |
|
78 | + if (isset($this->objects[$tableName][$id])) { |
|
79 | + if ($this->objects[$tableName][$id]->valid()) { |
|
80 | + return true; |
|
81 | + } else { |
|
82 | + unset($this->objects[$tableName][$id]); |
|
83 | + } |
|
84 | + } |
|
85 | 85 | |
86 | - return false; |
|
87 | - } |
|
86 | + return false; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Returns an object from the storage (or null if no object is set). |
|
91 | - * |
|
92 | - * @param string $tableName |
|
93 | - * @param string $id |
|
94 | - * |
|
95 | - * @return DbRow |
|
96 | - */ |
|
97 | - public function get($tableName, $id) |
|
98 | - { |
|
99 | - if (isset($this->objects[$tableName][$id])) { |
|
100 | - if ($this->objects[$tableName][$id]->valid()) { |
|
101 | - return $this->objects[$tableName][$id]->get(); |
|
102 | - } |
|
103 | - } else { |
|
104 | - return; |
|
105 | - } |
|
106 | - } |
|
89 | + /** |
|
90 | + * Returns an object from the storage (or null if no object is set). |
|
91 | + * |
|
92 | + * @param string $tableName |
|
93 | + * @param string $id |
|
94 | + * |
|
95 | + * @return DbRow |
|
96 | + */ |
|
97 | + public function get($tableName, $id) |
|
98 | + { |
|
99 | + if (isset($this->objects[$tableName][$id])) { |
|
100 | + if ($this->objects[$tableName][$id]->valid()) { |
|
101 | + return $this->objects[$tableName][$id]->get(); |
|
102 | + } |
|
103 | + } else { |
|
104 | + return; |
|
105 | + } |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Removes an object from the storage. |
|
110 | - * |
|
111 | - * @param string $tableName |
|
112 | - * @param string $id |
|
113 | - */ |
|
114 | - public function remove($tableName, $id) |
|
115 | - { |
|
116 | - unset($this->objects[$tableName][$id]); |
|
117 | - } |
|
108 | + /** |
|
109 | + * Removes an object from the storage. |
|
110 | + * |
|
111 | + * @param string $tableName |
|
112 | + * @param string $id |
|
113 | + */ |
|
114 | + public function remove($tableName, $id) |
|
115 | + { |
|
116 | + unset($this->objects[$tableName][$id]); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Applies the callback to all objects. |
|
121 | - * |
|
122 | - * @param callable $callback |
|
123 | - */ |
|
124 | - public function apply(callable $callback) |
|
125 | - { |
|
126 | - foreach ($this->objects as $tableName => $table) { |
|
127 | - foreach ($table as $id => $obj) { |
|
128 | - if ($obj->valid()) { |
|
129 | - $callback($obj->get(), $tableName, $id); |
|
130 | - } else { |
|
131 | - unset($this->objects[$tableName][$id]); |
|
132 | - } |
|
133 | - } |
|
134 | - } |
|
135 | - } |
|
119 | + /** |
|
120 | + * Applies the callback to all objects. |
|
121 | + * |
|
122 | + * @param callable $callback |
|
123 | + */ |
|
124 | + public function apply(callable $callback) |
|
125 | + { |
|
126 | + foreach ($this->objects as $tableName => $table) { |
|
127 | + foreach ($table as $id => $obj) { |
|
128 | + if ($obj->valid()) { |
|
129 | + $callback($obj->get(), $tableName, $id); |
|
130 | + } else { |
|
131 | + unset($this->objects[$tableName][$id]); |
|
132 | + } |
|
133 | + } |
|
134 | + } |
|
135 | + } |
|
136 | 136 | |
137 | - private function cleanupDanglingWeakRefs() |
|
138 | - { |
|
139 | - foreach ($this->objects as $tableName => $table) { |
|
140 | - foreach ($table as $id => $obj) { |
|
141 | - if (!$obj->valid()) { |
|
142 | - unset($this->objects[$tableName][$id]); |
|
143 | - } |
|
144 | - } |
|
145 | - } |
|
146 | - } |
|
137 | + private function cleanupDanglingWeakRefs() |
|
138 | + { |
|
139 | + foreach ($this->objects as $tableName => $table) { |
|
140 | + foreach ($table as $id => $obj) { |
|
141 | + if (!$obj->valid()) { |
|
142 | + unset($this->objects[$tableName][$id]); |
|
143 | + } |
|
144 | + } |
|
145 | + } |
|
146 | + } |
|
147 | 147 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | { |
66 | 66 | // First, are there many column or only one? |
67 | 67 | // If one column, we name the setter after it. Otherwise, we name the setter after the table name |
68 | - if (count($this->foreignKey->getLocalColumns()) > 1) { |
|
68 | + if (count($this->foreignKey->getLocalColumns())>1) { |
|
69 | 69 | $name = TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($this->foreignKey->getForeignTableName())); |
70 | 70 | if ($this->alternativeName) { |
71 | 71 | $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $this->foreignKey->getLocalColumns()); |
@@ -78,8 +78,8 @@ discard block |
||
78 | 78 | if (strpos(strtolower($column), 'id_') === 0) { |
79 | 79 | $column = substr($column, 3); |
80 | 80 | } |
81 | - if (strrpos(strtolower($column), '_id') === strlen($column) - 3) { |
|
82 | - $column = substr($column, 0, strlen($column) - 3); |
|
81 | + if (strrpos(strtolower($column), '_id') === strlen($column)-3) { |
|
82 | + $column = substr($column, 0, strlen($column)-3); |
|
83 | 83 | } |
84 | 84 | $name = TDBMDaoGenerator::toCamelCase($column); |
85 | 85 | if ($this->alternativeName) { |
@@ -12,156 +12,156 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class ObjectBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
14 | 14 | { |
15 | - /** |
|
16 | - * @var ForeignKeyConstraint |
|
17 | - */ |
|
18 | - private $foreignKey; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var SchemaAnalyzer |
|
22 | - */ |
|
23 | - private $schemaAnalyzer; |
|
24 | - |
|
25 | - public function __construct(Table $table, ForeignKeyConstraint $foreignKey, SchemaAnalyzer $schemaAnalyzer) |
|
26 | - { |
|
27 | - parent::__construct($table); |
|
28 | - $this->foreignKey = $foreignKey; |
|
29 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Returns the foreignkey the column is part of, if any. null otherwise. |
|
34 | - * |
|
35 | - * @return ForeignKeyConstraint|null |
|
36 | - */ |
|
37 | - public function getForeignKey() |
|
38 | - { |
|
39 | - return $this->foreignKey; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
44 | - * |
|
45 | - * @return null|string |
|
46 | - */ |
|
47 | - public function getClassName() |
|
48 | - { |
|
49 | - return TDBMDaoGenerator::getBeanNameFromTableName($this->foreignKey->getForeignTableName()); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Returns the param annotation for this property (useful for constructor). |
|
54 | - * |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - public function getParamAnnotation() |
|
58 | - { |
|
59 | - $str = ' * @param %s %s'; |
|
60 | - |
|
61 | - return sprintf($str, $this->getClassName(), $this->getVariableName()); |
|
62 | - } |
|
63 | - |
|
64 | - public function getUpperCamelCaseName() |
|
65 | - { |
|
66 | - // First, are there many column or only one? |
|
67 | - // If one column, we name the setter after it. Otherwise, we name the setter after the table name |
|
68 | - if (count($this->foreignKey->getLocalColumns()) > 1) { |
|
69 | - $name = TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($this->foreignKey->getForeignTableName())); |
|
70 | - if ($this->alternativeName) { |
|
71 | - $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $this->foreignKey->getLocalColumns()); |
|
72 | - |
|
73 | - $name .= 'By'.implode('And', $camelizedColumns); |
|
74 | - } |
|
75 | - } else { |
|
76 | - $column = $this->foreignKey->getLocalColumns()[0]; |
|
77 | - // Let's remove any _id or id_. |
|
78 | - if (strpos(strtolower($column), 'id_') === 0) { |
|
79 | - $column = substr($column, 3); |
|
80 | - } |
|
81 | - if (strrpos(strtolower($column), '_id') === strlen($column) - 3) { |
|
82 | - $column = substr($column, 0, strlen($column) - 3); |
|
83 | - } |
|
84 | - $name = TDBMDaoGenerator::toCamelCase($column); |
|
85 | - if ($this->alternativeName) { |
|
86 | - $name .= 'Object'; |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - return $name; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
95 | - * |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - public function isCompulsory() |
|
99 | - { |
|
100 | - // Are all columns nullable? |
|
101 | - $localColumnNames = $this->foreignKey->getLocalColumns(); |
|
102 | - |
|
103 | - foreach ($localColumnNames as $name) { |
|
104 | - $column = $this->table->getColumn($name); |
|
105 | - if ($column->getNotnull()) { |
|
106 | - return true; |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - return false; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Returns true if the property has a default value. |
|
115 | - * |
|
116 | - * @return bool |
|
117 | - */ |
|
118 | - public function hasDefault() |
|
119 | - { |
|
120 | - return false; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Returns the code that assigns a value to its default value. |
|
125 | - * |
|
126 | - * @return string |
|
127 | - * |
|
128 | - * @throws \TDBMException |
|
129 | - */ |
|
130 | - public function assignToDefaultCode() |
|
131 | - { |
|
132 | - throw new \TDBMException('Foreign key based properties cannot be assigned a default value.'); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Returns true if the property is the primary key. |
|
137 | - * |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - public function isPrimaryKey() |
|
141 | - { |
|
142 | - $fkColumns = $this->foreignKey->getLocalColumns(); |
|
143 | - sort($fkColumns); |
|
144 | - |
|
145 | - $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
146 | - sort($pkColumns); |
|
147 | - |
|
148 | - return $fkColumns == $pkColumns; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Returns the PHP code for getters and setters. |
|
153 | - * |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - public function getGetterSetterCode() |
|
157 | - { |
|
158 | - $tableName = $this->table->getName(); |
|
159 | - $getterName = $this->getGetterName(); |
|
160 | - $setterName = $this->getSetterName(); |
|
161 | - |
|
162 | - $referencedBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->foreignKey->getForeignTableName()); |
|
163 | - |
|
164 | - $str = ' /** |
|
15 | + /** |
|
16 | + * @var ForeignKeyConstraint |
|
17 | + */ |
|
18 | + private $foreignKey; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var SchemaAnalyzer |
|
22 | + */ |
|
23 | + private $schemaAnalyzer; |
|
24 | + |
|
25 | + public function __construct(Table $table, ForeignKeyConstraint $foreignKey, SchemaAnalyzer $schemaAnalyzer) |
|
26 | + { |
|
27 | + parent::__construct($table); |
|
28 | + $this->foreignKey = $foreignKey; |
|
29 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Returns the foreignkey the column is part of, if any. null otherwise. |
|
34 | + * |
|
35 | + * @return ForeignKeyConstraint|null |
|
36 | + */ |
|
37 | + public function getForeignKey() |
|
38 | + { |
|
39 | + return $this->foreignKey; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
44 | + * |
|
45 | + * @return null|string |
|
46 | + */ |
|
47 | + public function getClassName() |
|
48 | + { |
|
49 | + return TDBMDaoGenerator::getBeanNameFromTableName($this->foreignKey->getForeignTableName()); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Returns the param annotation for this property (useful for constructor). |
|
54 | + * |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + public function getParamAnnotation() |
|
58 | + { |
|
59 | + $str = ' * @param %s %s'; |
|
60 | + |
|
61 | + return sprintf($str, $this->getClassName(), $this->getVariableName()); |
|
62 | + } |
|
63 | + |
|
64 | + public function getUpperCamelCaseName() |
|
65 | + { |
|
66 | + // First, are there many column or only one? |
|
67 | + // If one column, we name the setter after it. Otherwise, we name the setter after the table name |
|
68 | + if (count($this->foreignKey->getLocalColumns()) > 1) { |
|
69 | + $name = TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($this->foreignKey->getForeignTableName())); |
|
70 | + if ($this->alternativeName) { |
|
71 | + $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $this->foreignKey->getLocalColumns()); |
|
72 | + |
|
73 | + $name .= 'By'.implode('And', $camelizedColumns); |
|
74 | + } |
|
75 | + } else { |
|
76 | + $column = $this->foreignKey->getLocalColumns()[0]; |
|
77 | + // Let's remove any _id or id_. |
|
78 | + if (strpos(strtolower($column), 'id_') === 0) { |
|
79 | + $column = substr($column, 3); |
|
80 | + } |
|
81 | + if (strrpos(strtolower($column), '_id') === strlen($column) - 3) { |
|
82 | + $column = substr($column, 0, strlen($column) - 3); |
|
83 | + } |
|
84 | + $name = TDBMDaoGenerator::toCamelCase($column); |
|
85 | + if ($this->alternativeName) { |
|
86 | + $name .= 'Object'; |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + return $name; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
95 | + * |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + public function isCompulsory() |
|
99 | + { |
|
100 | + // Are all columns nullable? |
|
101 | + $localColumnNames = $this->foreignKey->getLocalColumns(); |
|
102 | + |
|
103 | + foreach ($localColumnNames as $name) { |
|
104 | + $column = $this->table->getColumn($name); |
|
105 | + if ($column->getNotnull()) { |
|
106 | + return true; |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + return false; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Returns true if the property has a default value. |
|
115 | + * |
|
116 | + * @return bool |
|
117 | + */ |
|
118 | + public function hasDefault() |
|
119 | + { |
|
120 | + return false; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Returns the code that assigns a value to its default value. |
|
125 | + * |
|
126 | + * @return string |
|
127 | + * |
|
128 | + * @throws \TDBMException |
|
129 | + */ |
|
130 | + public function assignToDefaultCode() |
|
131 | + { |
|
132 | + throw new \TDBMException('Foreign key based properties cannot be assigned a default value.'); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Returns true if the property is the primary key. |
|
137 | + * |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + public function isPrimaryKey() |
|
141 | + { |
|
142 | + $fkColumns = $this->foreignKey->getLocalColumns(); |
|
143 | + sort($fkColumns); |
|
144 | + |
|
145 | + $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
146 | + sort($pkColumns); |
|
147 | + |
|
148 | + return $fkColumns == $pkColumns; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Returns the PHP code for getters and setters. |
|
153 | + * |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + public function getGetterSetterCode() |
|
157 | + { |
|
158 | + $tableName = $this->table->getName(); |
|
159 | + $getterName = $this->getGetterName(); |
|
160 | + $setterName = $this->getSetterName(); |
|
161 | + |
|
162 | + $referencedBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->foreignKey->getForeignTableName()); |
|
163 | + |
|
164 | + $str = ' /** |
|
165 | 165 | * Returns the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column. |
166 | 166 | * |
167 | 167 | * @return '.$referencedBeanName.' |
@@ -181,19 +181,19 @@ discard block |
||
181 | 181 | |
182 | 182 | '; |
183 | 183 | |
184 | - return $str; |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Returns the part of code useful when doing json serialization. |
|
189 | - * |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - public function getJsonSerializeCode() |
|
193 | - { |
|
194 | - return ' if (!$stopRecursion) { |
|
184 | + return $str; |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Returns the part of code useful when doing json serialization. |
|
189 | + * |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + public function getJsonSerializeCode() |
|
193 | + { |
|
194 | + return ' if (!$stopRecursion) { |
|
195 | 195 | $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName().'()->jsonSerialize(true); |
196 | 196 | } |
197 | 197 | '; |
198 | - } |
|
198 | + } |
|
199 | 199 | } |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | |
84 | 84 | private function toIndex($offset) |
85 | 85 | { |
86 | - if ($offset < 0 || filter_var($offset, FILTER_VALIDATE_INT) === false) { |
|
86 | + if ($offset<0 || filter_var($offset, FILTER_VALIDATE_INT) === false) { |
|
87 | 87 | throw new TDBMInvalidOffsetException('Trying to access result set using offset "'.$offset.'". An offset must be a positive integer.'); |
88 | 88 | } |
89 | 89 | if ($this->statement === null) { |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | public function next() |
101 | 101 | { |
102 | 102 | // Let's overload the next() method to store the result. |
103 | - if (isset($this->results[$this->key + 1])) { |
|
103 | + if (isset($this->results[$this->key+1])) { |
|
104 | 104 | ++$this->key; |
105 | 105 | $this->current = $this->results[$this->key]; |
106 | 106 | } else { |
@@ -27,100 +27,100 @@ |
||
27 | 27 | */ |
28 | 28 | class InnerResultArray extends InnerResultIterator |
29 | 29 | { |
30 | - /** |
|
31 | - * The list of results already fetched. |
|
32 | - * |
|
33 | - * @var AbstractTDBMObject[] |
|
34 | - */ |
|
35 | - private $results = []; |
|
30 | + /** |
|
31 | + * The list of results already fetched. |
|
32 | + * |
|
33 | + * @var AbstractTDBMObject[] |
|
34 | + */ |
|
35 | + private $results = []; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Whether a offset exists. |
|
39 | - * |
|
40 | - * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
41 | - * |
|
42 | - * @param mixed $offset <p> |
|
43 | - * An offset to check for. |
|
44 | - * </p> |
|
45 | - * |
|
46 | - * @return bool true on success or false on failure. |
|
47 | - * </p> |
|
48 | - * <p> |
|
49 | - * The return value will be casted to boolean if non-boolean was returned |
|
50 | - * |
|
51 | - * @since 5.0.0 |
|
52 | - */ |
|
53 | - public function offsetExists($offset) |
|
54 | - { |
|
55 | - try { |
|
56 | - $this->toIndex($offset); |
|
57 | - } catch (TDBMInvalidOffsetException $e) { |
|
58 | - return false; |
|
59 | - } |
|
37 | + /** |
|
38 | + * Whether a offset exists. |
|
39 | + * |
|
40 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
41 | + * |
|
42 | + * @param mixed $offset <p> |
|
43 | + * An offset to check for. |
|
44 | + * </p> |
|
45 | + * |
|
46 | + * @return bool true on success or false on failure. |
|
47 | + * </p> |
|
48 | + * <p> |
|
49 | + * The return value will be casted to boolean if non-boolean was returned |
|
50 | + * |
|
51 | + * @since 5.0.0 |
|
52 | + */ |
|
53 | + public function offsetExists($offset) |
|
54 | + { |
|
55 | + try { |
|
56 | + $this->toIndex($offset); |
|
57 | + } catch (TDBMInvalidOffsetException $e) { |
|
58 | + return false; |
|
59 | + } |
|
60 | 60 | |
61 | - return true; |
|
62 | - } |
|
61 | + return true; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Offset to retrieve. |
|
66 | - * |
|
67 | - * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
68 | - * |
|
69 | - * @param mixed $offset <p> |
|
70 | - * The offset to retrieve. |
|
71 | - * </p> |
|
72 | - * |
|
73 | - * @return mixed Can return all value types |
|
74 | - * |
|
75 | - * @since 5.0.0 |
|
76 | - */ |
|
77 | - public function offsetGet($offset) |
|
78 | - { |
|
79 | - $this->toIndex($offset); |
|
64 | + /** |
|
65 | + * Offset to retrieve. |
|
66 | + * |
|
67 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
68 | + * |
|
69 | + * @param mixed $offset <p> |
|
70 | + * The offset to retrieve. |
|
71 | + * </p> |
|
72 | + * |
|
73 | + * @return mixed Can return all value types |
|
74 | + * |
|
75 | + * @since 5.0.0 |
|
76 | + */ |
|
77 | + public function offsetGet($offset) |
|
78 | + { |
|
79 | + $this->toIndex($offset); |
|
80 | 80 | |
81 | - return $this->results[$offset]; |
|
82 | - } |
|
81 | + return $this->results[$offset]; |
|
82 | + } |
|
83 | 83 | |
84 | - private function toIndex($offset) |
|
85 | - { |
|
86 | - if ($offset < 0 || filter_var($offset, FILTER_VALIDATE_INT) === false) { |
|
87 | - throw new TDBMInvalidOffsetException('Trying to access result set using offset "'.$offset.'". An offset must be a positive integer.'); |
|
88 | - } |
|
89 | - if ($this->statement === null) { |
|
90 | - $this->executeQuery(); |
|
91 | - } |
|
92 | - while (!isset($this->results[$offset])) { |
|
93 | - $this->next(); |
|
94 | - if ($this->current === null) { |
|
95 | - throw new TDBMInvalidOffsetException('Offset "'.$offset.'" does not exist in result set.'); |
|
96 | - } |
|
97 | - } |
|
98 | - } |
|
84 | + private function toIndex($offset) |
|
85 | + { |
|
86 | + if ($offset < 0 || filter_var($offset, FILTER_VALIDATE_INT) === false) { |
|
87 | + throw new TDBMInvalidOffsetException('Trying to access result set using offset "'.$offset.'". An offset must be a positive integer.'); |
|
88 | + } |
|
89 | + if ($this->statement === null) { |
|
90 | + $this->executeQuery(); |
|
91 | + } |
|
92 | + while (!isset($this->results[$offset])) { |
|
93 | + $this->next(); |
|
94 | + if ($this->current === null) { |
|
95 | + throw new TDBMInvalidOffsetException('Offset "'.$offset.'" does not exist in result set.'); |
|
96 | + } |
|
97 | + } |
|
98 | + } |
|
99 | 99 | |
100 | - public function next() |
|
101 | - { |
|
102 | - // Let's overload the next() method to store the result. |
|
103 | - if (isset($this->results[$this->key + 1])) { |
|
104 | - ++$this->key; |
|
105 | - $this->current = $this->results[$this->key]; |
|
106 | - } else { |
|
107 | - parent::next(); |
|
108 | - if ($this->current !== null) { |
|
109 | - $this->results[$this->key] = $this->current; |
|
110 | - } |
|
111 | - } |
|
112 | - } |
|
100 | + public function next() |
|
101 | + { |
|
102 | + // Let's overload the next() method to store the result. |
|
103 | + if (isset($this->results[$this->key + 1])) { |
|
104 | + ++$this->key; |
|
105 | + $this->current = $this->results[$this->key]; |
|
106 | + } else { |
|
107 | + parent::next(); |
|
108 | + if ($this->current !== null) { |
|
109 | + $this->results[$this->key] = $this->current; |
|
110 | + } |
|
111 | + } |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Overloads the rewind implementation. |
|
116 | - * Do not reexecute the query. |
|
117 | - */ |
|
118 | - public function rewind() |
|
119 | - { |
|
120 | - if (!$this->fetchStarted) { |
|
121 | - $this->executeQuery(); |
|
122 | - } |
|
123 | - $this->key = -1; |
|
124 | - $this->next(); |
|
125 | - } |
|
114 | + /** |
|
115 | + * Overloads the rewind implementation. |
|
116 | + * Do not reexecute the query. |
|
117 | + */ |
|
118 | + public function rewind() |
|
119 | + { |
|
120 | + if (!$this->fetchStarted) { |
|
121 | + $this->executeQuery(); |
|
122 | + } |
|
123 | + $this->key = -1; |
|
124 | + $this->next(); |
|
125 | + } |
|
126 | 126 | } |
@@ -27,10 +27,10 @@ |
||
27 | 27 | */ |
28 | 28 | final class TDBMObjectStateEnum extends AbstractTDBMObject |
29 | 29 | { |
30 | - const STATE_DETACHED = 'detached'; |
|
31 | - const STATE_NEW = 'new'; |
|
32 | - const STATE_NOT_LOADED = 'not loaded'; |
|
33 | - const STATE_LOADED = 'loaded'; |
|
34 | - const STATE_DIRTY = 'dirty'; |
|
35 | - const STATE_DELETED = 'deleted'; |
|
30 | + const STATE_DETACHED = 'detached'; |
|
31 | + const STATE_NEW = 'new'; |
|
32 | + const STATE_NOT_LOADED = 'not loaded'; |
|
33 | + const STATE_LOADED = 'loaded'; |
|
34 | + const STATE_DIRTY = 'dirty'; |
|
35 | + const STATE_DELETED = 'deleted'; |
|
36 | 36 | } |
@@ -30,78 +30,78 @@ |
||
30 | 30 | */ |
31 | 31 | class StandardObjectStorage |
32 | 32 | { |
33 | - /** |
|
34 | - * An array of fetched object, accessible via table name and primary key. |
|
35 | - * If the primary key is split on several columns, access is done by an array of columns, serialized. |
|
36 | - * |
|
37 | - * @var array<string, array<string, TDBMObject>> |
|
38 | - */ |
|
39 | - private $objects = array(); |
|
33 | + /** |
|
34 | + * An array of fetched object, accessible via table name and primary key. |
|
35 | + * If the primary key is split on several columns, access is done by an array of columns, serialized. |
|
36 | + * |
|
37 | + * @var array<string, array<string, TDBMObject>> |
|
38 | + */ |
|
39 | + private $objects = array(); |
|
40 | 40 | |
41 | - /** |
|
42 | - * Sets an object in the storage. |
|
43 | - * |
|
44 | - * @param string $tableName |
|
45 | - * @param string $id |
|
46 | - * @param TDBMObject $dbRow |
|
47 | - */ |
|
48 | - public function set($tableName, $id, DbRow $dbRow) |
|
49 | - { |
|
50 | - $this->objects[$tableName][$id] = $dbRow; |
|
51 | - } |
|
41 | + /** |
|
42 | + * Sets an object in the storage. |
|
43 | + * |
|
44 | + * @param string $tableName |
|
45 | + * @param string $id |
|
46 | + * @param TDBMObject $dbRow |
|
47 | + */ |
|
48 | + public function set($tableName, $id, DbRow $dbRow) |
|
49 | + { |
|
50 | + $this->objects[$tableName][$id] = $dbRow; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Checks if an object is in the storage. |
|
55 | - * |
|
56 | - * @param string $tableName |
|
57 | - * @param string $id |
|
58 | - * |
|
59 | - * @return bool |
|
60 | - */ |
|
61 | - public function has($tableName, $id) |
|
62 | - { |
|
63 | - return isset($this->objects[$tableName][$id]); |
|
64 | - } |
|
53 | + /** |
|
54 | + * Checks if an object is in the storage. |
|
55 | + * |
|
56 | + * @param string $tableName |
|
57 | + * @param string $id |
|
58 | + * |
|
59 | + * @return bool |
|
60 | + */ |
|
61 | + public function has($tableName, $id) |
|
62 | + { |
|
63 | + return isset($this->objects[$tableName][$id]); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Returns an object from the storage (or null if no object is set). |
|
68 | - * |
|
69 | - * @param string $tableName |
|
70 | - * @param string $id |
|
71 | - * |
|
72 | - * @return DbRow |
|
73 | - */ |
|
74 | - public function get($tableName, $id) |
|
75 | - { |
|
76 | - if (isset($this->objects[$tableName][$id])) { |
|
77 | - return $this->objects[$tableName][$id]; |
|
78 | - } else { |
|
79 | - return; |
|
80 | - } |
|
81 | - } |
|
66 | + /** |
|
67 | + * Returns an object from the storage (or null if no object is set). |
|
68 | + * |
|
69 | + * @param string $tableName |
|
70 | + * @param string $id |
|
71 | + * |
|
72 | + * @return DbRow |
|
73 | + */ |
|
74 | + public function get($tableName, $id) |
|
75 | + { |
|
76 | + if (isset($this->objects[$tableName][$id])) { |
|
77 | + return $this->objects[$tableName][$id]; |
|
78 | + } else { |
|
79 | + return; |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Removes an object from the storage. |
|
85 | - * |
|
86 | - * @param string $tableName |
|
87 | - * @param string $id |
|
88 | - */ |
|
89 | - public function remove($tableName, $id) |
|
90 | - { |
|
91 | - unset($this->objects[$tableName][$id]); |
|
92 | - } |
|
83 | + /** |
|
84 | + * Removes an object from the storage. |
|
85 | + * |
|
86 | + * @param string $tableName |
|
87 | + * @param string $id |
|
88 | + */ |
|
89 | + public function remove($tableName, $id) |
|
90 | + { |
|
91 | + unset($this->objects[$tableName][$id]); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Applies the callback to all objects. |
|
96 | - * |
|
97 | - * @param callable $callback |
|
98 | - */ |
|
99 | - public function apply(callable $callback) |
|
100 | - { |
|
101 | - foreach ($this->objects as $tableName => $table) { |
|
102 | - foreach ($table as $id => $obj) { |
|
103 | - $callback($obj, $tableName, $id); |
|
104 | - } |
|
105 | - } |
|
106 | - } |
|
94 | + /** |
|
95 | + * Applies the callback to all objects. |
|
96 | + * |
|
97 | + * @param callable $callback |
|
98 | + */ |
|
99 | + public function apply(callable $callback) |
|
100 | + { |
|
101 | + foreach ($this->objects as $tableName => $table) { |
|
102 | + foreach ($table as $id => $obj) { |
|
103 | + $callback($obj, $tableName, $id); |
|
104 | + } |
|
105 | + } |
|
106 | + } |
|
107 | 107 | } |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | */ |
113 | 113 | public function getCurrentPage() |
114 | 114 | { |
115 | - return floor($this->offset / $this->limit) + 1; |
|
115 | + return floor($this->offset/$this->limit)+1; |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | /** |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | */ |
252 | 252 | public function jsonSerialize() |
253 | 253 | { |
254 | - return array_map(function (AbstractTDBMObject $item) { |
|
254 | + return array_map(function(AbstractTDBMObject $item) { |
|
255 | 255 | return $item->jsonSerialize(); |
256 | 256 | }, $this->toArray()); |
257 | 257 | } |
@@ -30,238 +30,238 @@ |
||
30 | 30 | */ |
31 | 31 | class PageIterator implements Page, \ArrayAccess, \JsonSerializable |
32 | 32 | { |
33 | - /** |
|
34 | - * @var Statement |
|
35 | - */ |
|
36 | - protected $statement; |
|
33 | + /** |
|
34 | + * @var Statement |
|
35 | + */ |
|
36 | + protected $statement; |
|
37 | 37 | |
38 | - protected $fetchStarted = false; |
|
39 | - private $objectStorage; |
|
40 | - private $className; |
|
38 | + protected $fetchStarted = false; |
|
39 | + private $objectStorage; |
|
40 | + private $className; |
|
41 | 41 | |
42 | - private $parentResult; |
|
43 | - private $tdbmService; |
|
44 | - private $magicSql; |
|
45 | - private $parameters; |
|
46 | - private $limit; |
|
47 | - private $offset; |
|
48 | - private $columnDescriptors; |
|
49 | - private $magicQuery; |
|
42 | + private $parentResult; |
|
43 | + private $tdbmService; |
|
44 | + private $magicSql; |
|
45 | + private $parameters; |
|
46 | + private $limit; |
|
47 | + private $offset; |
|
48 | + private $columnDescriptors; |
|
49 | + private $magicQuery; |
|
50 | 50 | |
51 | - /** |
|
52 | - * The key of the current retrieved object. |
|
53 | - * |
|
54 | - * @var int |
|
55 | - */ |
|
56 | - protected $key = -1; |
|
51 | + /** |
|
52 | + * The key of the current retrieved object. |
|
53 | + * |
|
54 | + * @var int |
|
55 | + */ |
|
56 | + protected $key = -1; |
|
57 | 57 | |
58 | - protected $current = null; |
|
58 | + protected $current = null; |
|
59 | 59 | |
60 | - private $databasePlatform; |
|
60 | + private $databasePlatform; |
|
61 | 61 | |
62 | - private $innerResultIterator; |
|
62 | + private $innerResultIterator; |
|
63 | 63 | |
64 | - private $mode; |
|
64 | + private $mode; |
|
65 | 65 | |
66 | - /** |
|
67 | - * @var LoggerInterface |
|
68 | - */ |
|
69 | - private $logger; |
|
66 | + /** |
|
67 | + * @var LoggerInterface |
|
68 | + */ |
|
69 | + private $logger; |
|
70 | 70 | |
71 | - public function __construct(ResultIterator $parentResult, $magicSql, array $parameters, $limit, $offset, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger) |
|
72 | - { |
|
73 | - $this->parentResult = $parentResult; |
|
74 | - $this->magicSql = $magicSql; |
|
75 | - $this->objectStorage = $objectStorage; |
|
76 | - $this->className = $className; |
|
77 | - $this->tdbmService = $tdbmService; |
|
78 | - $this->parameters = $parameters; |
|
79 | - $this->limit = $limit; |
|
80 | - $this->offset = $offset; |
|
81 | - $this->columnDescriptors = $columnDescriptors; |
|
82 | - $this->magicQuery = $magicQuery; |
|
83 | - $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform(); |
|
84 | - $this->mode = $mode; |
|
85 | - $this->logger = $logger; |
|
86 | - } |
|
71 | + public function __construct(ResultIterator $parentResult, $magicSql, array $parameters, $limit, $offset, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger) |
|
72 | + { |
|
73 | + $this->parentResult = $parentResult; |
|
74 | + $this->magicSql = $magicSql; |
|
75 | + $this->objectStorage = $objectStorage; |
|
76 | + $this->className = $className; |
|
77 | + $this->tdbmService = $tdbmService; |
|
78 | + $this->parameters = $parameters; |
|
79 | + $this->limit = $limit; |
|
80 | + $this->offset = $offset; |
|
81 | + $this->columnDescriptors = $columnDescriptors; |
|
82 | + $this->magicQuery = $magicQuery; |
|
83 | + $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform(); |
|
84 | + $this->mode = $mode; |
|
85 | + $this->logger = $logger; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Retrieve an external iterator. |
|
90 | - * |
|
91 | - * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
92 | - * |
|
93 | - * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
|
94 | - * <b>Traversable</b> |
|
95 | - * |
|
96 | - * @since 5.0.0 |
|
97 | - */ |
|
98 | - public function getIterator() |
|
99 | - { |
|
100 | - if ($this->innerResultIterator === null) { |
|
101 | - if ($this->mode === TDBMService::MODE_CURSOR) { |
|
102 | - $this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
103 | - } else { |
|
104 | - $this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
105 | - } |
|
106 | - } |
|
88 | + /** |
|
89 | + * Retrieve an external iterator. |
|
90 | + * |
|
91 | + * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
92 | + * |
|
93 | + * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
|
94 | + * <b>Traversable</b> |
|
95 | + * |
|
96 | + * @since 5.0.0 |
|
97 | + */ |
|
98 | + public function getIterator() |
|
99 | + { |
|
100 | + if ($this->innerResultIterator === null) { |
|
101 | + if ($this->mode === TDBMService::MODE_CURSOR) { |
|
102 | + $this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
103 | + } else { |
|
104 | + $this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
105 | + } |
|
106 | + } |
|
107 | 107 | |
108 | - return $this->innerResultIterator; |
|
109 | - } |
|
108 | + return $this->innerResultIterator; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * @return int |
|
113 | - */ |
|
114 | - public function getCurrentOffset() |
|
115 | - { |
|
116 | - return $this->offset; |
|
117 | - } |
|
111 | + /** |
|
112 | + * @return int |
|
113 | + */ |
|
114 | + public function getCurrentOffset() |
|
115 | + { |
|
116 | + return $this->offset; |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * @return int |
|
121 | - */ |
|
122 | - public function getCurrentPage() |
|
123 | - { |
|
124 | - return floor($this->offset / $this->limit) + 1; |
|
125 | - } |
|
119 | + /** |
|
120 | + * @return int |
|
121 | + */ |
|
122 | + public function getCurrentPage() |
|
123 | + { |
|
124 | + return floor($this->offset / $this->limit) + 1; |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * @return int |
|
129 | - */ |
|
130 | - public function getCurrentLimit() |
|
131 | - { |
|
132 | - return $this->limit; |
|
133 | - } |
|
127 | + /** |
|
128 | + * @return int |
|
129 | + */ |
|
130 | + public function getCurrentLimit() |
|
131 | + { |
|
132 | + return $this->limit; |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Return the number of results on the current page of the {@link Result}. |
|
137 | - * |
|
138 | - * @return int |
|
139 | - */ |
|
140 | - public function count() |
|
141 | - { |
|
142 | - return $this->getIterator()->count(); |
|
143 | - } |
|
135 | + /** |
|
136 | + * Return the number of results on the current page of the {@link Result}. |
|
137 | + * |
|
138 | + * @return int |
|
139 | + */ |
|
140 | + public function count() |
|
141 | + { |
|
142 | + return $this->getIterator()->count(); |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * Return the number of ALL results in the paginatable of {@link Result}. |
|
147 | - * |
|
148 | - * @return int |
|
149 | - */ |
|
150 | - public function totalCount() |
|
151 | - { |
|
152 | - return $this->parentResult->count(); |
|
153 | - } |
|
145 | + /** |
|
146 | + * Return the number of ALL results in the paginatable of {@link Result}. |
|
147 | + * |
|
148 | + * @return int |
|
149 | + */ |
|
150 | + public function totalCount() |
|
151 | + { |
|
152 | + return $this->parentResult->count(); |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * Casts the result set to a PHP array. |
|
157 | - * |
|
158 | - * @return array |
|
159 | - */ |
|
160 | - public function toArray() |
|
161 | - { |
|
162 | - return iterator_to_array($this->getIterator()); |
|
163 | - } |
|
155 | + /** |
|
156 | + * Casts the result set to a PHP array. |
|
157 | + * |
|
158 | + * @return array |
|
159 | + */ |
|
160 | + public function toArray() |
|
161 | + { |
|
162 | + return iterator_to_array($this->getIterator()); |
|
163 | + } |
|
164 | 164 | |
165 | - /** |
|
166 | - * Returns a new iterator mapping any call using the $callable function. |
|
167 | - * |
|
168 | - * @param callable $callable |
|
169 | - * |
|
170 | - * @return MapIterator |
|
171 | - */ |
|
172 | - public function map(callable $callable) |
|
173 | - { |
|
174 | - return new MapIterator($this->getIterator(), $callable); |
|
175 | - } |
|
165 | + /** |
|
166 | + * Returns a new iterator mapping any call using the $callable function. |
|
167 | + * |
|
168 | + * @param callable $callable |
|
169 | + * |
|
170 | + * @return MapIterator |
|
171 | + */ |
|
172 | + public function map(callable $callable) |
|
173 | + { |
|
174 | + return new MapIterator($this->getIterator(), $callable); |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Whether a offset exists. |
|
179 | - * |
|
180 | - * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
181 | - * |
|
182 | - * @param mixed $offset <p> |
|
183 | - * An offset to check for. |
|
184 | - * </p> |
|
185 | - * |
|
186 | - * @return bool true on success or false on failure. |
|
187 | - * </p> |
|
188 | - * <p> |
|
189 | - * The return value will be casted to boolean if non-boolean was returned |
|
190 | - * |
|
191 | - * @since 5.0.0 |
|
192 | - */ |
|
193 | - public function offsetExists($offset) |
|
194 | - { |
|
195 | - return $this->getIterator()->offsetExists($offset); |
|
196 | - } |
|
177 | + /** |
|
178 | + * Whether a offset exists. |
|
179 | + * |
|
180 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
181 | + * |
|
182 | + * @param mixed $offset <p> |
|
183 | + * An offset to check for. |
|
184 | + * </p> |
|
185 | + * |
|
186 | + * @return bool true on success or false on failure. |
|
187 | + * </p> |
|
188 | + * <p> |
|
189 | + * The return value will be casted to boolean if non-boolean was returned |
|
190 | + * |
|
191 | + * @since 5.0.0 |
|
192 | + */ |
|
193 | + public function offsetExists($offset) |
|
194 | + { |
|
195 | + return $this->getIterator()->offsetExists($offset); |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * Offset to retrieve. |
|
200 | - * |
|
201 | - * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
202 | - * |
|
203 | - * @param mixed $offset <p> |
|
204 | - * The offset to retrieve. |
|
205 | - * </p> |
|
206 | - * |
|
207 | - * @return mixed Can return all value types |
|
208 | - * |
|
209 | - * @since 5.0.0 |
|
210 | - */ |
|
211 | - public function offsetGet($offset) |
|
212 | - { |
|
213 | - return $this->getIterator()->offsetGet($offset); |
|
214 | - } |
|
198 | + /** |
|
199 | + * Offset to retrieve. |
|
200 | + * |
|
201 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
202 | + * |
|
203 | + * @param mixed $offset <p> |
|
204 | + * The offset to retrieve. |
|
205 | + * </p> |
|
206 | + * |
|
207 | + * @return mixed Can return all value types |
|
208 | + * |
|
209 | + * @since 5.0.0 |
|
210 | + */ |
|
211 | + public function offsetGet($offset) |
|
212 | + { |
|
213 | + return $this->getIterator()->offsetGet($offset); |
|
214 | + } |
|
215 | 215 | |
216 | - /** |
|
217 | - * Offset to set. |
|
218 | - * |
|
219 | - * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
220 | - * |
|
221 | - * @param mixed $offset <p> |
|
222 | - * The offset to assign the value to. |
|
223 | - * </p> |
|
224 | - * @param mixed $value <p> |
|
225 | - * The value to set. |
|
226 | - * </p> |
|
227 | - * |
|
228 | - * @since 5.0.0 |
|
229 | - */ |
|
230 | - public function offsetSet($offset, $value) |
|
231 | - { |
|
232 | - return $this->getIterator()->offsetSet($offset, $value); |
|
233 | - } |
|
216 | + /** |
|
217 | + * Offset to set. |
|
218 | + * |
|
219 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
220 | + * |
|
221 | + * @param mixed $offset <p> |
|
222 | + * The offset to assign the value to. |
|
223 | + * </p> |
|
224 | + * @param mixed $value <p> |
|
225 | + * The value to set. |
|
226 | + * </p> |
|
227 | + * |
|
228 | + * @since 5.0.0 |
|
229 | + */ |
|
230 | + public function offsetSet($offset, $value) |
|
231 | + { |
|
232 | + return $this->getIterator()->offsetSet($offset, $value); |
|
233 | + } |
|
234 | 234 | |
235 | - /** |
|
236 | - * Offset to unset. |
|
237 | - * |
|
238 | - * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
239 | - * |
|
240 | - * @param mixed $offset <p> |
|
241 | - * The offset to unset. |
|
242 | - * </p> |
|
243 | - * |
|
244 | - * @since 5.0.0 |
|
245 | - */ |
|
246 | - public function offsetUnset($offset) |
|
247 | - { |
|
248 | - return $this->getIterator()->offsetUnset($offset); |
|
249 | - } |
|
235 | + /** |
|
236 | + * Offset to unset. |
|
237 | + * |
|
238 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
239 | + * |
|
240 | + * @param mixed $offset <p> |
|
241 | + * The offset to unset. |
|
242 | + * </p> |
|
243 | + * |
|
244 | + * @since 5.0.0 |
|
245 | + */ |
|
246 | + public function offsetUnset($offset) |
|
247 | + { |
|
248 | + return $this->getIterator()->offsetUnset($offset); |
|
249 | + } |
|
250 | 250 | |
251 | - /** |
|
252 | - * Specify data which should be serialized to JSON. |
|
253 | - * |
|
254 | - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
255 | - * |
|
256 | - * @return mixed data which can be serialized by <b>json_encode</b>, |
|
257 | - * which is a value of any type other than a resource |
|
258 | - * |
|
259 | - * @since 5.4.0 |
|
260 | - */ |
|
261 | - public function jsonSerialize() |
|
262 | - { |
|
263 | - return array_map(function (AbstractTDBMObject $item) { |
|
264 | - return $item->jsonSerialize(); |
|
265 | - }, $this->toArray()); |
|
266 | - } |
|
251 | + /** |
|
252 | + * Specify data which should be serialized to JSON. |
|
253 | + * |
|
254 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
255 | + * |
|
256 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
|
257 | + * which is a value of any type other than a resource |
|
258 | + * |
|
259 | + * @since 5.4.0 |
|
260 | + */ |
|
261 | + public function jsonSerialize() |
|
262 | + { |
|
263 | + return array_map(function (AbstractTDBMObject $item) { |
|
264 | + return $item->jsonSerialize(); |
|
265 | + }, $this->toArray()); |
|
266 | + } |
|
267 | 267 | } |
@@ -8,7 +8,7 @@ |
||
8 | 8 | <input type="hidden" id="selfedit" name="selfedit" value="<?php echo plainstring_to_htmlprotected($this->selfedit) ?>" /> |
9 | 9 | |
10 | 10 | <?php if (!$this->autoloadDetected) { |
11 | - ?> |
|
11 | + ?> |
|
12 | 12 | <div class="alert">Warning! TDBM could not detect the autoload section of your composer.json file. |
13 | 13 | Unless you are developing your own autoload system, you should configure <strong>composer.json</strong> to <a href="http://getcomposer.org/doc/01-basic-usage.md#autoloading" target="_blank">define a source directory and a root namespace using PSR-0</a>.</div> |
14 | 14 | <?php |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | */ |
187 | 187 | private function checkTableName($tableName = null) { |
188 | 188 | if ($tableName === null) { |
189 | - if (count($this->dbRows) > 1) { |
|
189 | + if (count($this->dbRows)>1) { |
|
190 | 190 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
191 | 191 | } elseif (count($this->dbRows) === 1) { |
192 | 192 | $tableName = array_keys($this->dbRows)[0]; |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | protected function set($var, $value, $tableName = null) |
214 | 214 | { |
215 | 215 | if ($tableName === null) { |
216 | - if (count($this->dbRows) > 1) { |
|
216 | + if (count($this->dbRows)>1) { |
|
217 | 217 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
218 | 218 | } elseif (count($this->dbRows) === 1) { |
219 | 219 | $tableName = array_keys($this->dbRows)[0]; |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | protected function setRef($foreignKeyName, AbstractTDBMObject $bean, $tableName = null) |
240 | 240 | { |
241 | 241 | if ($tableName === null) { |
242 | - if (count($this->dbRows) > 1) { |
|
242 | + if (count($this->dbRows)>1) { |
|
243 | 243 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
244 | 244 | } elseif (count($this->dbRows) === 1) { |
245 | 245 | $tableName = array_keys($this->dbRows)[0]; |
@@ -31,604 +31,604 @@ |
||
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 = array(); |
|
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 = array(), 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 | - |
|
185 | - /** |
|
186 | - * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null" |
|
187 | - * or throws an error. |
|
188 | - * |
|
189 | - * @param string $tableName |
|
190 | - * |
|
191 | - * @return string |
|
192 | - */ |
|
193 | - private function checkTableName($tableName = null) |
|
194 | - { |
|
195 | - if ($tableName === null) { |
|
196 | - if (count($this->dbRows) > 1) { |
|
197 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
198 | - } elseif (count($this->dbRows) === 1) { |
|
199 | - $tableName = array_keys($this->dbRows)[0]; |
|
200 | - } |
|
201 | - } |
|
202 | - |
|
203 | - if (!isset($this->dbRows[$tableName])) { |
|
204 | - if (count($this->dbRows === 0)) { |
|
205 | - throw new TDBMException('Object is not yet bound to any table.'); |
|
206 | - } else { |
|
207 | - throw new TDBMException('Unknown table "'.$tableName.'"" in object.'); |
|
208 | - } |
|
209 | - } |
|
210 | - |
|
211 | - return $tableName; |
|
212 | - } |
|
213 | - |
|
214 | - protected function get($var, $tableName = null) |
|
215 | - { |
|
216 | - $tableName = $this->checkTableName($tableName); |
|
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 | - return $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Adds a many to many relationship to this bean. |
|
292 | - * |
|
293 | - * @param string $pivotTableName |
|
294 | - * @param AbstractTDBMObject $remoteBean |
|
295 | - */ |
|
296 | - protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
297 | - { |
|
298 | - $this->setRelationship($pivotTableName, $remoteBean, 'new'); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Returns true if there is a relationship to this bean. |
|
303 | - * |
|
304 | - * @param string $pivotTableName |
|
305 | - * @param AbstractTDBMObject $remoteBean |
|
306 | - * |
|
307 | - * @return bool |
|
308 | - */ |
|
309 | - protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
310 | - { |
|
311 | - $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
312 | - |
|
313 | - if ($storage->contains($remoteBean)) { |
|
314 | - if ($storage[$remoteBean]['status'] !== 'delete') { |
|
315 | - return true; |
|
316 | - } |
|
317 | - } |
|
318 | - |
|
319 | - return false; |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Internal TDBM method. Removes a many to many relationship from this bean. |
|
324 | - * |
|
325 | - * @param string $pivotTableName |
|
326 | - * @param AbstractTDBMObject $remoteBean |
|
327 | - */ |
|
328 | - public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
329 | - { |
|
330 | - if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') { |
|
331 | - unset($this->relationships[$pivotTableName][$remoteBean]); |
|
332 | - unset($remoteBean->relationships[$pivotTableName][$this]); |
|
333 | - } else { |
|
334 | - $this->setRelationship($pivotTableName, $remoteBean, 'delete'); |
|
335 | - } |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * Sets many to many relationships for this bean. |
|
340 | - * Adds new relationships and removes unused ones. |
|
341 | - * |
|
342 | - * @param $pivotTableName |
|
343 | - * @param array $remoteBeans |
|
344 | - */ |
|
345 | - protected function setRelationships($pivotTableName, array $remoteBeans) |
|
346 | - { |
|
347 | - $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
348 | - |
|
349 | - foreach ($storage as $oldRemoteBean) { |
|
350 | - if (!in_array($oldRemoteBean, $remoteBeans, true)) { |
|
351 | - // $oldRemoteBean must be removed |
|
352 | - $this->_removeRelationship($pivotTableName, $oldRemoteBean); |
|
353 | - } |
|
354 | - } |
|
355 | - |
|
356 | - foreach ($remoteBeans as $remoteBean) { |
|
357 | - if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') { |
|
358 | - // $remoteBean must be added |
|
359 | - $this->addRelationship($pivotTableName, $remoteBean); |
|
360 | - } |
|
361 | - } |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * Returns the list of objects linked to this bean via $pivotTableName. |
|
366 | - * |
|
367 | - * @param $pivotTableName |
|
368 | - * |
|
369 | - * @return \SplObjectStorage |
|
370 | - */ |
|
371 | - private function retrieveRelationshipsStorage($pivotTableName) |
|
372 | - { |
|
373 | - $storage = $this->getRelationshipStorage($pivotTableName); |
|
374 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) { |
|
375 | - return $storage; |
|
376 | - } |
|
377 | - |
|
378 | - $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this); |
|
379 | - $this->loadedRelationships[$pivotTableName] = true; |
|
380 | - |
|
381 | - foreach ($beans as $bean) { |
|
382 | - if (isset($storage[$bean])) { |
|
383 | - $oldStatus = $storage[$bean]['status']; |
|
384 | - if ($oldStatus === 'delete') { |
|
385 | - // Keep deleted things deleted |
|
386 | - continue; |
|
387 | - } |
|
388 | - } |
|
389 | - $this->setRelationship($pivotTableName, $bean, 'loaded'); |
|
390 | - } |
|
391 | - |
|
392 | - return $storage; |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
|
397 | - * |
|
398 | - * @param $pivotTableName |
|
399 | - * |
|
400 | - * @return AbstractTDBMObject[] |
|
401 | - */ |
|
402 | - public function _getRelationships($pivotTableName) |
|
403 | - { |
|
404 | - return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName)); |
|
405 | - } |
|
406 | - |
|
407 | - private function relationshipStorageToArray(\SplObjectStorage $storage) |
|
408 | - { |
|
409 | - $beans = []; |
|
410 | - foreach ($storage as $bean) { |
|
411 | - $statusArr = $storage[$bean]; |
|
412 | - if ($statusArr['status'] !== 'delete') { |
|
413 | - $beans[] = $bean; |
|
414 | - } |
|
415 | - } |
|
416 | - |
|
417 | - return $beans; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Declares a relationship between. |
|
422 | - * |
|
423 | - * @param string $pivotTableName |
|
424 | - * @param AbstractTDBMObject $remoteBean |
|
425 | - * @param string $status |
|
426 | - */ |
|
427 | - private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
|
428 | - { |
|
429 | - $storage = $this->getRelationshipStorage($pivotTableName); |
|
430 | - $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
431 | - if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
432 | - $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
|
433 | - } |
|
434 | - |
|
435 | - $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
|
436 | - $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
441 | - * |
|
442 | - * @param string $pivotTableName |
|
443 | - * |
|
444 | - * @return \SplObjectStorage |
|
445 | - */ |
|
446 | - private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage |
|
447 | - { |
|
448 | - return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage(); |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
453 | - * |
|
454 | - * @param string $tableName |
|
455 | - * @param string $foreignKeyName |
|
456 | - * |
|
457 | - * @return AlterableResultIterator |
|
458 | - */ |
|
459 | - private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator |
|
460 | - { |
|
461 | - $key = $tableName.'___'.$foreignKeyName; |
|
462 | - |
|
463 | - return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator(); |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * Declares a relationship between this bean and the bean pointing to it. |
|
468 | - * |
|
469 | - * @param string $tableName |
|
470 | - * @param string $foreignKeyName |
|
471 | - * @param AbstractTDBMObject $remoteBean |
|
472 | - */ |
|
473 | - private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
474 | - { |
|
475 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
476 | - $alterableResultIterator->add($remoteBean); |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Declares a relationship between this bean and the bean pointing to it. |
|
481 | - * |
|
482 | - * @param string $tableName |
|
483 | - * @param string $foreignKeyName |
|
484 | - * @param AbstractTDBMObject $remoteBean |
|
485 | - */ |
|
486 | - private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
487 | - { |
|
488 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
489 | - $alterableResultIterator->remove($remoteBean); |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Returns the list of objects linked to this bean via a given foreign key. |
|
494 | - * |
|
495 | - * @param string $tableName |
|
496 | - * @param string $foreignKeyName |
|
497 | - * @param string $searchTableName |
|
498 | - * @param array $searchFilter |
|
499 | - * @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 ! |
|
500 | - * |
|
501 | - * @return AlterableResultIterator |
|
502 | - */ |
|
503 | - protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator |
|
504 | - { |
|
505 | - $key = $tableName.'___'.$foreignKeyName; |
|
506 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
507 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) { |
|
508 | - return $alterableResultIterator; |
|
509 | - } |
|
510 | - |
|
511 | - $unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString); |
|
512 | - |
|
513 | - $alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator()); |
|
514 | - |
|
515 | - return $alterableResultIterator; |
|
516 | - } |
|
517 | - |
|
518 | - /** |
|
519 | - * Reverts any changes made to the object and resumes it to its DB state. |
|
520 | - * This can only be called on objects that come from database and that have not been deleted. |
|
521 | - * Otherwise, this will throw an exception. |
|
522 | - * |
|
523 | - * @throws TDBMException |
|
524 | - */ |
|
525 | - public function discardChanges() |
|
526 | - { |
|
527 | - if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
528 | - 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."); |
|
529 | - } |
|
530 | - |
|
531 | - if ($this->status === TDBMObjectStateEnum::STATE_DELETED) { |
|
532 | - throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.'); |
|
533 | - } |
|
534 | - |
|
535 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
536 | - } |
|
537 | - |
|
538 | - /** |
|
539 | - * Method used internally by TDBM. You should not use it directly. |
|
540 | - * This method returns the status of the TDBMObject. |
|
541 | - * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
542 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
543 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
544 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
545 | - * |
|
546 | - * @return string |
|
547 | - */ |
|
548 | - public function _getStatus() |
|
549 | - { |
|
550 | - return $this->status; |
|
551 | - } |
|
552 | - |
|
553 | - /** |
|
554 | - * Override the native php clone function for TDBMObjects. |
|
555 | - */ |
|
556 | - public function __clone() |
|
557 | - { |
|
558 | - // Let's clone the many to many relationships |
|
559 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
560 | - $pivotTableList = array_keys($this->relationships); |
|
561 | - } else { |
|
562 | - $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this); |
|
563 | - } |
|
564 | - |
|
565 | - foreach ($pivotTableList as $pivotTable) { |
|
566 | - $storage = $this->retrieveRelationshipsStorage($pivotTable); |
|
567 | - |
|
568 | - // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!! |
|
569 | - /*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 = array(); |
|
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 = array(), 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 | + |
|
185 | + /** |
|
186 | + * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null" |
|
187 | + * or throws an error. |
|
188 | + * |
|
189 | + * @param string $tableName |
|
190 | + * |
|
191 | + * @return string |
|
192 | + */ |
|
193 | + private function checkTableName($tableName = null) |
|
194 | + { |
|
195 | + if ($tableName === null) { |
|
196 | + if (count($this->dbRows) > 1) { |
|
197 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
198 | + } elseif (count($this->dbRows) === 1) { |
|
199 | + $tableName = array_keys($this->dbRows)[0]; |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + if (!isset($this->dbRows[$tableName])) { |
|
204 | + if (count($this->dbRows === 0)) { |
|
205 | + throw new TDBMException('Object is not yet bound to any table.'); |
|
206 | + } else { |
|
207 | + throw new TDBMException('Unknown table "'.$tableName.'"" in object.'); |
|
208 | + } |
|
209 | + } |
|
210 | + |
|
211 | + return $tableName; |
|
212 | + } |
|
213 | + |
|
214 | + protected function get($var, $tableName = null) |
|
215 | + { |
|
216 | + $tableName = $this->checkTableName($tableName); |
|
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 | + return $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Adds a many to many relationship to this bean. |
|
292 | + * |
|
293 | + * @param string $pivotTableName |
|
294 | + * @param AbstractTDBMObject $remoteBean |
|
295 | + */ |
|
296 | + protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
297 | + { |
|
298 | + $this->setRelationship($pivotTableName, $remoteBean, 'new'); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Returns true if there is a relationship to this bean. |
|
303 | + * |
|
304 | + * @param string $pivotTableName |
|
305 | + * @param AbstractTDBMObject $remoteBean |
|
306 | + * |
|
307 | + * @return bool |
|
308 | + */ |
|
309 | + protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
310 | + { |
|
311 | + $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
312 | + |
|
313 | + if ($storage->contains($remoteBean)) { |
|
314 | + if ($storage[$remoteBean]['status'] !== 'delete') { |
|
315 | + return true; |
|
316 | + } |
|
317 | + } |
|
318 | + |
|
319 | + return false; |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Internal TDBM method. Removes a many to many relationship from this bean. |
|
324 | + * |
|
325 | + * @param string $pivotTableName |
|
326 | + * @param AbstractTDBMObject $remoteBean |
|
327 | + */ |
|
328 | + public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
329 | + { |
|
330 | + if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') { |
|
331 | + unset($this->relationships[$pivotTableName][$remoteBean]); |
|
332 | + unset($remoteBean->relationships[$pivotTableName][$this]); |
|
333 | + } else { |
|
334 | + $this->setRelationship($pivotTableName, $remoteBean, 'delete'); |
|
335 | + } |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * Sets many to many relationships for this bean. |
|
340 | + * Adds new relationships and removes unused ones. |
|
341 | + * |
|
342 | + * @param $pivotTableName |
|
343 | + * @param array $remoteBeans |
|
344 | + */ |
|
345 | + protected function setRelationships($pivotTableName, array $remoteBeans) |
|
346 | + { |
|
347 | + $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
348 | + |
|
349 | + foreach ($storage as $oldRemoteBean) { |
|
350 | + if (!in_array($oldRemoteBean, $remoteBeans, true)) { |
|
351 | + // $oldRemoteBean must be removed |
|
352 | + $this->_removeRelationship($pivotTableName, $oldRemoteBean); |
|
353 | + } |
|
354 | + } |
|
355 | + |
|
356 | + foreach ($remoteBeans as $remoteBean) { |
|
357 | + if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') { |
|
358 | + // $remoteBean must be added |
|
359 | + $this->addRelationship($pivotTableName, $remoteBean); |
|
360 | + } |
|
361 | + } |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * Returns the list of objects linked to this bean via $pivotTableName. |
|
366 | + * |
|
367 | + * @param $pivotTableName |
|
368 | + * |
|
369 | + * @return \SplObjectStorage |
|
370 | + */ |
|
371 | + private function retrieveRelationshipsStorage($pivotTableName) |
|
372 | + { |
|
373 | + $storage = $this->getRelationshipStorage($pivotTableName); |
|
374 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) { |
|
375 | + return $storage; |
|
376 | + } |
|
377 | + |
|
378 | + $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this); |
|
379 | + $this->loadedRelationships[$pivotTableName] = true; |
|
380 | + |
|
381 | + foreach ($beans as $bean) { |
|
382 | + if (isset($storage[$bean])) { |
|
383 | + $oldStatus = $storage[$bean]['status']; |
|
384 | + if ($oldStatus === 'delete') { |
|
385 | + // Keep deleted things deleted |
|
386 | + continue; |
|
387 | + } |
|
388 | + } |
|
389 | + $this->setRelationship($pivotTableName, $bean, 'loaded'); |
|
390 | + } |
|
391 | + |
|
392 | + return $storage; |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
|
397 | + * |
|
398 | + * @param $pivotTableName |
|
399 | + * |
|
400 | + * @return AbstractTDBMObject[] |
|
401 | + */ |
|
402 | + public function _getRelationships($pivotTableName) |
|
403 | + { |
|
404 | + return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName)); |
|
405 | + } |
|
406 | + |
|
407 | + private function relationshipStorageToArray(\SplObjectStorage $storage) |
|
408 | + { |
|
409 | + $beans = []; |
|
410 | + foreach ($storage as $bean) { |
|
411 | + $statusArr = $storage[$bean]; |
|
412 | + if ($statusArr['status'] !== 'delete') { |
|
413 | + $beans[] = $bean; |
|
414 | + } |
|
415 | + } |
|
416 | + |
|
417 | + return $beans; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Declares a relationship between. |
|
422 | + * |
|
423 | + * @param string $pivotTableName |
|
424 | + * @param AbstractTDBMObject $remoteBean |
|
425 | + * @param string $status |
|
426 | + */ |
|
427 | + private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
|
428 | + { |
|
429 | + $storage = $this->getRelationshipStorage($pivotTableName); |
|
430 | + $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
431 | + if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
432 | + $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
|
433 | + } |
|
434 | + |
|
435 | + $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
|
436 | + $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
441 | + * |
|
442 | + * @param string $pivotTableName |
|
443 | + * |
|
444 | + * @return \SplObjectStorage |
|
445 | + */ |
|
446 | + private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage |
|
447 | + { |
|
448 | + return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage(); |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
453 | + * |
|
454 | + * @param string $tableName |
|
455 | + * @param string $foreignKeyName |
|
456 | + * |
|
457 | + * @return AlterableResultIterator |
|
458 | + */ |
|
459 | + private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator |
|
460 | + { |
|
461 | + $key = $tableName.'___'.$foreignKeyName; |
|
462 | + |
|
463 | + return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator(); |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * Declares a relationship between this bean and the bean pointing to it. |
|
468 | + * |
|
469 | + * @param string $tableName |
|
470 | + * @param string $foreignKeyName |
|
471 | + * @param AbstractTDBMObject $remoteBean |
|
472 | + */ |
|
473 | + private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
474 | + { |
|
475 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
476 | + $alterableResultIterator->add($remoteBean); |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Declares a relationship between this bean and the bean pointing to it. |
|
481 | + * |
|
482 | + * @param string $tableName |
|
483 | + * @param string $foreignKeyName |
|
484 | + * @param AbstractTDBMObject $remoteBean |
|
485 | + */ |
|
486 | + private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
487 | + { |
|
488 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
489 | + $alterableResultIterator->remove($remoteBean); |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Returns the list of objects linked to this bean via a given foreign key. |
|
494 | + * |
|
495 | + * @param string $tableName |
|
496 | + * @param string $foreignKeyName |
|
497 | + * @param string $searchTableName |
|
498 | + * @param array $searchFilter |
|
499 | + * @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 ! |
|
500 | + * |
|
501 | + * @return AlterableResultIterator |
|
502 | + */ |
|
503 | + protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator |
|
504 | + { |
|
505 | + $key = $tableName.'___'.$foreignKeyName; |
|
506 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
507 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) { |
|
508 | + return $alterableResultIterator; |
|
509 | + } |
|
510 | + |
|
511 | + $unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString); |
|
512 | + |
|
513 | + $alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator()); |
|
514 | + |
|
515 | + return $alterableResultIterator; |
|
516 | + } |
|
517 | + |
|
518 | + /** |
|
519 | + * Reverts any changes made to the object and resumes it to its DB state. |
|
520 | + * This can only be called on objects that come from database and that have not been deleted. |
|
521 | + * Otherwise, this will throw an exception. |
|
522 | + * |
|
523 | + * @throws TDBMException |
|
524 | + */ |
|
525 | + public function discardChanges() |
|
526 | + { |
|
527 | + if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
528 | + 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."); |
|
529 | + } |
|
530 | + |
|
531 | + if ($this->status === TDBMObjectStateEnum::STATE_DELETED) { |
|
532 | + throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.'); |
|
533 | + } |
|
534 | + |
|
535 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
536 | + } |
|
537 | + |
|
538 | + /** |
|
539 | + * Method used internally by TDBM. You should not use it directly. |
|
540 | + * This method returns the status of the TDBMObject. |
|
541 | + * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
542 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
543 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
544 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
545 | + * |
|
546 | + * @return string |
|
547 | + */ |
|
548 | + public function _getStatus() |
|
549 | + { |
|
550 | + return $this->status; |
|
551 | + } |
|
552 | + |
|
553 | + /** |
|
554 | + * Override the native php clone function for TDBMObjects. |
|
555 | + */ |
|
556 | + public function __clone() |
|
557 | + { |
|
558 | + // Let's clone the many to many relationships |
|
559 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
560 | + $pivotTableList = array_keys($this->relationships); |
|
561 | + } else { |
|
562 | + $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this); |
|
563 | + } |
|
564 | + |
|
565 | + foreach ($pivotTableList as $pivotTable) { |
|
566 | + $storage = $this->retrieveRelationshipsStorage($pivotTable); |
|
567 | + |
|
568 | + // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!! |
|
569 | + /*foreach ($storage as $remoteBean) { |
|
570 | 570 | $metadata = $storage[$remoteBean]; |
571 | 571 | |
572 | 572 | $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable); |
573 | 573 | $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]); |
574 | 574 | }*/ |
575 | - } |
|
576 | - |
|
577 | - // Let's clone each row |
|
578 | - foreach ($this->dbRows as $key => &$dbRow) { |
|
579 | - $dbRow = clone $dbRow; |
|
580 | - $dbRow->setTDBMObject($this); |
|
581 | - } |
|
582 | - |
|
583 | - $this->manyToOneRelationships = []; |
|
584 | - |
|
585 | - // Let's set the status to new (to enter the save function) |
|
586 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
587 | - } |
|
588 | - |
|
589 | - /** |
|
590 | - * Returns raw database rows. |
|
591 | - * |
|
592 | - * @return DbRow[] Key: table name, Value: DbRow object |
|
593 | - */ |
|
594 | - public function _getDbRows() |
|
595 | - { |
|
596 | - return $this->dbRows; |
|
597 | - } |
|
598 | - |
|
599 | - private function registerTable($tableName) |
|
600 | - { |
|
601 | - $dbRow = new DbRow($this, $tableName); |
|
602 | - |
|
603 | - if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
604 | - // Let's get the primary key for the new table |
|
605 | - $anotherDbRow = array_values($this->dbRows)[0]; |
|
606 | - /* @var $anotherDbRow DbRow */ |
|
607 | - $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys()); |
|
608 | - $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys); |
|
609 | - $dbRow->_setPrimaryKeys($primaryKeys); |
|
610 | - } |
|
611 | - |
|
612 | - $dbRow->_setStatus($this->status); |
|
613 | - |
|
614 | - $this->dbRows[$tableName] = $dbRow; |
|
615 | - // TODO: look at status (if not new)=> get primary key from tdbmservice |
|
616 | - } |
|
617 | - |
|
618 | - /** |
|
619 | - * Internal function: return the list of relationships. |
|
620 | - * |
|
621 | - * @return \SplObjectStorage[] |
|
622 | - */ |
|
623 | - public function _getCachedRelationships() |
|
624 | - { |
|
625 | - return $this->relationships; |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Returns an array of used tables by this bean (from parent to child relationship). |
|
630 | - * |
|
631 | - * @return string[] |
|
632 | - */ |
|
633 | - abstract protected function getUsedTables(); |
|
575 | + } |
|
576 | + |
|
577 | + // Let's clone each row |
|
578 | + foreach ($this->dbRows as $key => &$dbRow) { |
|
579 | + $dbRow = clone $dbRow; |
|
580 | + $dbRow->setTDBMObject($this); |
|
581 | + } |
|
582 | + |
|
583 | + $this->manyToOneRelationships = []; |
|
584 | + |
|
585 | + // Let's set the status to new (to enter the save function) |
|
586 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
587 | + } |
|
588 | + |
|
589 | + /** |
|
590 | + * Returns raw database rows. |
|
591 | + * |
|
592 | + * @return DbRow[] Key: table name, Value: DbRow object |
|
593 | + */ |
|
594 | + public function _getDbRows() |
|
595 | + { |
|
596 | + return $this->dbRows; |
|
597 | + } |
|
598 | + |
|
599 | + private function registerTable($tableName) |
|
600 | + { |
|
601 | + $dbRow = new DbRow($this, $tableName); |
|
602 | + |
|
603 | + if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
604 | + // Let's get the primary key for the new table |
|
605 | + $anotherDbRow = array_values($this->dbRows)[0]; |
|
606 | + /* @var $anotherDbRow DbRow */ |
|
607 | + $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys()); |
|
608 | + $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys); |
|
609 | + $dbRow->_setPrimaryKeys($primaryKeys); |
|
610 | + } |
|
611 | + |
|
612 | + $dbRow->_setStatus($this->status); |
|
613 | + |
|
614 | + $this->dbRows[$tableName] = $dbRow; |
|
615 | + // TODO: look at status (if not new)=> get primary key from tdbmservice |
|
616 | + } |
|
617 | + |
|
618 | + /** |
|
619 | + * Internal function: return the list of relationships. |
|
620 | + * |
|
621 | + * @return \SplObjectStorage[] |
|
622 | + */ |
|
623 | + public function _getCachedRelationships() |
|
624 | + { |
|
625 | + return $this->relationships; |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * Returns an array of used tables by this bean (from parent to child relationship). |
|
630 | + * |
|
631 | + * @return string[] |
|
632 | + */ |
|
633 | + abstract protected function getUsedTables(); |
|
634 | 634 | } |