mirror of
https://github.com/dg/dibi.git
synced 2025-08-06 06:07:39 +02:00
- added DibiTable magic fetch
- fixed some bugs
This commit is contained in:
@@ -89,7 +89,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
|
||||
}
|
||||
|
||||
DibiDriverException::tryError();
|
||||
if (isset($config['persistent'])) {
|
||||
if (empty($config['persistent'])) {
|
||||
$this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW);
|
||||
} else {
|
||||
$this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW);
|
||||
|
@@ -304,7 +304,8 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
|
||||
// check columns
|
||||
foreach ($assoc as $as) {
|
||||
if ($as !== '#' && $as !== '=' && $as !== '@' && !array_key_exists($as, $row)) {
|
||||
// offsetExists ignores NULL in PHP 5.2.1, isset() surprisingly NULL accepts
|
||||
if ($as !== '#' && $as !== '=' && $as !== '@' && !isset($row[$as])) {
|
||||
throw new InvalidArgumentException("Unknown column '$as' in associative descriptor.");
|
||||
}
|
||||
}
|
||||
@@ -407,7 +408,7 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
$value = $tmp[1];
|
||||
|
||||
} else {
|
||||
if (!array_key_exists($value, $row)) {
|
||||
if (!isset($row[$value])) {
|
||||
throw new InvalidArgumentException("Unknown value column '$value'.");
|
||||
}
|
||||
|
||||
@@ -418,7 +419,7 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
return $data;
|
||||
}
|
||||
|
||||
if (!array_key_exists($key, $row)) {
|
||||
if (!isset($row[$key])) {
|
||||
throw new InvalidArgumentException("Unknown key column '$key'.");
|
||||
}
|
||||
}
|
||||
|
@@ -358,4 +358,53 @@ abstract class DibiTable extends DibiObject
|
||||
return $this->command()->__call('select', $args)->from($this->name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********************* magic fetching ****************d*g**/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Magic fetch.
|
||||
* - $row = $model->fetchByUrl('about-us');
|
||||
* - $arr = $model->fetchAllByCategoryIdAndVisibility(5, TRUE);
|
||||
*
|
||||
* @param string
|
||||
* @param array
|
||||
* @return DibiRow|array
|
||||
*/
|
||||
public function __call($name, $args)
|
||||
{
|
||||
if (strncmp($name, 'fetchBy', 7) === 0) { // single row
|
||||
$single = TRUE;
|
||||
$name = substr($name, 7);
|
||||
|
||||
} elseif (strncmp($name, 'fetchAllBy', 10) === 0) { // multi row
|
||||
$name = substr($name, 10);
|
||||
|
||||
} else {
|
||||
parent::__call($name, $args);
|
||||
}
|
||||
|
||||
// ProductIdAndTitle -> array('product', 'title')
|
||||
$parts = explode('_and_', strtolower(preg_replace('#(.)(?=[A-Z])#', '$1_', $name)));
|
||||
|
||||
if (count($parts) !== count($args)) {
|
||||
throw new InvalidArgumentException("Magic fetch expects " . count($parts) . " parameters, but " . count($args) . " was given.");
|
||||
}
|
||||
|
||||
if (isset($single)) {
|
||||
return $this->complete($this->connection->query(
|
||||
'SELECT * FROM %n', $this->name,
|
||||
'WHERE %and', array_combine($parts, $args),
|
||||
'LIMIT 1'
|
||||
))->fetch();
|
||||
} else {
|
||||
return $this->complete($this->connection->query(
|
||||
'SELECT * FROM %n', $this->name,
|
||||
'WHERE %and', array_combine($parts, $args)
|
||||
))->fetchAll();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user