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
|
<?php
/**
* abstract class extends Zend_Db_Table_Abstract
*
* @name App_Db_Table_Abstract
* @dir library/app/Db/Table
* @package App
* @author Sébastien CHOMY
* @abstract
*/
abstract class App_Db_Table_Abstract extends Zend_Db_Table_Abstract
{
/**
*
* Fetches rows by name column.
* The second argument specifies one or more key value(s).
* To find multiple rows by name column, the second argument must
* be an array.
*
* The findBy() method always returns a Rowset object, even if only one row
* was found.
*
* @param string $column name column
* @param mixed (string | array ) $values argument where clause
* @return Zend_Db_Table_Rowset_Abstract Row(s) matching the criteria.
* @throws Zend_Db_Table_Exception
*/
public function findBy($column, $values)
{
// Check if the provided column is a column of the table
if (!isset($this->_metadata[$column])) {
/**
* @see Zend_Db_Table_Exception
*/
require_once 'Zend/Db/Table/Exception.php';
throw new Zend_Db_Table_Exception('Column "' . $column . '" not found in table.');
}
if (!is_array($values)) {
$whereList[] = $values;
} else {
$whereList = $values;
}
if (!is_null($whereList[0])) {
$tableName = $this->_db->quoteTableAs($this->_name, null, true);
$columnName = $this->_db->quoteIdentifier($column, true);
$select = $this->select();
foreach ($whereList as $keyPosition => $value) {
$$value = $this->_db->quoteIdentifier($value);
if ($keyPosition == 0) {
$select->where($this->_db->quoteInto($tableName . '.' . $columnName . ' = ?', $value));
} else {
$select->orWhere($this->_db->quoteInto($tableName . '.' . $columnName . ' = ?', $value));
}
}
}
// empty whereList should return empty rowset
if (is_null($whereList[0])) {
$rowsetClass = $this->getRowsetClass();
if (!class_exists($rowsetClass)) {
require_once 'Zend/Loader.php';
Zend_Loader::loadClass($rowsetClass);
}
return new $rowsetClass(array('table' => $this, 'rowClass' => $this->getRowClass(), 'stored' => true));
}
return $this->fetchAll($select);
}//end:: findBy
/**
* magic method
*/
public function __call($method, $args)
{
// call findBy() method
$matches = array();
if (preg_match('/^findBy(\w+?)$/', $method, $matches)) {
// use inflector
$inflector = new Zend_Filter_Inflector(':cible');
$inflector->setRules (array( ':cible' => array('Word_CamelCaseToUnderscore', 'StringToLower') ));
$column = $inflector->filter(array('cible' => $matches[1]));
$this->findBy($column, $args[0]);
}
}//end::__call
}//Eof::class
?> |
Partager