diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index 2cdd570a..88663437 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -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); diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index caa791d4..0f57ac31 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -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'."); } } diff --git a/dibi/libs/DibiTable.php b/dibi/libs/DibiTable.php index b28e1929..34f3e0b3 100644 --- a/dibi/libs/DibiTable.php +++ b/dibi/libs/DibiTable.php @@ -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(); + } + } + }