1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 14:16:39 +02:00

- added DibiTable magic fetch

- fixed some bugs
This commit is contained in:
David Grudl
2008-10-21 13:42:53 +00:00
parent 3e04378375
commit 1e33b67e37
3 changed files with 54 additions and 4 deletions

View File

@@ -89,7 +89,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
} }
DibiDriverException::tryError(); DibiDriverException::tryError();
if (isset($config['persistent'])) { if (empty($config['persistent'])) {
$this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW); $this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW);
} else { } else {
$this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW); $this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW);

View File

@@ -304,7 +304,8 @@ class DibiResult extends DibiObject implements IDataSource
// check columns // check columns
foreach ($assoc as $as) { 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."); throw new InvalidArgumentException("Unknown column '$as' in associative descriptor.");
} }
} }
@@ -407,7 +408,7 @@ class DibiResult extends DibiObject implements IDataSource
$value = $tmp[1]; $value = $tmp[1];
} else { } else {
if (!array_key_exists($value, $row)) { if (!isset($row[$value])) {
throw new InvalidArgumentException("Unknown value column '$value'."); throw new InvalidArgumentException("Unknown value column '$value'.");
} }
@@ -418,7 +419,7 @@ class DibiResult extends DibiObject implements IDataSource
return $data; return $data;
} }
if (!array_key_exists($key, $row)) { if (!isset($row[$key])) {
throw new InvalidArgumentException("Unknown key column '$key'."); throw new InvalidArgumentException("Unknown key column '$key'.");
} }
} }

View File

@@ -358,4 +358,53 @@ abstract class DibiTable extends DibiObject
return $this->command()->__call('select', $args)->from($this->name); 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();
}
}
} }