Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Call callback with passing result

  • Loading branch information...
commit da3ebdf92e914dddfcdc332fd9af3ccdf6ea2c55 1 parent 1b71499
Jakub Vrana authored
View
12 NotORM.php
@@ -91,5 +91,17 @@ function __call($table, array $where) {
}
return $return;
}
+
+ /** Pass results to callback
+ * @param NotORM_Result|NotORM_Row
+ * @param ...
+ * @param callback it will get results in arguments
+ * @return null
+ */
+ static function then($result1, $callback) {
+ $results = func_get_args();
+ $callback = array_pop($results);
+ call_user_func_array($callback, $results); // don't return its result to be forward compatible with deferred calls
+ }
}
View
9 NotORM/Result.php
@@ -630,6 +630,15 @@ function fetchPairs($key, $value = '') {
return $return;
}
+ /** Pass result to callback
+ * @param callback it will get $this in argument
+ * @return null
+ */
+ function then($callback) {
+ return NotORM::then($this, $callback);
+ // don't return $this - should be at the end of fluent call
+ }
+
protected function access($key, $delete = false) {
if ($delete) {
if (is_array($this->access)) {
View
17 tests/32-then.php
@@ -0,0 +1,17 @@
+<?php
+include_once dirname(__FILE__) . "/connect.inc.php";
+
+$software->author()->order("id")->then(function ($authors) {
+ foreach ($authors as $author) {
+ echo "$author[name]\n";
+ }
+});
+echo "\n";
+
+$software->application_tag()->order("application_id, tag_id")->then(function ($application_tags) {
+ foreach ($application_tags as $application_tag) {
+ NotORM::then($application_tag->application, $application_tag->tag, function ($application, $tag) {
+ echo "$application[title]: $tag[name]\n";
+ });
+ }
+});
View
20 tests/32-then.phpt
@@ -0,0 +1,20 @@
+--TEST--
+Passing result to callback
+--SKIPIF--
+<?php
+echo (version_compare(PHP_VERSION, '5.3.0') < 0 ? "PHP 5.3+ required\n" : "");
+?>
+--FILE--
+<?php
+include dirname(__FILE__) . "/32-then.php";
+?>
+--EXPECTF--
+Jakub Vrana
+David Grudl
+
+Adminer: PHP
+Adminer: MySQL
+JUSH: JavaScript
+Nette: PHP
+Dibi: PHP
+Dibi: MySQL
Please sign in to comment.
Something went wrong with that request. Please try again.