@@ -93,9 +103,23 @@ abstract class NObject
throw new BadMethodCallException("Call to method without name");
}
+ $class = get_class($this);
+
+ // event functionality
+ if (self::hasEvent($class, $name)) {
+ $list = $this->$name;
+ if (is_array($list) || $list instanceof Traversable) {
+ foreach ($list as $handler) {
+ if ($handler === '') continue;
+ call_user_func_array($handler, $args);
+ }
+ }
+ return;
+ }
+
// object prototypes support Class__method()
// (or use class Class__method { static function ... } with autoloading?)
- $cl = $class = get_class($this);
+ $cl = $class;
do {
if (function_exists($nm = $cl . '_prototype_' . $name)) {
array_unshift($args, $this);
@@ -199,7 +223,7 @@ abstract class NObject
/**
- * Has property accessor?
+ * Has property an accessor?
*
* @param string class name
* @param string method name
@@ -221,4 +245,26 @@ abstract class NObject
return isset($cache[$c][$m]);
}
+
+
+ /**
+ * Is property an event?
+ *
+ * @param string class name
+ * @param string method name
+ * @return bool
+ */
+ private static function hasEvent($c, $m)
+ {
+ if (strncmp($m, 'on', 2)) return FALSE;
+
+ static $cache;
+ if (!isset($cache[$c])) {
+ // get_class_vars returns ONLY PUBLIC properties
+ // but returns static methods too (nothing doing...)
+ $cache[$c] = get_class_vars($c);
+ }
+ return isset($cache[$c][$m]) && $cache[$c][$m] === '';
+ }
+
}
diff --git a/dibi/libs/DibiDriverInterface.php b/dibi/libs/interfaces.php
similarity index 87%
rename from dibi/libs/DibiDriverInterface.php
rename to dibi/libs/interfaces.php
index e419c568..a41504f4 100644
--- a/dibi/libs/DibiDriverInterface.php
+++ b/dibi/libs/interfaces.php
@@ -19,6 +19,26 @@
+/**
+ * Interface for user variable, used for generating SQL
+ * @package dibi
+ */
+interface IDibiVariable
+{
+ /**
+ * Format for SQL
+ *
+ * @param object destination IDibiDriver
+ * @param string optional modifier
+ * @return string SQL code
+ */
+ public function toSql(IDibiDriver $driver, $modifier);
+}
+
+
+
+
+
/**
* dibi driver interface
*
@@ -27,7 +47,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
-interface DibiDriverInterface
+interface IDibiDriver
{
/**
diff --git a/examples/connect.php b/examples/connect.php
index efd2a329..3b758bcc 100644
--- a/examples/connect.php
+++ b/examples/connect.php
@@ -10,6 +10,7 @@ try {
dibi::connect(array(
'driver' => 'sqlite',
'database' => 'sample.sdb',
+ 'result:objects' => TRUE, // fetch rows as objects
));
echo 'OK';
diff --git a/examples/datetime.demo.php b/examples/datetime.demo.php
index 592f4823..9adaf583 100644
--- a/examples/datetime.demo.php
+++ b/examples/datetime.demo.php
@@ -1,4 +1,4 @@
-DibiVariableInterface example
+IDibiVariable example
DibiTable demo
+
+ 'sqlite',
+ 'database' => 'sample_tmp.sdb',
+));
+
+
+
+
+// table products
+class Products extends DibiTable
+{
+// rely on autodetection...
+// protected $name = 'products';
+// protected $primary = 'product_id';
+
+}
+
+// autodetection: primary keys are customer_id, order_id, ...
+DibiTable::$primaryMask = '%s_id';
+
+
+
+// create table object
+$products = new Products();
+
+echo "Table name: $products->name\n";
+echo "Primary key: $products->primary\n";
+
+
+// Finds rows by primary key
+foreach ($products->find(1, 3) as $row) {
+ ...
+}
+
+
+// select all
+$products->findAll()->dump();
+
+
+// select all, order by title, product_id
+$products->findAll('title', $products->primary)->dump();
+
+
+// fetches single row with id 3
+$row = $products->fetch(3);
+
+
+// deletes row from a table
+$count = $products->delete(1);
+
+// deletes multiple rows
+$count = $products->delete(array(1, 2, 3));
+var_dump($count); // number of deleted rows
+
+
+// update row #2 in a table
+$data = (object) NULL;
+$data->title = 'New title';
+$count = $products->update(2, $data);
+var_dump($count); // number of updated rows
+
+
+// update multiple rows in a table
+$count = $products->update(array(3, 5), $data);
+var_dump($count); // number of updated rows
+
+
+// inserts row into a table
+$data = array();
+$data['title'] = 'New product';
+$id = $products->insert($data);
+var_dump($id); // generated id
+
+
+// is absolutely SQL injection safe
+$key = '3 OR 1=1';
+$products->delete($key);
+// --> DELETE FROM [products] WHERE [product_id] IN ( 3 )