Passed
Branch php-cs-fixer (b9836a)
by Fabio
15:02
created

TTableGateway::onExecuteCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * TTableGateway class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 * @package Prado\Data\DataGateway
10
 */
11
12
namespace Prado\Data\DataGateway;
13
14
/**
15
 * Loads the data gateway command builder and sql criteria.
16
 */
17
use Prado\Data\Common\TDbMetaData;
18
use Prado\Data\Common\TDbTableInfo;
19
use Prado\Exceptions\TDbException;
20
use Prado\Prado;
21
22
23
/**
24
 * TTableGateway class provides several find methods to get data from the database
25
 * and update, insert, and delete methods.
26
 *
27
 * Each method maps the input parameters into a SQL call and executes the SQL
28
 * against a database connection. The TTableGateway is stateless
29
 * (with respect to the data and data objects), as its role is to push data back and forth.
30
 *
31
 * Example usage:
32
 * <code>
33
 * //create a connection
34
 * $dsn = 'pgsql:host=localhost;dbname=test';
35
 * $conn = new TDbConnection($dsn, 'dbuser','dbpass');
36
 *
37
 * //create a table gateway for table/view named 'address'
38
 * $table = new TTableGateway('address', $conn);
39
 *
40
 * //insert a new row, returns last insert id (if applicable)
41
 * $id = $table->insert(array('name'=>'wei', 'phone'=>'111111'));
42
 *
43
 * $record1 = $table->findByPk($id); //find inserted record
44
 *
45
 * //finds all records, returns an iterator
46
 * $records = $table->findAll();
47
 * print_r($records->readAll());
48
 *
49
 * //update the row
50
 * $table->updateByPk($record1, $id);
51
 * </code>
52
 *
53
 * All methods that may return more than one row of data will return an
54
 * TDbDataReader iterator.
55
 *
56
 * The OnCreateCommand event is raised when a command is prepared and parameter
57
 * binding is completed. The parameter object is a TDataGatewayEventParameter of which the
58
 * {@link TDataGatewayEventParameter::getCommand Command} property can be
59
 * inspected to obtain the sql query to be executed.
60
 *
61
 * The OnExecuteCommand	event is raised when a command is executed and the result
62
 * from the database was returned. The parameter object is a
63
 * TDataGatewayResultEventParameter of which the
64
 * {@link TDataGatewayEventParameter::getResult Result} property contains
65
 * the data return from the database. The data returned can be changed
66
 * by setting the {@link TDataGatewayEventParameter::setResult Result} property.
67
 *
68
 * <code>
69
 * $table->OnCreateCommand[] = 'log_it'; //any valid PHP callback statement
70
 * $table->OnExecuteCommand[] = array($obj, 'method_name'); // calls 'method_name' on $obj
71
 *
72
 * function log_it($sender, $param)
73
 * {
74
 *     var_dump($param); //TDataGatewayEventParameter object.
75
 * }
76
 * </code>
77
 *
78
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
79
 * @package Prado\Data\DataGateway
80
 * @since 3.1
81
 */
82
class TTableGateway extends \Prado\TComponent
83
{
84
	private $_command;
85
	private $_connection;
86
87
	/**
88
	 * Creates a new generic table gateway for a given table or view name
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
89
	 * and a database connection.
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\database was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
90
	 * @param string|TDbTableInfo table or view name or table information.
91
	 * @param TDbConnection database connection.
92
	 */
93
	public function __construct($table, $connection)
94
	{
95
		$this->_connection = $connection;
96
		if(is_string($table))
97
			$this->setTableName($table);
98
		elseif($table instanceof TDbTableInfo)
99
			$this->setTableInfo($table);
100
		else
101
			throw new TDbException('dbtablegateway_invalid_table_info');
102
	}
103
104
	/**
105
	 * @param TDbTableInfo table or view information.
106
	 */
107
	protected function setTableInfo($tableInfo)
108
	{
109
		$builder = $tableInfo->createCommandBuilder($this->getDbConnection());
110
		$this->initCommandBuilder($builder);
111
	}
112
113
	/**
114
	 * Sets up the command builder for the given table.
115
	 * @param string table or view name.
116
	 */
117
	protected function setTableName($tableName)
118
	{
119
		$meta = TDbMetaData::getInstance($this->getDbConnection());
120
		$this->initCommandBuilder($meta->createCommandBuilder($tableName));
121
	}
122
123
	public function getTableInfo()
124
	{
125
		return $this->getCommand()->getTableInfo();
126
	}
127
128
	public function getTableName()
129
	{
130
		return $this->getTableInfo()->getTableName();
131
	}
132
133
	/**
134
	 * @param TDbCommandBuilder database specific command builder.
135
	 */
136
	protected function initCommandBuilder($builder)
137
	{
138
		$this->_command = new TDataGatewayCommand($builder);
139
		$this->_command->OnCreateCommand[] = [$this, 'onCreateCommand'];
0 ignored issues
show
Bug Best Practice introduced by
The property OnCreateCommand does not exist on Prado\Data\DataGateway\TDataGatewayCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
140
		$this->_command->OnExecuteCommand[] = [$this, 'onExecuteCommand'];
0 ignored issues
show
Bug Best Practice introduced by
The property OnExecuteCommand does not exist on Prado\Data\DataGateway\TDataGatewayCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
141
	}
142
143
	/**
144
	 * Raised when a command is prepared and parameter binding is completed.
145
	 * The parameter object is TDataGatewayEventParameter of which the
146
	 * {@link TDataGatewayEventParameter::getCommand Command} property can be
147
	 * inspected to obtain the sql query to be executed.
148
	 * @param TDataGatewayCommand originator $sender
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\originator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
149
	 * @param TDataGatewayEventParameter
150
	 */
151
	public function onCreateCommand($sender, $param)
152
	{
153
		$this->raiseEvent('OnCreateCommand', $this, $param);
154
	}
155
156
	/**
157
	 * Raised when a command is executed and the result from the database was returned.
158
	 * The parameter object is TDataGatewayResultEventParameter of which the
159
	 * {@link TDataGatewayEventParameter::getResult Result} property contains
160
	 * the data return from the database. The data returned can be changed
161
	 * by setting the {@link TDataGatewayEventParameter::setResult Result} property.
162
	 * @param TDataGatewayCommand originator $sender
163
	 * @param TDataGatewayResultEventParameter
164
	 */
165
	public function onExecuteCommand($sender, $param)
166
	{
167
		$this->raiseEvent('OnExecuteCommand', $this, $param);
168
	}
169
170
	/**
171
	 * @return TDataGatewayCommand command builder and executor.
172
	 */
173
	protected function getCommand()
174
	{
175
		return $this->_command;
176
	}
177
178
	/**
179
	 * @return TDbConnection database connection.
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\TDbConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
180
	 */
181
	public function getDbConnection()
182
	{
183
		return $this->_connection;
184
	}
185
186
	/**
187
	 * Execute arbituary sql command with binding parameters.
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\binding was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
188
	 * @param string SQL query string.
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\SQL was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
189
	 * @param array binding parameters, positional or named.
190
	 * @return array query results.
191
	 */
192
	public function findBySql($sql, $parameters = [])
193
	{
194
		$args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null;
195
		$criteria = $this->getCriteria($sql, $parameters, $args);
196
		return $this->getCommand()->findBySql($criteria);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getCommand()->findBySql($criteria) returns the type Prado\Data\DataGateway\TDbDataReader which is incompatible with the documented return type array.
Loading history...
197
	}
198
199
	/**
200
	 * Execute arbituary sql command with binding parameters.
201
	 * @param string SQL query string.
202
	 * @param array binding parameters, positional or named.
203
	 * @return TDbDataReader query results.
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\TDbDataReader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
204
	 */
205
	public function findAllBySql($sql, $parameters = [])
206
	{
207
		$args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null;
208
		$criteria = $this->getCriteria($sql, $parameters, $args);
209
		return $this->getCommand()->findAllBySql($criteria);
210
	}
211
212
	/**
213
	 * Find one single record that matches the criteria.
214
	 *
215
	 * Usage:
216
	 * <code>
217
	 * $table->find('username = :name AND password = :pass',
218
	 * 					array(':name'=>$name, ':pass'=>$pass));
219
	 * $table->find('username = ? AND password = ?', array($name, $pass));
220
	 * $table->find('username = ? AND password = ?', $name, $pass);
221
	 * //$criteria is of TSqlCriteria
222
	 * $table->find($criteria); //the 2nd parameter for find() is ignored.
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\parameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
223
	 * </code>
224
	 *
225
	 * @param string|TSqlCriteria SQL condition or criteria object.
226
	 * @param mixed parameter values.
227
	 * @return array matching record object.
228
	 */
229
	public function find($criteria, $parameters = [])
230
	{
231
		$args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null;
232
		$criteria = $this->getCriteria($criteria, $parameters, $args);
233
		return $this->getCommand()->find($criteria);
234
	}
235
236
	/**
237
	 * Accepts same parameters as find(), but returns TDbDataReader instead.
238
	 * @param string|TSqlCriteria SQL condition or criteria object.
239
	 * @param mixed parameter values.
240
	 * @return TDbDataReader matching records.
241
	 */
242
	public function findAll($criteria = null, $parameters = [])
243
	{
244
		$args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null;
245
		if($criteria !== null)
246
			$criteria = $this->getCriteria($criteria, $parameters, $args);
247
		return $this->getCommand()->findAll($criteria);
248
	}
249
250
	/**
251
	 * Find one record using only the primary key or composite primary keys. Usage:
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\primary was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
252
	 *
253
	 * <code>
254
	 * $table->findByPk($primaryKey);
255
	 * $table->findByPk($key1, $key2, ...);
256
	 * $table->findByPk(array($key1,$key2,...));
257
	 * </code>
258
	 *
259
	 * @param mixed primary keys
260
	 * @return array matching record.
261
	 */
262
	public function findByPk($keys)
263
	{
264
		if(func_num_args() > 1)
265
			$keys = func_get_args();
266
		return $this->getCommand()->findByPk($keys);
267
	}
268
269
	/**
270
	 * Similar to findByPk(), but returns TDbDataReader instead.
271
	 *
272
	 * For scalar primary keys:
273
	 * <code>
274
	 * $table->findAllByPk($key1, $key2, ...);
275
	 * $table->findAllByPk(array($key1, $key2, ...));
276
	 * </code>
277
	 *
278
	 * For composite keys:
279
	 * <code>
280
	 * $table->findAllByPk(array($key1, $key2), array($key3, $key4), ...);
281
	 * $table->findAllByPk(array(array($key1, $key2), array($key3, $key4), ...));
282
	 * </code>
283
	 * @param mixed primary keys
284
	 * @return TDbDataReader data reader.
285
	 */
286
	public function findAllByPks($keys)
287
	{
288
		if(func_num_args() > 1)
289
			$keys = func_get_args();
290
		return $this->getCommand()->findAllByPk($keys);
291
	}
292
293
	/**
294
	 * Delete records from the table with condition given by $where and
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\condition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
295
	 * binding values specified by $parameter argument.
296
	 * This method uses additional arguments as $parameters. E.g.
297
	 * <code>
298
	 * $table->delete('age > ? AND location = ?', $age, $location);
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\delete was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
299
	 * </code>
300
	 * @param string delete condition.
301
	 * @param array condition parameters.
302
	 * @return integer number of records deleted.
303
	 */
304
	public function deleteAll($criteria, $parameters = [])
305
	{
306
		$args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null;
307
		$criteria = $this->getCriteria($criteria, $parameters, $args);
308
		return $this->getCommand()->delete($criteria);
309
	}
310
311
	/**
312
	 * Delete records by primary key. Usage:
313
	 *
314
	 * <code>
315
	 * $table->deleteByPk($primaryKey); //delete 1 record
316
	 * $table->deleteByPk($key1,$key2,...); //delete multiple records
317
	 * $table->deleteByPk(array($key1,$key2,...)); //delete multiple records
318
	 * </code>
319
	 *
320
	 * For composite primary keys (determined from the table definitions):
321
	 * <code>
322
	 * $table->deleteByPk(array($key1,$key2)); //delete 1 record
323
	 *
324
	 * //delete multiple records
325
	 * $table->deleteByPk(array($key1,$key2), array($key3,$key4),...);
326
	 *
327
	 * //delete multiple records
328
	 * $table->deleteByPk(array( array($key1,$key2), array($key3,$key4), .. ));
329
	 * </code>
330
	 *
331
	 * @param mixed primary key values.
332
	 * @return int number of records deleted.
333
	 */
334
	public function deleteByPk($keys)
335
	{
336
		if(func_num_args() > 1)
337
			$keys = func_get_args();
338
		return $this->getCommand()->deleteByPk($keys);
339
	}
340
341
	/**
342
	 * Alias for deleteByPk()
343
	 */
344
	public function deleteAllByPks($keys)
345
	{
346
		if(func_num_args() > 1)
347
			$keys = func_get_args();
348
		return $this->deleteByPk($keys);
349
	}
350
351
	/**
352
	 * Find the number of records.
353
	 * @param string|TSqlCriteria SQL condition or criteria object.
354
	 * @param mixed parameter values.
355
	 * @return int number of records.
356
	 */
357
	public function count($criteria = null, $parameters = [])
358
	{
359
		$args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null;
360
		if($criteria !== null)
361
			$criteria = $this->getCriteria($criteria, $parameters, $args);
362
		return $this->getCommand()->count($criteria);
363
	}
364
365
	/**
366
	 * Updates the table with new name-value pair $data. Each array key must
367
	 * correspond to a column name in the table. The update condition is
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\update was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
368
	 * specified by the $where argument and additional binding values can be
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\additional was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
369
	 * specified using the $parameter argument.
370
	 * This method uses additional arguments as $parameters. E.g.
371
	 * <code>
372
	 * $gateway->update($data, 'age > ? AND location = ?', $age, $location);
373
	 * </code>
374
	 * @param array new record data.
375
	 * @param string update condition
376
	 * @param array additional binding name-value pairs.
377
	 * @return integer number of records updated.
378
	 */
379
	public function update($data, $criteria, $parameters = [])
380
	{
381
		$args = func_num_args() > 2 ? array_slice(func_get_args(), 2) : null;
382
		$criteria = $this->getCriteria($criteria, $parameters, $args);
383
		return $this->getCommand()->update($data, $criteria);
384
	}
385
386
	/**
387
	 * Inserts a new record into the table. Each array key must
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\new was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
388
	 * correspond to a column name in the table unless a null value is permitted.
389
	 * @param array new record data.
390
	 * @return mixed last insert id if one column contains a serial or sequence,
391
	 * otherwise true if command executes successfully and affected 1 or more rows.
392
	 */
393
	public function insert($data)
394
	{
395
		return $this->getCommand()->insert($data);
396
	}
397
398
	/**
399
	 * @return mixed last insert id, null if none is found.
400
	 */
401
	public function getLastInsertId()
402
	{
403
		return $this->getCommand()->getLastInsertId();
404
	}
405
406
	/**
407
	 * Create a new TSqlCriteria object from a string $criteria. The $args
408
	 * are additional parameters and are used in place of the $parameters
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\parameters was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
409
	 * if $parameters is not an array and $args is an arrary.
410
	 * @param string|TSqlCriteria sql criteria
0 ignored issues
show
Bug introduced by
The type Prado\Data\DataGateway\sql was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
411
	 * @param mixed parameters passed by the user.
412
	 * @param array additional parameters obtained from function_get_args().
413
	 * @return TSqlCriteria criteria object.
414
	 */
415
	protected function getCriteria($criteria, $parameters, $args)
416
	{
417
		if(is_string($criteria))
418
		{
419
			$useArgs = !is_array($parameters) && is_array($args);
420
			return new TSqlCriteria($criteria, $useArgs ? $args : $parameters);
421
		}
422
		elseif($criteria instanceof TSqlCriteria)
423
			return $criteria;
424
		else
425
			throw new TDbException('dbtablegateway_invalid_criteria');
426
	}
427
428
	/**
429
	 * Dynamic find method using parts of method name as search criteria.
430
	 * Method name starting with "findBy" only returns 1 record.
431
	 * Method name starting with "findAllBy" returns 0 or more records.
432
	 * Method name starting with "deleteBy" deletes records by the trail criteria.
433
	 * The condition is taken as part of the method name after "findBy", "findAllBy"
434
	 * or "deleteBy".
435
	 *
436
	 * The following are equivalent:
437
	 * <code>
438
	 * $table->findByName($name)
439
	 * $table->find('Name = ?', $name);
440
	 * </code>
441
	 * <code>
442
	 * $table->findByUsernameAndPassword($name,$pass); // OR may be used
443
	 * $table->findBy_Username_And_Password($name,$pass); // _OR_ may be used
444
	 * $table->find('Username = ? AND Password = ?', $name, $pass);
445
	 * </code>
446
	 * <code>
447
	 * $table->findAllByAge($age);
448
	 * $table->findAll('Age = ?', $age);
449
	 * </code>
450
	 * <code>
451
	 * $table->deleteAll('Name = ?', $name);
452
	 * $table->deleteByName($name);
453
	 * </code>
454
	 * @return mixed single record if method name starts with "findBy", 0 or more records
455
	 * if method name starts with "findAllBy"
456
	 */
457
	public function __call($method, $args)
458
	{
459
		$delete = false;
460
		if($findOne = substr(strtolower($method), 0, 6) === 'findby')
461
			$condition = $method[6] === '_' ? substr($method, 7) : substr($method, 6);
462
		elseif(substr(strtolower($method), 0, 9) === 'findallby')
463
			$condition = $method[9] === '_' ? substr($method, 10) : substr($method, 9);
464
		elseif($delete = substr(strtolower($method), 0, 8) === 'deleteby')
465
			$condition = $method[8] === '_' ? substr($method, 9) : substr($method, 8);
466
		elseif($delete = substr(strtolower($method), 0, 11) === 'deleteallby')
467
			$condition = $method[11] === '_' ? substr($method, 12) : substr($method, 11);
468
		else
469
			return null;
470
471
		$criteria = $this->getCommand()->createCriteriaFromString($method, $condition, $args);
472
		if($delete)
473
			return $this->deleteAll($criteria);
474
		else
475
			return $findOne ? $this->find($criteria) : $this->findAll($criteria);
476
	}
477
}
478
479