@@ -4,36 +4,36 @@ |
||
4 | 4 | |
5 | 5 | interface MethodDescriptorInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * Returns the name of the method to be generated. |
|
9 | - * |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - public function getName() : string; |
|
7 | + /** |
|
8 | + * Returns the name of the method to be generated. |
|
9 | + * |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + public function getName() : string; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Requests the use of an alternative name for this method. |
|
16 | - */ |
|
17 | - public function useAlternativeName(); |
|
14 | + /** |
|
15 | + * Requests the use of an alternative name for this method. |
|
16 | + */ |
|
17 | + public function useAlternativeName(); |
|
18 | 18 | |
19 | - /** |
|
20 | - * Returns the code of the method. |
|
21 | - * |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public function getCode() : string; |
|
19 | + /** |
|
20 | + * Returns the code of the method. |
|
21 | + * |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public function getCode() : string; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Returns an array of classes that needs a "use" for this method. |
|
28 | - * |
|
29 | - * @return string[] |
|
30 | - */ |
|
31 | - public function getUsedClasses() : array; |
|
26 | + /** |
|
27 | + * Returns an array of classes that needs a "use" for this method. |
|
28 | + * |
|
29 | + * @return string[] |
|
30 | + */ |
|
31 | + public function getUsedClasses() : array; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Returns the code to past in jsonSerialize. |
|
35 | - * |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function getJsonSerializeCode() : string; |
|
33 | + /** |
|
34 | + * Returns the code to past in jsonSerialize. |
|
35 | + * |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function getJsonSerializeCode() : string; |
|
39 | 39 | } |
@@ -12,65 +12,65 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class DirectForeignKeyMethodDescriptor implements MethodDescriptorInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * @var ForeignKeyConstraint |
|
17 | - */ |
|
18 | - private $fk; |
|
19 | - |
|
20 | - private $useAlternateName = false; |
|
21 | - /** |
|
22 | - * @var Table |
|
23 | - */ |
|
24 | - private $mainTable; |
|
25 | - |
|
26 | - /** |
|
27 | - * @param ForeignKeyConstraint $fk The foreign key pointing to our bean |
|
28 | - * @param Table $mainTable The main table that is pointed to |
|
29 | - */ |
|
30 | - public function __construct(ForeignKeyConstraint $fk, Table $mainTable) |
|
31 | - { |
|
32 | - $this->fk = $fk; |
|
33 | - $this->mainTable = $mainTable; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Returns the name of the method to be generated. |
|
38 | - * |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function getName() : string |
|
42 | - { |
|
43 | - if (!$this->useAlternateName) { |
|
44 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()); |
|
45 | - } else { |
|
46 | - $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By'; |
|
47 | - |
|
48 | - $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns()); |
|
49 | - |
|
50 | - $methodName .= implode('And', $camelizedColumns); |
|
51 | - |
|
52 | - return $methodName; |
|
53 | - } |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Requests the use of an alternative name for this method. |
|
58 | - */ |
|
59 | - public function useAlternativeName() |
|
60 | - { |
|
61 | - $this->useAlternateName = true; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Returns the code of the method. |
|
66 | - * |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function getCode() : string |
|
70 | - { |
|
71 | - $code = ''; |
|
72 | - |
|
73 | - $getterCode = ' /** |
|
15 | + /** |
|
16 | + * @var ForeignKeyConstraint |
|
17 | + */ |
|
18 | + private $fk; |
|
19 | + |
|
20 | + private $useAlternateName = false; |
|
21 | + /** |
|
22 | + * @var Table |
|
23 | + */ |
|
24 | + private $mainTable; |
|
25 | + |
|
26 | + /** |
|
27 | + * @param ForeignKeyConstraint $fk The foreign key pointing to our bean |
|
28 | + * @param Table $mainTable The main table that is pointed to |
|
29 | + */ |
|
30 | + public function __construct(ForeignKeyConstraint $fk, Table $mainTable) |
|
31 | + { |
|
32 | + $this->fk = $fk; |
|
33 | + $this->mainTable = $mainTable; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Returns the name of the method to be generated. |
|
38 | + * |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function getName() : string |
|
42 | + { |
|
43 | + if (!$this->useAlternateName) { |
|
44 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()); |
|
45 | + } else { |
|
46 | + $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By'; |
|
47 | + |
|
48 | + $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns()); |
|
49 | + |
|
50 | + $methodName .= implode('And', $camelizedColumns); |
|
51 | + |
|
52 | + return $methodName; |
|
53 | + } |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Requests the use of an alternative name for this method. |
|
58 | + */ |
|
59 | + public function useAlternativeName() |
|
60 | + { |
|
61 | + $this->useAlternateName = true; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Returns the code of the method. |
|
66 | + * |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function getCode() : string |
|
70 | + { |
|
71 | + $code = ''; |
|
72 | + |
|
73 | + $getterCode = ' /** |
|
74 | 74 | * Returns the list of %s pointing to this bean via the %s column. |
75 | 75 | * |
76 | 76 | * @return %s[]|AlterableResultIterator |
@@ -82,55 +82,55 @@ discard block |
||
82 | 82 | |
83 | 83 | '; |
84 | 84 | |
85 | - $beanClass = TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getLocalTableName()); |
|
86 | - $code .= sprintf($getterCode, |
|
87 | - $beanClass, |
|
88 | - implode(', ', $this->fk->getColumns()), |
|
89 | - $beanClass, |
|
90 | - $this->getName(), |
|
91 | - var_export($this->fk->getLocalTableName(), true), |
|
92 | - var_export($this->fk->getName(), true), |
|
93 | - var_export($this->fk->getLocalTableName(), true), |
|
94 | - $this->getFilters($this->fk) |
|
95 | - ); |
|
96 | - |
|
97 | - return $code; |
|
98 | - } |
|
99 | - |
|
100 | - private function getFilters(ForeignKeyConstraint $fk) : string |
|
101 | - { |
|
102 | - $counter = 0; |
|
103 | - $parameters = []; |
|
104 | - |
|
105 | - $pkColumns = $this->mainTable->getPrimaryKeyColumns(); |
|
106 | - |
|
107 | - foreach ($fk->getLocalColumns() as $columnName) { |
|
108 | - $pkColumn = $pkColumns[$counter]; |
|
109 | - $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true)); |
|
110 | - ++$counter; |
|
111 | - } |
|
112 | - $parametersCode = '['.implode(', ', $parameters).']'; |
|
113 | - |
|
114 | - return $parametersCode; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Returns an array of classes that needs a "use" for this method. |
|
119 | - * |
|
120 | - * @return string[] |
|
121 | - */ |
|
122 | - public function getUsedClasses() : array |
|
123 | - { |
|
124 | - return [TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getForeignTableName())]; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Returns the code to past in jsonSerialize. |
|
129 | - * |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - public function getJsonSerializeCode() : string |
|
133 | - { |
|
134 | - return ''; |
|
135 | - } |
|
85 | + $beanClass = TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getLocalTableName()); |
|
86 | + $code .= sprintf($getterCode, |
|
87 | + $beanClass, |
|
88 | + implode(', ', $this->fk->getColumns()), |
|
89 | + $beanClass, |
|
90 | + $this->getName(), |
|
91 | + var_export($this->fk->getLocalTableName(), true), |
|
92 | + var_export($this->fk->getName(), true), |
|
93 | + var_export($this->fk->getLocalTableName(), true), |
|
94 | + $this->getFilters($this->fk) |
|
95 | + ); |
|
96 | + |
|
97 | + return $code; |
|
98 | + } |
|
99 | + |
|
100 | + private function getFilters(ForeignKeyConstraint $fk) : string |
|
101 | + { |
|
102 | + $counter = 0; |
|
103 | + $parameters = []; |
|
104 | + |
|
105 | + $pkColumns = $this->mainTable->getPrimaryKeyColumns(); |
|
106 | + |
|
107 | + foreach ($fk->getLocalColumns() as $columnName) { |
|
108 | + $pkColumn = $pkColumns[$counter]; |
|
109 | + $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true)); |
|
110 | + ++$counter; |
|
111 | + } |
|
112 | + $parametersCode = '['.implode(', ', $parameters).']'; |
|
113 | + |
|
114 | + return $parametersCode; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Returns an array of classes that needs a "use" for this method. |
|
119 | + * |
|
120 | + * @return string[] |
|
121 | + */ |
|
122 | + public function getUsedClasses() : array |
|
123 | + { |
|
124 | + return [TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getForeignTableName())]; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Returns the code to past in jsonSerialize. |
|
129 | + * |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + public function getJsonSerializeCode() : string |
|
133 | + { |
|
134 | + return ''; |
|
135 | + } |
|
136 | 136 | } |
@@ -7,99 +7,99 @@ discard block |
||
7 | 7 | |
8 | 8 | class PivotTableMethodsDescriptor implements MethodDescriptorInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * @var Table |
|
12 | - */ |
|
13 | - private $pivotTable; |
|
14 | - |
|
15 | - private $useAlternateName = false; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var ForeignKeyConstraint |
|
19 | - */ |
|
20 | - private $localFk; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var ForeignKeyConstraint |
|
24 | - */ |
|
25 | - private $remoteFk; |
|
26 | - |
|
27 | - /** |
|
28 | - * @param Table $pivotTable The pivot table |
|
29 | - * @param ForeignKeyConstraint $localFk |
|
30 | - * @param ForeignKeyConstraint $remoteFk |
|
31 | - */ |
|
32 | - public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk) |
|
33 | - { |
|
34 | - $this->pivotTable = $pivotTable; |
|
35 | - $this->localFk = $localFk; |
|
36 | - $this->remoteFk = $remoteFk; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Requests the use of an alternative name for this method. |
|
41 | - */ |
|
42 | - public function useAlternativeName() |
|
43 | - { |
|
44 | - $this->useAlternateName = true; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Returns the name of the method to be generated. |
|
49 | - * |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function getName() : string |
|
53 | - { |
|
54 | - if (!$this->useAlternateName) { |
|
55 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
56 | - } else { |
|
57 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Returns the plural name. |
|
63 | - * |
|
64 | - * @return string |
|
65 | - */ |
|
66 | - private function getPluralName() : string |
|
67 | - { |
|
68 | - if (!$this->useAlternateName) { |
|
69 | - return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
70 | - } else { |
|
71 | - return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Returns the singular name. |
|
77 | - * |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - private function getSingularName() : string |
|
81 | - { |
|
82 | - if (!$this->useAlternateName) { |
|
83 | - return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())); |
|
84 | - } else { |
|
85 | - return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Returns the code of the method. |
|
91 | - * |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public function getCode() : string |
|
95 | - { |
|
96 | - $singularName = $this->getSingularName(); |
|
97 | - $pluralName = $this->getPluralName(); |
|
98 | - $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName()); |
|
99 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
100 | - $pluralVariableName = $variableName.'s'; |
|
101 | - |
|
102 | - $str = ' /** |
|
10 | + /** |
|
11 | + * @var Table |
|
12 | + */ |
|
13 | + private $pivotTable; |
|
14 | + |
|
15 | + private $useAlternateName = false; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var ForeignKeyConstraint |
|
19 | + */ |
|
20 | + private $localFk; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var ForeignKeyConstraint |
|
24 | + */ |
|
25 | + private $remoteFk; |
|
26 | + |
|
27 | + /** |
|
28 | + * @param Table $pivotTable The pivot table |
|
29 | + * @param ForeignKeyConstraint $localFk |
|
30 | + * @param ForeignKeyConstraint $remoteFk |
|
31 | + */ |
|
32 | + public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk) |
|
33 | + { |
|
34 | + $this->pivotTable = $pivotTable; |
|
35 | + $this->localFk = $localFk; |
|
36 | + $this->remoteFk = $remoteFk; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Requests the use of an alternative name for this method. |
|
41 | + */ |
|
42 | + public function useAlternativeName() |
|
43 | + { |
|
44 | + $this->useAlternateName = true; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Returns the name of the method to be generated. |
|
49 | + * |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function getName() : string |
|
53 | + { |
|
54 | + if (!$this->useAlternateName) { |
|
55 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
56 | + } else { |
|
57 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Returns the plural name. |
|
63 | + * |
|
64 | + * @return string |
|
65 | + */ |
|
66 | + private function getPluralName() : string |
|
67 | + { |
|
68 | + if (!$this->useAlternateName) { |
|
69 | + return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
70 | + } else { |
|
71 | + return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Returns the singular name. |
|
77 | + * |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + private function getSingularName() : string |
|
81 | + { |
|
82 | + if (!$this->useAlternateName) { |
|
83 | + return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())); |
|
84 | + } else { |
|
85 | + return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Returns the code of the method. |
|
91 | + * |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public function getCode() : string |
|
95 | + { |
|
96 | + $singularName = $this->getSingularName(); |
|
97 | + $pluralName = $this->getPluralName(); |
|
98 | + $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName()); |
|
99 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
100 | + $pluralVariableName = $variableName.'s'; |
|
101 | + |
|
102 | + $str = ' /** |
|
103 | 103 | * Returns the list of %s associated to this bean via the %s pivot table. |
104 | 104 | * |
105 | 105 | * @return %s[] |
@@ -110,9 +110,9 @@ discard block |
||
110 | 110 | } |
111 | 111 | '; |
112 | 112 | |
113 | - $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true)); |
|
113 | + $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true)); |
|
114 | 114 | |
115 | - $str = ' /** |
|
115 | + $str = ' /** |
|
116 | 116 | * Adds a relationship with %s associated to this bean via the %s pivot table. |
117 | 117 | * |
118 | 118 | * @param %s %s |
@@ -123,9 +123,9 @@ discard block |
||
123 | 123 | } |
124 | 124 | '; |
125 | 125 | |
126 | - $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
126 | + $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
127 | 127 | |
128 | - $str = ' /** |
|
128 | + $str = ' /** |
|
129 | 129 | * Deletes the relationship with %s associated to this bean via the %s pivot table. |
130 | 130 | * |
131 | 131 | * @param %s %s |
@@ -136,9 +136,9 @@ discard block |
||
136 | 136 | } |
137 | 137 | '; |
138 | 138 | |
139 | - $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
139 | + $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
140 | 140 | |
141 | - $str = ' /** |
|
141 | + $str = ' /** |
|
142 | 142 | * Returns whether this bean is associated with %s via the %s pivot table. |
143 | 143 | * |
144 | 144 | * @param %s %s |
@@ -150,9 +150,9 @@ discard block |
||
150 | 150 | } |
151 | 151 | '; |
152 | 152 | |
153 | - $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
153 | + $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
154 | 154 | |
155 | - $str = ' /** |
|
155 | + $str = ' /** |
|
156 | 156 | * Sets all relationships with %s associated to this bean via the %s pivot table. |
157 | 157 | * Exiting relationships will be removed and replaced by the provided relationships. |
158 | 158 | * |
@@ -164,38 +164,38 @@ discard block |
||
164 | 164 | } |
165 | 165 | '; |
166 | 166 | |
167 | - $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName); |
|
168 | - |
|
169 | - $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
170 | - |
|
171 | - return $code; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Returns an array of classes that needs a "use" for this method. |
|
176 | - * |
|
177 | - * @return string[] |
|
178 | - */ |
|
179 | - public function getUsedClasses() : array |
|
180 | - { |
|
181 | - return [TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName())]; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Returns the code to past in jsonSerialize. |
|
186 | - * |
|
187 | - * @return string |
|
188 | - */ |
|
189 | - public function getJsonSerializeCode() : string |
|
190 | - { |
|
191 | - $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName()); |
|
192 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
193 | - |
|
194 | - return ' if (!$stopRecursion) { |
|
167 | + $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName); |
|
168 | + |
|
169 | + $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
170 | + |
|
171 | + return $code; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Returns an array of classes that needs a "use" for this method. |
|
176 | + * |
|
177 | + * @return string[] |
|
178 | + */ |
|
179 | + public function getUsedClasses() : array |
|
180 | + { |
|
181 | + return [TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName())]; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Returns the code to past in jsonSerialize. |
|
186 | + * |
|
187 | + * @return string |
|
188 | + */ |
|
189 | + public function getJsonSerializeCode() : string |
|
190 | + { |
|
191 | + $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName()); |
|
192 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
193 | + |
|
194 | + return ' if (!$stopRecursion) { |
|
195 | 195 | $array[\''.lcfirst($this->getPluralName()).'\'] = array_map(function ('.$remoteBeanName.' '.$variableName.') { |
196 | 196 | return '.$variableName.'->jsonSerialize(true); |
197 | 197 | }, $this->'.$this->getName().'()); |
198 | 198 | } |
199 | 199 | '; |
200 | - } |
|
200 | + } |
|
201 | 201 | } |
@@ -14,274 +14,274 @@ |
||
14 | 14 | */ |
15 | 15 | class AlterableResultIterator implements Result, \ArrayAccess, \JsonSerializable |
16 | 16 | { |
17 | - /** |
|
18 | - * @var \Iterator|null |
|
19 | - */ |
|
20 | - private $resultIterator; |
|
17 | + /** |
|
18 | + * @var \Iterator|null |
|
19 | + */ |
|
20 | + private $resultIterator; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Key: the object to alter in the result set. |
|
24 | - * Value: "add" => the object will be added to the resultset (if it is not found in it) |
|
25 | - * "delete" => the object will be removed from the resultset (if found). |
|
26 | - * |
|
27 | - * @var \SplObjectStorage |
|
28 | - */ |
|
29 | - private $alterations; |
|
22 | + /** |
|
23 | + * Key: the object to alter in the result set. |
|
24 | + * Value: "add" => the object will be added to the resultset (if it is not found in it) |
|
25 | + * "delete" => the object will be removed from the resultset (if found). |
|
26 | + * |
|
27 | + * @var \SplObjectStorage |
|
28 | + */ |
|
29 | + private $alterations; |
|
30 | 30 | |
31 | - /** |
|
32 | - * The result array from the result set. |
|
33 | - * |
|
34 | - * @var array|null |
|
35 | - */ |
|
36 | - private $resultArray; |
|
31 | + /** |
|
32 | + * The result array from the result set. |
|
33 | + * |
|
34 | + * @var array|null |
|
35 | + */ |
|
36 | + private $resultArray; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param \Iterator|null $resultIterator |
|
40 | - */ |
|
41 | - public function __construct(\Iterator $resultIterator = null) |
|
42 | - { |
|
43 | - $this->resultIterator = $resultIterator; |
|
44 | - $this->alterations = new \SplObjectStorage(); |
|
45 | - } |
|
38 | + /** |
|
39 | + * @param \Iterator|null $resultIterator |
|
40 | + */ |
|
41 | + public function __construct(\Iterator $resultIterator = null) |
|
42 | + { |
|
43 | + $this->resultIterator = $resultIterator; |
|
44 | + $this->alterations = new \SplObjectStorage(); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Sets a new iterator as the base iterator to be altered. |
|
49 | - * |
|
50 | - * @param \Iterator $resultIterator |
|
51 | - */ |
|
52 | - public function setResultIterator(\Iterator $resultIterator) |
|
53 | - { |
|
54 | - $this->resultIterator = $resultIterator; |
|
55 | - $this->resultArray = null; |
|
56 | - } |
|
47 | + /** |
|
48 | + * Sets a new iterator as the base iterator to be altered. |
|
49 | + * |
|
50 | + * @param \Iterator $resultIterator |
|
51 | + */ |
|
52 | + public function setResultIterator(\Iterator $resultIterator) |
|
53 | + { |
|
54 | + $this->resultIterator = $resultIterator; |
|
55 | + $this->resultArray = null; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Returns the non altered result iterator (or null if none exist). |
|
60 | - * |
|
61 | - * @return \Iterator|null |
|
62 | - */ |
|
63 | - public function getUnderlyingResultIterator() |
|
64 | - { |
|
65 | - return $this->resultIterator; |
|
66 | - } |
|
58 | + /** |
|
59 | + * Returns the non altered result iterator (or null if none exist). |
|
60 | + * |
|
61 | + * @return \Iterator|null |
|
62 | + */ |
|
63 | + public function getUnderlyingResultIterator() |
|
64 | + { |
|
65 | + return $this->resultIterator; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Adds an additional object to the result set (if not already available). |
|
70 | - * |
|
71 | - * @param $object |
|
72 | - */ |
|
73 | - public function add($object) |
|
74 | - { |
|
75 | - $this->alterations->attach($object, 'add'); |
|
68 | + /** |
|
69 | + * Adds an additional object to the result set (if not already available). |
|
70 | + * |
|
71 | + * @param $object |
|
72 | + */ |
|
73 | + public function add($object) |
|
74 | + { |
|
75 | + $this->alterations->attach($object, 'add'); |
|
76 | 76 | |
77 | - if ($this->resultArray !== null) { |
|
78 | - $foundKey = array_search($object, $this->resultArray, true); |
|
79 | - if ($foundKey === false) { |
|
80 | - $this->resultArray[] = $object; |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
77 | + if ($this->resultArray !== null) { |
|
78 | + $foundKey = array_search($object, $this->resultArray, true); |
|
79 | + if ($foundKey === false) { |
|
80 | + $this->resultArray[] = $object; |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Removes an object from the result set. |
|
87 | - * |
|
88 | - * @param $object |
|
89 | - */ |
|
90 | - public function remove($object) |
|
91 | - { |
|
92 | - $this->alterations->attach($object, 'delete'); |
|
85 | + /** |
|
86 | + * Removes an object from the result set. |
|
87 | + * |
|
88 | + * @param $object |
|
89 | + */ |
|
90 | + public function remove($object) |
|
91 | + { |
|
92 | + $this->alterations->attach($object, 'delete'); |
|
93 | 93 | |
94 | - if ($this->resultArray !== null) { |
|
95 | - $foundKey = array_search($object, $this->resultArray, true); |
|
96 | - if ($foundKey !== false) { |
|
97 | - unset($this->resultArray[$foundKey]); |
|
98 | - } |
|
99 | - } |
|
100 | - } |
|
94 | + if ($this->resultArray !== null) { |
|
95 | + $foundKey = array_search($object, $this->resultArray, true); |
|
96 | + if ($foundKey !== false) { |
|
97 | + unset($this->resultArray[$foundKey]); |
|
98 | + } |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Casts the result set to a PHP array. |
|
104 | - * |
|
105 | - * @return array |
|
106 | - */ |
|
107 | - public function toArray() |
|
108 | - { |
|
109 | - if ($this->resultArray === null) { |
|
110 | - if ($this->resultIterator !== null) { |
|
111 | - $this->resultArray = iterator_to_array($this->resultIterator); |
|
112 | - } else { |
|
113 | - $this->resultArray = []; |
|
114 | - } |
|
102 | + /** |
|
103 | + * Casts the result set to a PHP array. |
|
104 | + * |
|
105 | + * @return array |
|
106 | + */ |
|
107 | + public function toArray() |
|
108 | + { |
|
109 | + if ($this->resultArray === null) { |
|
110 | + if ($this->resultIterator !== null) { |
|
111 | + $this->resultArray = iterator_to_array($this->resultIterator); |
|
112 | + } else { |
|
113 | + $this->resultArray = []; |
|
114 | + } |
|
115 | 115 | |
116 | - foreach ($this->alterations as $obj) { |
|
117 | - $action = $this->alterations->getInfo(); // return, if exists, associated with cur. obj. data; else NULL |
|
116 | + foreach ($this->alterations as $obj) { |
|
117 | + $action = $this->alterations->getInfo(); // return, if exists, associated with cur. obj. data; else NULL |
|
118 | 118 | |
119 | - $foundKey = array_search($obj, $this->resultArray, true); |
|
119 | + $foundKey = array_search($obj, $this->resultArray, true); |
|
120 | 120 | |
121 | - if ($action === 'add' && $foundKey === false) { |
|
122 | - $this->resultArray[] = $obj; |
|
123 | - } elseif ($action === 'delete' && $foundKey !== false) { |
|
124 | - unset($this->resultArray[$foundKey]); |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
121 | + if ($action === 'add' && $foundKey === false) { |
|
122 | + $this->resultArray[] = $obj; |
|
123 | + } elseif ($action === 'delete' && $foundKey !== false) { |
|
124 | + unset($this->resultArray[$foundKey]); |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | 128 | |
129 | - return array_values($this->resultArray); |
|
130 | - } |
|
129 | + return array_values($this->resultArray); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Whether a offset exists. |
|
134 | - * |
|
135 | - * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
136 | - * |
|
137 | - * @param mixed $offset <p> |
|
138 | - * An offset to check for. |
|
139 | - * </p> |
|
140 | - * |
|
141 | - * @return bool true on success or false on failure. |
|
142 | - * </p> |
|
143 | - * <p> |
|
144 | - * The return value will be casted to boolean if non-boolean was returned |
|
145 | - * |
|
146 | - * @since 5.0.0 |
|
147 | - */ |
|
148 | - public function offsetExists($offset) |
|
149 | - { |
|
150 | - return isset($this->toArray()[$offset]); |
|
151 | - } |
|
132 | + /** |
|
133 | + * Whether a offset exists. |
|
134 | + * |
|
135 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
136 | + * |
|
137 | + * @param mixed $offset <p> |
|
138 | + * An offset to check for. |
|
139 | + * </p> |
|
140 | + * |
|
141 | + * @return bool true on success or false on failure. |
|
142 | + * </p> |
|
143 | + * <p> |
|
144 | + * The return value will be casted to boolean if non-boolean was returned |
|
145 | + * |
|
146 | + * @since 5.0.0 |
|
147 | + */ |
|
148 | + public function offsetExists($offset) |
|
149 | + { |
|
150 | + return isset($this->toArray()[$offset]); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Offset to retrieve. |
|
155 | - * |
|
156 | - * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
157 | - * |
|
158 | - * @param mixed $offset <p> |
|
159 | - * The offset to retrieve. |
|
160 | - * </p> |
|
161 | - * |
|
162 | - * @return mixed Can return all value types |
|
163 | - * |
|
164 | - * @since 5.0.0 |
|
165 | - */ |
|
166 | - public function offsetGet($offset) |
|
167 | - { |
|
168 | - return $this->toArray()[$offset]; |
|
169 | - } |
|
153 | + /** |
|
154 | + * Offset to retrieve. |
|
155 | + * |
|
156 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
157 | + * |
|
158 | + * @param mixed $offset <p> |
|
159 | + * The offset to retrieve. |
|
160 | + * </p> |
|
161 | + * |
|
162 | + * @return mixed Can return all value types |
|
163 | + * |
|
164 | + * @since 5.0.0 |
|
165 | + */ |
|
166 | + public function offsetGet($offset) |
|
167 | + { |
|
168 | + return $this->toArray()[$offset]; |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * Offset to set. |
|
173 | - * |
|
174 | - * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
175 | - * |
|
176 | - * @param mixed $offset <p> |
|
177 | - * The offset to assign the value to. |
|
178 | - * </p> |
|
179 | - * @param mixed $value <p> |
|
180 | - * The value to set. |
|
181 | - * </p> |
|
182 | - * |
|
183 | - * @since 5.0.0 |
|
184 | - */ |
|
185 | - public function offsetSet($offset, $value) |
|
186 | - { |
|
187 | - throw new TDBMInvalidOperationException('You can set values in a TDBM result set, even in an alterable one. Use the add method instead.'); |
|
188 | - } |
|
171 | + /** |
|
172 | + * Offset to set. |
|
173 | + * |
|
174 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
175 | + * |
|
176 | + * @param mixed $offset <p> |
|
177 | + * The offset to assign the value to. |
|
178 | + * </p> |
|
179 | + * @param mixed $value <p> |
|
180 | + * The value to set. |
|
181 | + * </p> |
|
182 | + * |
|
183 | + * @since 5.0.0 |
|
184 | + */ |
|
185 | + public function offsetSet($offset, $value) |
|
186 | + { |
|
187 | + throw new TDBMInvalidOperationException('You can set values in a TDBM result set, even in an alterable one. Use the add method instead.'); |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * Offset to unset. |
|
192 | - * |
|
193 | - * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
194 | - * |
|
195 | - * @param mixed $offset <p> |
|
196 | - * The offset to unset. |
|
197 | - * </p> |
|
198 | - * |
|
199 | - * @since 5.0.0 |
|
200 | - */ |
|
201 | - public function offsetUnset($offset) |
|
202 | - { |
|
203 | - throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.'); |
|
204 | - } |
|
190 | + /** |
|
191 | + * Offset to unset. |
|
192 | + * |
|
193 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
194 | + * |
|
195 | + * @param mixed $offset <p> |
|
196 | + * The offset to unset. |
|
197 | + * </p> |
|
198 | + * |
|
199 | + * @since 5.0.0 |
|
200 | + */ |
|
201 | + public function offsetUnset($offset) |
|
202 | + { |
|
203 | + throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.'); |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * @param int $offset |
|
208 | - * |
|
209 | - * @return \Porpaginas\Page |
|
210 | - */ |
|
211 | - public function take($offset, $limit) |
|
212 | - { |
|
213 | - // TODO: replace this with a class implementing the map method. |
|
214 | - return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray())); |
|
215 | - } |
|
206 | + /** |
|
207 | + * @param int $offset |
|
208 | + * |
|
209 | + * @return \Porpaginas\Page |
|
210 | + */ |
|
211 | + public function take($offset, $limit) |
|
212 | + { |
|
213 | + // TODO: replace this with a class implementing the map method. |
|
214 | + return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray())); |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Return the number of all results in the paginatable. |
|
219 | - * |
|
220 | - * @return int |
|
221 | - */ |
|
222 | - public function count() |
|
223 | - { |
|
224 | - return count($this->toArray()); |
|
225 | - } |
|
217 | + /** |
|
218 | + * Return the number of all results in the paginatable. |
|
219 | + * |
|
220 | + * @return int |
|
221 | + */ |
|
222 | + public function count() |
|
223 | + { |
|
224 | + return count($this->toArray()); |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * Return an iterator over all results of the paginatable. |
|
229 | - * |
|
230 | - * @return Iterator |
|
231 | - */ |
|
232 | - public function getIterator() |
|
233 | - { |
|
234 | - if ($this->alterations->count() === 0) { |
|
235 | - if ($this->resultIterator !== null) { |
|
236 | - return $this->resultIterator; |
|
237 | - } else { |
|
238 | - return new \ArrayIterator([]); |
|
239 | - } |
|
240 | - } else { |
|
241 | - return new \ArrayIterator($this->toArray()); |
|
242 | - } |
|
243 | - } |
|
227 | + /** |
|
228 | + * Return an iterator over all results of the paginatable. |
|
229 | + * |
|
230 | + * @return Iterator |
|
231 | + */ |
|
232 | + public function getIterator() |
|
233 | + { |
|
234 | + if ($this->alterations->count() === 0) { |
|
235 | + if ($this->resultIterator !== null) { |
|
236 | + return $this->resultIterator; |
|
237 | + } else { |
|
238 | + return new \ArrayIterator([]); |
|
239 | + } |
|
240 | + } else { |
|
241 | + return new \ArrayIterator($this->toArray()); |
|
242 | + } |
|
243 | + } |
|
244 | 244 | |
245 | - /** |
|
246 | - * Specify data which should be serialized to JSON. |
|
247 | - * |
|
248 | - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
249 | - * |
|
250 | - * @return mixed data which can be serialized by <b>json_encode</b>, |
|
251 | - * which is a value of any type other than a resource |
|
252 | - * |
|
253 | - * @since 5.4.0 |
|
254 | - */ |
|
255 | - public function jsonSerialize() |
|
256 | - { |
|
257 | - return $this->toArray(); |
|
258 | - } |
|
245 | + /** |
|
246 | + * Specify data which should be serialized to JSON. |
|
247 | + * |
|
248 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
249 | + * |
|
250 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
|
251 | + * which is a value of any type other than a resource |
|
252 | + * |
|
253 | + * @since 5.4.0 |
|
254 | + */ |
|
255 | + public function jsonSerialize() |
|
256 | + { |
|
257 | + return $this->toArray(); |
|
258 | + } |
|
259 | 259 | |
260 | - /** |
|
261 | - * Returns only one value (the first) of the result set. |
|
262 | - * Returns null if no value exists. |
|
263 | - * |
|
264 | - * @return mixed|null |
|
265 | - */ |
|
266 | - public function first() |
|
267 | - { |
|
268 | - $page = $this->take(0, 1); |
|
269 | - foreach ($page as $bean) { |
|
270 | - return $bean; |
|
271 | - } |
|
260 | + /** |
|
261 | + * Returns only one value (the first) of the result set. |
|
262 | + * Returns null if no value exists. |
|
263 | + * |
|
264 | + * @return mixed|null |
|
265 | + */ |
|
266 | + public function first() |
|
267 | + { |
|
268 | + $page = $this->take(0, 1); |
|
269 | + foreach ($page as $bean) { |
|
270 | + return $bean; |
|
271 | + } |
|
272 | 272 | |
273 | - return; |
|
274 | - } |
|
273 | + return; |
|
274 | + } |
|
275 | 275 | |
276 | - /** |
|
277 | - * Returns a new iterator mapping any call using the $callable function. |
|
278 | - * |
|
279 | - * @param callable $callable |
|
280 | - * |
|
281 | - * @return MapIterator |
|
282 | - */ |
|
283 | - public function map(callable $callable) |
|
284 | - { |
|
285 | - return new MapIterator($this->getIterator(), $callable); |
|
286 | - } |
|
276 | + /** |
|
277 | + * Returns a new iterator mapping any call using the $callable function. |
|
278 | + * |
|
279 | + * @param callable $callable |
|
280 | + * |
|
281 | + * @return MapIterator |
|
282 | + */ |
|
283 | + public function map(callable $callable) |
|
284 | + { |
|
285 | + return new MapIterator($this->getIterator(), $callable); |
|
286 | + } |
|
287 | 287 | } |
@@ -7,23 +7,23 @@ |
||
7 | 7 | */ |
8 | 8 | class TDBMInheritanceException extends TDBMException |
9 | 9 | { |
10 | - public static function create(array $tables) : TDBMInheritanceException |
|
11 | - { |
|
12 | - return new self(sprintf('The tables (%s) cannot be linked by an inheritance relationship. Does your data set contains multiple children for one parent row? (multiple inheritance is not supported by TDBM)', implode(', ', $tables))); |
|
13 | - } |
|
10 | + public static function create(array $tables) : TDBMInheritanceException |
|
11 | + { |
|
12 | + return new self(sprintf('The tables (%s) cannot be linked by an inheritance relationship. Does your data set contains multiple children for one parent row? (multiple inheritance is not supported by TDBM)', implode(', ', $tables))); |
|
13 | + } |
|
14 | 14 | |
15 | - public static function extendException(TDBMInheritanceException $e, TDBMService $tdbmService, array $beanData) : TDBMInheritanceException |
|
16 | - { |
|
17 | - $pks = []; |
|
18 | - foreach ($beanData as $table => $row) { |
|
19 | - $primaryKeyColumns = $tdbmService->getPrimaryKeyColumns($table); |
|
20 | - foreach ($primaryKeyColumns as $columnName) { |
|
21 | - if ($row[$columnName] !== null) { |
|
22 | - $pks[] = $table.'.'.$columnName.' => '.var_export($row[$columnName], true); |
|
23 | - } |
|
24 | - } |
|
25 | - } |
|
15 | + public static function extendException(TDBMInheritanceException $e, TDBMService $tdbmService, array $beanData) : TDBMInheritanceException |
|
16 | + { |
|
17 | + $pks = []; |
|
18 | + foreach ($beanData as $table => $row) { |
|
19 | + $primaryKeyColumns = $tdbmService->getPrimaryKeyColumns($table); |
|
20 | + foreach ($primaryKeyColumns as $columnName) { |
|
21 | + if ($row[$columnName] !== null) { |
|
22 | + $pks[] = $table.'.'.$columnName.' => '.var_export($row[$columnName], true); |
|
23 | + } |
|
24 | + } |
|
25 | + } |
|
26 | 26 | |
27 | - throw new self($e->getMessage().' (row in error: '.implode(', ', $pks).')', 0, $e); |
|
28 | - } |
|
27 | + throw new self($e->getMessage().' (row in error: '.implode(', ', $pks).')', 0, $e); |
|
28 | + } |
|
29 | 29 | } |
@@ -4,13 +4,13 @@ |
||
4 | 4 | |
5 | 5 | class TDBMCyclicReferenceException extends TDBMException |
6 | 6 | { |
7 | - public static function createCyclicReference(string $tableName, AbstractTDBMObject $object) : TDBMCyclicReferenceException |
|
8 | - { |
|
9 | - return new self(sprintf("You are trying a grape of objects that reference each other. Unable to save object '%s' in table '%s'. It is already in the process of being saved.", get_class($object), $tableName)); |
|
10 | - } |
|
7 | + public static function createCyclicReference(string $tableName, AbstractTDBMObject $object) : TDBMCyclicReferenceException |
|
8 | + { |
|
9 | + return new self(sprintf("You are trying a grape of objects that reference each other. Unable to save object '%s' in table '%s'. It is already in the process of being saved.", get_class($object), $tableName)); |
|
10 | + } |
|
11 | 11 | |
12 | - public static function extendCyclicReference(TDBMCyclicReferenceException $e, string $tableName, AbstractTDBMObject $object, string $fkName) : TDBMCyclicReferenceException |
|
13 | - { |
|
14 | - return new self($e->getMessage().sprintf(" This object is referenced by an object of type '%s' (table '%s') via foreign key '%s'", get_class($object), $tableName, $fkName), $e->getCode(), $e); |
|
15 | - } |
|
12 | + public static function extendCyclicReference(TDBMCyclicReferenceException $e, string $tableName, AbstractTDBMObject $object, string $fkName) : TDBMCyclicReferenceException |
|
13 | + { |
|
14 | + return new self($e->getMessage().sprintf(" This object is referenced by an object of type '%s' (table '%s') via foreign key '%s'", get_class($object), $tableName, $fkName), $e->getCode(), $e); |
|
15 | + } |
|
16 | 16 | } |
@@ -4,8 +4,8 @@ |
||
4 | 4 | |
5 | 5 | class TDBMMissingReferenceException extends TDBMException |
6 | 6 | { |
7 | - public static function referenceDeleted(string $tableName, AbstractTDBMObject $reference) : TDBMMissingReferenceException |
|
8 | - { |
|
9 | - return new self(sprintf("Unable to save object in table '%s'. Your object references an object of type '%s' that is deleted.", $tableName, get_class($reference))); |
|
10 | - } |
|
7 | + public static function referenceDeleted(string $tableName, AbstractTDBMObject $reference) : TDBMMissingReferenceException |
|
8 | + { |
|
9 | + return new self(sprintf("Unable to save object in table '%s'. Your object references an object of type '%s' that is deleted.", $tableName, get_class($reference))); |
|
10 | + } |
|
11 | 11 | } |
@@ -27,170 +27,170 @@ discard block |
||
27 | 27 | */ |
28 | 28 | class DbRow |
29 | 29 | { |
30 | - /** |
|
31 | - * The service this object is bound to. |
|
32 | - * |
|
33 | - * @var TDBMService |
|
34 | - */ |
|
35 | - protected $tdbmService; |
|
36 | - |
|
37 | - /** |
|
38 | - * The object containing this db row. |
|
39 | - * |
|
40 | - * @var AbstractTDBMObject |
|
41 | - */ |
|
42 | - private $object; |
|
43 | - |
|
44 | - /** |
|
45 | - * The name of the table the object if issued from. |
|
46 | - * |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - private $dbTableName; |
|
50 | - |
|
51 | - /** |
|
52 | - * The array of columns returned from database. |
|
53 | - * |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - private $dbRow = array(); |
|
57 | - |
|
58 | - /** |
|
59 | - * @var AbstractTDBMObject[] |
|
60 | - */ |
|
61 | - private $references = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
65 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
66 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
67 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
68 | - * |
|
69 | - * @var string |
|
70 | - */ |
|
71 | - private $status; |
|
72 | - |
|
73 | - /** |
|
74 | - * The values of the primary key. |
|
75 | - * This is set when the object is in "loaded" state. |
|
76 | - * |
|
77 | - * @var array An array of column => value |
|
78 | - */ |
|
79 | - private $primaryKeys; |
|
80 | - |
|
81 | - /** |
|
82 | - * You should never call the constructor directly. Instead, you should use the |
|
83 | - * TDBMService class that will create TDBMObjects for you. |
|
84 | - * |
|
85 | - * Used with id!=false when we want to retrieve an existing object |
|
86 | - * and id==false if we want a new object |
|
87 | - * |
|
88 | - * @param AbstractTDBMObject $object The object containing this db row |
|
89 | - * @param string $table_name |
|
90 | - * @param array $primaryKeys |
|
91 | - * @param TDBMService $tdbmService |
|
92 | - * |
|
93 | - * @throws TDBMException |
|
94 | - * @throws TDBMInvalidOperationException |
|
95 | - */ |
|
96 | - public function __construct(AbstractTDBMObject $object, $table_name, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = array()) |
|
97 | - { |
|
98 | - $this->object = $object; |
|
99 | - $this->dbTableName = $table_name; |
|
100 | - |
|
101 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
102 | - |
|
103 | - if ($tdbmService === null) { |
|
104 | - if (!empty($primaryKeys)) { |
|
105 | - throw new TDBMException('You cannot pass an id to the DbRow constructor without passing also a TDBMService.'); |
|
106 | - } |
|
107 | - } else { |
|
108 | - $this->tdbmService = $tdbmService; |
|
109 | - |
|
110 | - if (!empty($primaryKeys)) { |
|
111 | - $this->_setPrimaryKeys($primaryKeys); |
|
112 | - if (!empty($dbRow)) { |
|
113 | - $this->dbRow = $dbRow; |
|
114 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
115 | - } else { |
|
116 | - $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
117 | - } |
|
118 | - $tdbmService->_addToCache($this); |
|
119 | - } else { |
|
120 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
121 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
122 | - } |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - public function _attach(TDBMService $tdbmService) |
|
127 | - { |
|
128 | - if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
129 | - throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
130 | - } |
|
131 | - $this->tdbmService = $tdbmService; |
|
132 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
133 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Sets the state of the TDBM Object |
|
138 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
139 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
140 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
141 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
142 | - * |
|
143 | - * @param string $state |
|
144 | - */ |
|
145 | - public function _setStatus($state) |
|
146 | - { |
|
147 | - $this->status = $state; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * This is an internal method. You should not call this method yourself. The TDBM library will do it for you. |
|
152 | - * If the object is in state 'not loaded', this method performs a query in database to load the object. |
|
153 | - * |
|
154 | - * A TDBMException is thrown is no object can be retrieved (for instance, if the primary key specified |
|
155 | - * cannot be found). |
|
156 | - */ |
|
157 | - public function _dbLoadIfNotLoaded() |
|
158 | - { |
|
159 | - if ($this->status == TDBMObjectStateEnum::STATE_NOT_LOADED) { |
|
160 | - $connection = $this->tdbmService->getConnection(); |
|
161 | - |
|
162 | - /// buildFilterFromFilterBag($filter_bag) |
|
163 | - list($sql_where, $parameters) = $this->tdbmService->buildFilterFromFilterBag($this->primaryKeys); |
|
164 | - |
|
165 | - $sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where; |
|
166 | - $result = $connection->executeQuery($sql, $parameters); |
|
167 | - |
|
168 | - if ($result->rowCount() === 0) { |
|
169 | - throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \"\"."); |
|
170 | - } |
|
171 | - |
|
172 | - $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
173 | - |
|
174 | - $this->dbRow = []; |
|
175 | - $types = $this->tdbmService->_getColumnTypesForTable($this->dbTableName); |
|
176 | - |
|
177 | - foreach ($row as $key => $value) { |
|
178 | - $this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform()); |
|
179 | - } |
|
180 | - |
|
181 | - $result->closeCursor(); |
|
182 | - |
|
183 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - public function get($var) |
|
188 | - { |
|
189 | - $this->_dbLoadIfNotLoaded(); |
|
190 | - |
|
191 | - // Let's first check if the key exist. |
|
192 | - if (!isset($this->dbRow[$var])) { |
|
193 | - /* |
|
30 | + /** |
|
31 | + * The service this object is bound to. |
|
32 | + * |
|
33 | + * @var TDBMService |
|
34 | + */ |
|
35 | + protected $tdbmService; |
|
36 | + |
|
37 | + /** |
|
38 | + * The object containing this db row. |
|
39 | + * |
|
40 | + * @var AbstractTDBMObject |
|
41 | + */ |
|
42 | + private $object; |
|
43 | + |
|
44 | + /** |
|
45 | + * The name of the table the object if issued from. |
|
46 | + * |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + private $dbTableName; |
|
50 | + |
|
51 | + /** |
|
52 | + * The array of columns returned from database. |
|
53 | + * |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + private $dbRow = array(); |
|
57 | + |
|
58 | + /** |
|
59 | + * @var AbstractTDBMObject[] |
|
60 | + */ |
|
61 | + private $references = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
65 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
66 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
67 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
68 | + * |
|
69 | + * @var string |
|
70 | + */ |
|
71 | + private $status; |
|
72 | + |
|
73 | + /** |
|
74 | + * The values of the primary key. |
|
75 | + * This is set when the object is in "loaded" state. |
|
76 | + * |
|
77 | + * @var array An array of column => value |
|
78 | + */ |
|
79 | + private $primaryKeys; |
|
80 | + |
|
81 | + /** |
|
82 | + * You should never call the constructor directly. Instead, you should use the |
|
83 | + * TDBMService class that will create TDBMObjects for you. |
|
84 | + * |
|
85 | + * Used with id!=false when we want to retrieve an existing object |
|
86 | + * and id==false if we want a new object |
|
87 | + * |
|
88 | + * @param AbstractTDBMObject $object The object containing this db row |
|
89 | + * @param string $table_name |
|
90 | + * @param array $primaryKeys |
|
91 | + * @param TDBMService $tdbmService |
|
92 | + * |
|
93 | + * @throws TDBMException |
|
94 | + * @throws TDBMInvalidOperationException |
|
95 | + */ |
|
96 | + public function __construct(AbstractTDBMObject $object, $table_name, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = array()) |
|
97 | + { |
|
98 | + $this->object = $object; |
|
99 | + $this->dbTableName = $table_name; |
|
100 | + |
|
101 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
102 | + |
|
103 | + if ($tdbmService === null) { |
|
104 | + if (!empty($primaryKeys)) { |
|
105 | + throw new TDBMException('You cannot pass an id to the DbRow constructor without passing also a TDBMService.'); |
|
106 | + } |
|
107 | + } else { |
|
108 | + $this->tdbmService = $tdbmService; |
|
109 | + |
|
110 | + if (!empty($primaryKeys)) { |
|
111 | + $this->_setPrimaryKeys($primaryKeys); |
|
112 | + if (!empty($dbRow)) { |
|
113 | + $this->dbRow = $dbRow; |
|
114 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
115 | + } else { |
|
116 | + $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
117 | + } |
|
118 | + $tdbmService->_addToCache($this); |
|
119 | + } else { |
|
120 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
121 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
122 | + } |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + public function _attach(TDBMService $tdbmService) |
|
127 | + { |
|
128 | + if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
129 | + throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
130 | + } |
|
131 | + $this->tdbmService = $tdbmService; |
|
132 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
133 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Sets the state of the TDBM Object |
|
138 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
139 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
140 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
141 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
142 | + * |
|
143 | + * @param string $state |
|
144 | + */ |
|
145 | + public function _setStatus($state) |
|
146 | + { |
|
147 | + $this->status = $state; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * This is an internal method. You should not call this method yourself. The TDBM library will do it for you. |
|
152 | + * If the object is in state 'not loaded', this method performs a query in database to load the object. |
|
153 | + * |
|
154 | + * A TDBMException is thrown is no object can be retrieved (for instance, if the primary key specified |
|
155 | + * cannot be found). |
|
156 | + */ |
|
157 | + public function _dbLoadIfNotLoaded() |
|
158 | + { |
|
159 | + if ($this->status == TDBMObjectStateEnum::STATE_NOT_LOADED) { |
|
160 | + $connection = $this->tdbmService->getConnection(); |
|
161 | + |
|
162 | + /// buildFilterFromFilterBag($filter_bag) |
|
163 | + list($sql_where, $parameters) = $this->tdbmService->buildFilterFromFilterBag($this->primaryKeys); |
|
164 | + |
|
165 | + $sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where; |
|
166 | + $result = $connection->executeQuery($sql, $parameters); |
|
167 | + |
|
168 | + if ($result->rowCount() === 0) { |
|
169 | + throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \"\"."); |
|
170 | + } |
|
171 | + |
|
172 | + $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
173 | + |
|
174 | + $this->dbRow = []; |
|
175 | + $types = $this->tdbmService->_getColumnTypesForTable($this->dbTableName); |
|
176 | + |
|
177 | + foreach ($row as $key => $value) { |
|
178 | + $this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform()); |
|
179 | + } |
|
180 | + |
|
181 | + $result->closeCursor(); |
|
182 | + |
|
183 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + public function get($var) |
|
188 | + { |
|
189 | + $this->_dbLoadIfNotLoaded(); |
|
190 | + |
|
191 | + // Let's first check if the key exist. |
|
192 | + if (!isset($this->dbRow[$var])) { |
|
193 | + /* |
|
194 | 194 | // Unable to find column.... this is an error if the object has been retrieved from database. |
195 | 195 | // If it's a new object, well, that may not be an error after all! |
196 | 196 | // Let's check if the column does exist in the table |
@@ -210,39 +210,39 @@ discard block |
||
210 | 210 | $str = "Could not find column \"$var\" in table \"$this->dbTableName\". Maybe you meant one of those columns: '".implode("', '",$result_array)."'"; |
211 | 211 | |
212 | 212 | throw new TDBMException($str);*/ |
213 | - return; |
|
214 | - } |
|
215 | - |
|
216 | - $value = $this->dbRow[$var]; |
|
217 | - if ($value instanceof \DateTime) { |
|
218 | - if (method_exists('DateTimeImmutable', 'createFromMutable')) { // PHP 5.6+ only |
|
219 | - return \DateTimeImmutable::createFromMutable($value); |
|
220 | - } else { |
|
221 | - return new \DateTimeImmutable($value->format('c')); |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - return $this->dbRow[$var]; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Returns true if a column is set, false otherwise. |
|
230 | - * |
|
231 | - * @param string $var |
|
232 | - * |
|
233 | - * @return bool |
|
234 | - */ |
|
235 | - /*public function has($var) { |
|
213 | + return; |
|
214 | + } |
|
215 | + |
|
216 | + $value = $this->dbRow[$var]; |
|
217 | + if ($value instanceof \DateTime) { |
|
218 | + if (method_exists('DateTimeImmutable', 'createFromMutable')) { // PHP 5.6+ only |
|
219 | + return \DateTimeImmutable::createFromMutable($value); |
|
220 | + } else { |
|
221 | + return new \DateTimeImmutable($value->format('c')); |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + return $this->dbRow[$var]; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Returns true if a column is set, false otherwise. |
|
230 | + * |
|
231 | + * @param string $var |
|
232 | + * |
|
233 | + * @return bool |
|
234 | + */ |
|
235 | + /*public function has($var) { |
|
236 | 236 | $this->_dbLoadIfNotLoaded(); |
237 | 237 | |
238 | 238 | return isset($this->dbRow[$var]); |
239 | 239 | }*/ |
240 | 240 | |
241 | - public function set($var, $value) |
|
242 | - { |
|
243 | - $this->_dbLoadIfNotLoaded(); |
|
241 | + public function set($var, $value) |
|
242 | + { |
|
243 | + $this->_dbLoadIfNotLoaded(); |
|
244 | 244 | |
245 | - /* |
|
245 | + /* |
|
246 | 246 | // Ok, let's start by checking the column type |
247 | 247 | $type = $this->db_connection->getColumnType($this->dbTableName, $var); |
248 | 248 | |
@@ -252,198 +252,198 @@ discard block |
||
252 | 252 | } |
253 | 253 | */ |
254 | 254 | |
255 | - /*if ($var == $this->getPrimaryKey() && isset($this->dbRow[$var])) |
|
255 | + /*if ($var == $this->getPrimaryKey() && isset($this->dbRow[$var])) |
|
256 | 256 | throw new TDBMException("Error! Changing primary key value is forbidden.");*/ |
257 | - $this->dbRow[$var] = $value; |
|
258 | - if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
259 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
260 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
261 | - } |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @param string $foreignKeyName |
|
266 | - * @param AbstractTDBMObject $bean |
|
267 | - */ |
|
268 | - public function setRef($foreignKeyName, AbstractTDBMObject $bean = null) |
|
269 | - { |
|
270 | - $this->references[$foreignKeyName] = $bean; |
|
271 | - |
|
272 | - if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
273 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
274 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @param string $foreignKeyName A unique name for this reference |
|
280 | - * |
|
281 | - * @return AbstractTDBMObject|null |
|
282 | - */ |
|
283 | - public function getRef($foreignKeyName) |
|
284 | - { |
|
285 | - if (array_key_exists($foreignKeyName, $this->references)) { |
|
286 | - return $this->references[$foreignKeyName]; |
|
287 | - } elseif ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->tdbmService === null) { |
|
288 | - // If the object is new and has no property, then it has to be empty. |
|
289 | - return; |
|
290 | - } else { |
|
291 | - $this->_dbLoadIfNotLoaded(); |
|
292 | - |
|
293 | - // Let's match the name of the columns to the primary key values |
|
294 | - $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
295 | - |
|
296 | - $values = []; |
|
297 | - foreach ($fk->getLocalColumns() as $column) { |
|
298 | - if (!isset($this->dbRow[$column])) { |
|
299 | - return; |
|
300 | - } |
|
301 | - $values[] = $this->dbRow[$column]; |
|
302 | - } |
|
303 | - |
|
304 | - $filter = array_combine($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()), $values); |
|
305 | - |
|
306 | - return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true); |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Returns the name of the table this object comes from. |
|
312 | - * |
|
313 | - * @return string |
|
314 | - */ |
|
315 | - public function _getDbTableName() |
|
316 | - { |
|
317 | - return $this->dbTableName; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Method used internally by TDBM. You should not use it directly. |
|
322 | - * This method returns the status of the TDBMObject. |
|
323 | - * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
324 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
325 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
326 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
327 | - * |
|
328 | - * @return string |
|
329 | - */ |
|
330 | - public function _getStatus() |
|
331 | - { |
|
332 | - return $this->status; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Override the native php clone function for TDBMObjects. |
|
337 | - */ |
|
338 | - public function __clone() |
|
339 | - { |
|
340 | - // Let's load the row (before we lose the ID!) |
|
341 | - $this->_dbLoadIfNotLoaded(); |
|
342 | - |
|
343 | - //Let's set the status to detached |
|
344 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
345 | - |
|
346 | - $this->primaryKeys = []; |
|
347 | - |
|
348 | - //Now unset the PK from the row |
|
349 | - if ($this->tdbmService) { |
|
350 | - $pk_array = $this->tdbmService->getPrimaryKeyColumns($this->dbTableName); |
|
351 | - foreach ($pk_array as $pk) { |
|
352 | - $this->dbRow[$pk] = null; |
|
353 | - } |
|
354 | - } |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Returns raw database row. |
|
359 | - * |
|
360 | - * @return array |
|
361 | - * |
|
362 | - * @throws TDBMMissingReferenceException |
|
363 | - */ |
|
364 | - public function _getDbRow() |
|
365 | - { |
|
366 | - // Let's merge $dbRow and $references |
|
367 | - $dbRow = $this->dbRow; |
|
368 | - |
|
369 | - foreach ($this->references as $foreignKeyName => $reference) { |
|
370 | - // Let's match the name of the columns to the primary key values |
|
371 | - $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
372 | - $localColumns = $fk->getLocalColumns(); |
|
373 | - |
|
374 | - if ($reference !== null) { |
|
375 | - $refDbRows = $reference->_getDbRows(); |
|
376 | - $firstRefDbRow = reset($refDbRows); |
|
377 | - if ($firstRefDbRow->_getStatus() == TDBMObjectStateEnum::STATE_DELETED) { |
|
378 | - throw TDBMMissingReferenceException::referenceDeleted($this->dbTableName, $reference); |
|
379 | - } |
|
380 | - $pkValues = array_values($firstRefDbRow->_getPrimaryKeys()); |
|
381 | - for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
382 | - $dbRow[$localColumns[$i]] = $pkValues[$i]; |
|
383 | - } |
|
384 | - } else { |
|
385 | - for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
386 | - $dbRow[$localColumns[$i]] = null; |
|
387 | - } |
|
388 | - } |
|
389 | - } |
|
390 | - |
|
391 | - return $dbRow; |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Returns references array. |
|
396 | - * |
|
397 | - * @return AbstractTDBMObject[] |
|
398 | - */ |
|
399 | - public function _getReferences() |
|
400 | - { |
|
401 | - return $this->references; |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * Returns the values of the primary key. |
|
406 | - * This is set when the object is in "loaded" state. |
|
407 | - * |
|
408 | - * @return array |
|
409 | - */ |
|
410 | - public function _getPrimaryKeys() |
|
411 | - { |
|
412 | - return $this->primaryKeys; |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Sets the values of the primary key. |
|
417 | - * This is set when the object is in "loaded" state. |
|
418 | - * |
|
419 | - * @param array $primaryKeys |
|
420 | - */ |
|
421 | - public function _setPrimaryKeys(array $primaryKeys) |
|
422 | - { |
|
423 | - $this->primaryKeys = $primaryKeys; |
|
424 | - foreach ($this->primaryKeys as $column => $value) { |
|
425 | - $this->dbRow[$column] = $value; |
|
426 | - } |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Returns the TDBMObject this bean is associated to. |
|
431 | - * |
|
432 | - * @return AbstractTDBMObject |
|
433 | - */ |
|
434 | - public function getTDBMObject() |
|
435 | - { |
|
436 | - return $this->object; |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Sets the TDBMObject this bean is associated to. |
|
441 | - * Only used when cloning. |
|
442 | - * |
|
443 | - * @param AbstractTDBMObject $object |
|
444 | - */ |
|
445 | - public function setTDBMObject(AbstractTDBMObject $object) |
|
446 | - { |
|
447 | - $this->object = $object; |
|
448 | - } |
|
257 | + $this->dbRow[$var] = $value; |
|
258 | + if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
259 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
260 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
261 | + } |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @param string $foreignKeyName |
|
266 | + * @param AbstractTDBMObject $bean |
|
267 | + */ |
|
268 | + public function setRef($foreignKeyName, AbstractTDBMObject $bean = null) |
|
269 | + { |
|
270 | + $this->references[$foreignKeyName] = $bean; |
|
271 | + |
|
272 | + if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
273 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
274 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @param string $foreignKeyName A unique name for this reference |
|
280 | + * |
|
281 | + * @return AbstractTDBMObject|null |
|
282 | + */ |
|
283 | + public function getRef($foreignKeyName) |
|
284 | + { |
|
285 | + if (array_key_exists($foreignKeyName, $this->references)) { |
|
286 | + return $this->references[$foreignKeyName]; |
|
287 | + } elseif ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->tdbmService === null) { |
|
288 | + // If the object is new and has no property, then it has to be empty. |
|
289 | + return; |
|
290 | + } else { |
|
291 | + $this->_dbLoadIfNotLoaded(); |
|
292 | + |
|
293 | + // Let's match the name of the columns to the primary key values |
|
294 | + $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
295 | + |
|
296 | + $values = []; |
|
297 | + foreach ($fk->getLocalColumns() as $column) { |
|
298 | + if (!isset($this->dbRow[$column])) { |
|
299 | + return; |
|
300 | + } |
|
301 | + $values[] = $this->dbRow[$column]; |
|
302 | + } |
|
303 | + |
|
304 | + $filter = array_combine($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()), $values); |
|
305 | + |
|
306 | + return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true); |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Returns the name of the table this object comes from. |
|
312 | + * |
|
313 | + * @return string |
|
314 | + */ |
|
315 | + public function _getDbTableName() |
|
316 | + { |
|
317 | + return $this->dbTableName; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Method used internally by TDBM. You should not use it directly. |
|
322 | + * This method returns the status of the TDBMObject. |
|
323 | + * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
324 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
325 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
326 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
327 | + * |
|
328 | + * @return string |
|
329 | + */ |
|
330 | + public function _getStatus() |
|
331 | + { |
|
332 | + return $this->status; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Override the native php clone function for TDBMObjects. |
|
337 | + */ |
|
338 | + public function __clone() |
|
339 | + { |
|
340 | + // Let's load the row (before we lose the ID!) |
|
341 | + $this->_dbLoadIfNotLoaded(); |
|
342 | + |
|
343 | + //Let's set the status to detached |
|
344 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
345 | + |
|
346 | + $this->primaryKeys = []; |
|
347 | + |
|
348 | + //Now unset the PK from the row |
|
349 | + if ($this->tdbmService) { |
|
350 | + $pk_array = $this->tdbmService->getPrimaryKeyColumns($this->dbTableName); |
|
351 | + foreach ($pk_array as $pk) { |
|
352 | + $this->dbRow[$pk] = null; |
|
353 | + } |
|
354 | + } |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Returns raw database row. |
|
359 | + * |
|
360 | + * @return array |
|
361 | + * |
|
362 | + * @throws TDBMMissingReferenceException |
|
363 | + */ |
|
364 | + public function _getDbRow() |
|
365 | + { |
|
366 | + // Let's merge $dbRow and $references |
|
367 | + $dbRow = $this->dbRow; |
|
368 | + |
|
369 | + foreach ($this->references as $foreignKeyName => $reference) { |
|
370 | + // Let's match the name of the columns to the primary key values |
|
371 | + $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
372 | + $localColumns = $fk->getLocalColumns(); |
|
373 | + |
|
374 | + if ($reference !== null) { |
|
375 | + $refDbRows = $reference->_getDbRows(); |
|
376 | + $firstRefDbRow = reset($refDbRows); |
|
377 | + if ($firstRefDbRow->_getStatus() == TDBMObjectStateEnum::STATE_DELETED) { |
|
378 | + throw TDBMMissingReferenceException::referenceDeleted($this->dbTableName, $reference); |
|
379 | + } |
|
380 | + $pkValues = array_values($firstRefDbRow->_getPrimaryKeys()); |
|
381 | + for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
382 | + $dbRow[$localColumns[$i]] = $pkValues[$i]; |
|
383 | + } |
|
384 | + } else { |
|
385 | + for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
386 | + $dbRow[$localColumns[$i]] = null; |
|
387 | + } |
|
388 | + } |
|
389 | + } |
|
390 | + |
|
391 | + return $dbRow; |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Returns references array. |
|
396 | + * |
|
397 | + * @return AbstractTDBMObject[] |
|
398 | + */ |
|
399 | + public function _getReferences() |
|
400 | + { |
|
401 | + return $this->references; |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * Returns the values of the primary key. |
|
406 | + * This is set when the object is in "loaded" state. |
|
407 | + * |
|
408 | + * @return array |
|
409 | + */ |
|
410 | + public function _getPrimaryKeys() |
|
411 | + { |
|
412 | + return $this->primaryKeys; |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Sets the values of the primary key. |
|
417 | + * This is set when the object is in "loaded" state. |
|
418 | + * |
|
419 | + * @param array $primaryKeys |
|
420 | + */ |
|
421 | + public function _setPrimaryKeys(array $primaryKeys) |
|
422 | + { |
|
423 | + $this->primaryKeys = $primaryKeys; |
|
424 | + foreach ($this->primaryKeys as $column => $value) { |
|
425 | + $this->dbRow[$column] = $value; |
|
426 | + } |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Returns the TDBMObject this bean is associated to. |
|
431 | + * |
|
432 | + * @return AbstractTDBMObject |
|
433 | + */ |
|
434 | + public function getTDBMObject() |
|
435 | + { |
|
436 | + return $this->object; |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Sets the TDBMObject this bean is associated to. |
|
441 | + * Only used when cloning. |
|
442 | + * |
|
443 | + * @param AbstractTDBMObject $object |
|
444 | + */ |
|
445 | + public function setTDBMObject(AbstractTDBMObject $object) |
|
446 | + { |
|
447 | + $this->object = $object; |
|
448 | + } |
|
449 | 449 | } |
@@ -27,11 +27,11 @@ |
||
27 | 27 | */ |
28 | 28 | final class TDBMObjectStateEnum |
29 | 29 | { |
30 | - const STATE_DETACHED = 'detached'; |
|
31 | - const STATE_NEW = 'new'; |
|
32 | - const STATE_SAVING = 'saving'; |
|
33 | - const STATE_NOT_LOADED = 'not loaded'; |
|
34 | - const STATE_LOADED = 'loaded'; |
|
35 | - const STATE_DIRTY = 'dirty'; |
|
36 | - const STATE_DELETED = 'deleted'; |
|
30 | + const STATE_DETACHED = 'detached'; |
|
31 | + const STATE_NEW = 'new'; |
|
32 | + const STATE_SAVING = 'saving'; |
|
33 | + const STATE_NOT_LOADED = 'not loaded'; |
|
34 | + const STATE_LOADED = 'loaded'; |
|
35 | + const STATE_DIRTY = 'dirty'; |
|
36 | + const STATE_DELETED = 'deleted'; |
|
37 | 37 | } |