-
Notifications
You must be signed in to change notification settings - Fork 397
/
QueryInheritanceBuilder.php
298 lines (267 loc) · 7.95 KB
/
QueryInheritanceBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Generator\Builder\Om;
use Propel\Generator\Exception\BuildException;
use Propel\Generator\Model\Inheritance;
/**
* Generates the empty PHP5 stub query class for use with single table
* inheritance.
*
* This class produces the empty stub class that can be customized with
* application business logic, custom behavior, etc.
*
*
* @author François Zaninotto
*/
class QueryInheritanceBuilder extends AbstractOMBuilder
{
/**
* The current child "object" we are operating on.
*/
protected $child;
/**
* Returns the name of the current class being built.
*
* @return string
*/
public function getUnprefixedClassName()
{
return $this->getNewStubQueryInheritanceBuilder($this->getChild())->getUnprefixedClassName();
}
/**
* Gets the package for the [base] object classes.
*
* @return string
*/
public function getPackage()
{
return ($this->getChild()->getPackage() ? $this->getChild()->getPackage() : parent::getPackage()) . '.Base';
}
/**
* Gets the namespace for the [base] object classes.
*
* @return string
*/
public function getNamespace()
{
if ($namespace = parent::getNamespace()) {
return $namespace . '\\Base';
}
return 'Base';
}
/**
* Sets the child object that we're operating on currently.
*
* @param Inheritance $child
*/
public function setChild(Inheritance $child)
{
$this->child = $child;
}
/**
* Returns the child object we're operating on currently.
*
* @return Inheritance
* @throws BuildException
*/
public function getChild()
{
if (!$this->child) {
throw new BuildException("The PHP5MultiExtendObjectBuilder needs to be told which child class to build (via setChild() method) before it can build the stub class.");
}
return $this->child;
}
/**
* Returns classpath to parent class.
*
* @return string
*/
protected function getParentClassName()
{
$ancestorClassName = ClassTools::classname($this->getChild()->getAncestor());
if ($this->getDatabase()->hasTableByPhpName($ancestorClassName)) {
return $this->getNewStubQueryBuilder($this->getDatabase()->getTableByPhpName($ancestorClassName))->getUnqualifiedClassName();
}
// find the inheritance for the parent class
foreach ($this->getTable()->getChildrenColumn()->getChildren() as $child) {
if ($child->getClassName() == $ancestorClassName) {
return $this->getNewStubQueryInheritanceBuilder($child)->getUnqualifiedClassName();
}
}
}
/**
* Adds class phpdoc comment and opening of class.
*
* @param string &$script
*/
protected function addClassOpen(&$script)
{
$table = $this->getTable();
$tableName = $table->getName();
$tableDesc = $table->getDescription();
$baseBuilder = $this->getStubQueryBuilder();
$this->declareClassFromBuilder($baseBuilder);
$baseClassName = $this->getParentClassName();
$script .= "
/**
* Skeleton subclass for representing a query for one of the subclasses of the '$tableName' table.
*
* $tableDesc
*";
if ($this->getBuildProperty('addTimeStamp')) {
$now = strftime('%c');
$script .= "
* This class was autogenerated by Propel " . $this->getBuildProperty('version') . " on:
*
* $now
*";
}
$script .= "
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*
*/
class " .$this->getUnqualifiedClassName() . " extends " . $baseClassName . " {
";
}
/**
* Specifies the methods that are added as part of the stub object class.
*
* By default there are no methods for the empty stub classes; override this
* method if you want to change that behavior.
*
* @param string $script
* @see ObjectBuilder::addClassBody()
*/
protected function addClassBody(&$script)
{
$this->declareClassFromBuilder($this->getStubPeerBuilder());
$this->declareClasses(
'\Propel\Runtime\Connection\ConnectionInterface',
'\Propel\Runtime\ActiveQuery\Criteria'
);
$this->addFactory($script);
$this->addPreSelect($script);
$this->addPreUpdate($script);
$this->addPreDelete($script);
$this->addDoDeleteAll($script);
}
/**
* Adds the factory for this object.
*
* @param string &$script
*/
protected function addFactory(&$script)
{
$builder = $this->getNewStubQueryInheritanceBuilder($this->getChild());
$this->declareClassFromBuilder($builder, 'Child');
$classname = $builder->getClassName();
$script .= "
/**
* Returns a new " . $classname . " object.
*
* @param string \$modelAlias The alias of a model in the query
* @param Criteria \$criteria Optional Criteria to build the query from
*
* @return " . $classname . "
*/
public static function create(\$modelAlias = null, \$criteria = null)
{
if (\$criteria instanceof " . $classname . ") {
return \$criteria;
}
\$query = new " . $classname . "();
if (null !== \$modelAlias) {
\$query->setModelAlias(\$modelAlias);
}
if (\$criteria instanceof Criteria) {
\$query->mergeWith(\$criteria);
}
return \$query;
}
";
}
protected function addPreSelect(&$script)
{
$child = $this->getChild();
$script .= "
/**
* Filters the query to target only " . $child->getClassName() . " objects.
*/
public function preSelect(ConnectionInterface \$con)
{
" . $this->getClassKeyCondition() . "
}
";
}
protected function addPreUpdate(&$script)
{
$child = $this->getChild();
$script .= "
/**
* Filters the query to target only " . $child->getClassName() . " objects.
*/
public function preUpdate(&\$values, ConnectionInterface \$con, \$forceIndividualSaves = false)
{
" . $this->getClassKeyCondition() . "
}
";
}
protected function addPreDelete(&$script)
{
$child = $this->getChild();
$script .= "
/**
* Filters the query to target only " . $child->getClassName() . " objects.
*/
public function preDelete(ConnectionInterface \$con)
{
" . $this->getClassKeyCondition() . "
}
";
}
protected function getClassKeyCondition()
{
$child = $this->getChild();
$col = $child->getColumn();
return "\$this->addUsingAlias(" . $col->getConstantName() . ", " . $this->getPeerClassName()."::CLASSKEY_".strtoupper($child->getKey()).");";
}
protected function addDoDeleteAll(&$script)
{
$child = $this->getChild();
$script .= "
/**
* Issue a DELETE query based on the current ModelCriteria deleting all rows in the table
* Having the " . $child->getClassName() . " class.
* This method is called by ModelCriteria::deleteAll() inside a transaction
*
* @param ConnectionInterface \$con a connection object
*
* @return integer the number of deleted rows
*/
public function doDeleteAll(\$con)
{
// condition on class key is already added in preDelete()
return parent::doDelete(\$con);
}
";
}
/**
* Closes class.
*
* @param string &$script
*/
protected function addClassClose(&$script)
{
$script .= "
} // " . $this->getUnqualifiedClassName() . "
";
}
}