Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add new termination method: exists() #438

Merged
merged 1 commit into from

3 participants

William Durand Don't Add Me To Your Organization a.k.a The Travis Bot Francois Zaninotto
William Durand
Owner

See #427

William Durand
Owner

Ok, I tried to use EXISTS but I failed, and it seems not usable without using a subrequest. So, this way seems better because it does one fetch without hydration, etc.. Pretty fast.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 669c33e6 into f9e3c77).

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged ad903bce into f9e3c77).

Francois Zaninotto
Collaborator

In that case, you'd better do a count and return count > 0. What happens when you test EXISTS ?

William Durand
Owner

EXISTS is a clause keyword, so you have to write something like that:

SELECT * FROM table WHERE EXISTS (SELECT * FROM table WHERE title = '...')
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 754adbf into 363fce3).

Francois Zaninotto
Collaborator
William Durand
Owner
Francois Zaninotto
Collaborator

To get a real SQL EXISTS query instead of a COUNT query. There ae probably performance implications, although not much important from my point of view.

William Durand willdurand merged commit 92d2677 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 7, 2012
  1. William Durand

    Add new termination method: exists()

    willdurand authored
    Closes #427.
    Issue URL: propelorm/Propel#427.
This page is out of date. Refresh to see the latest.
9 runtime/lib/query/ModelCriteria.php
View
@@ -2247,4 +2247,13 @@ public function explain($con = null)
throw new PropelException('Unable to execute query explain plan', $e);
}
}
+
+ /**
+ * @param PropelPDO $con = null
+ * @return boolean
+ */
+ public function exists($con = null)
+ {
+ return 0 !== $this->count($con);
+ }
}
13 test/testsuite/runtime/query/ModelCriteriaTest.php
View
@@ -2480,6 +2480,19 @@ public function testOrderByWithInsensitiveCase()
$c->orderBy('TITLE');
$this->assertCriteriaTranslation($c, $sql, $params);
}
+
+ public function testExists()
+ {
+ $c = new ModelCriteria('bookstore', 'Book', 'b');
+ $this->assertTrue($c->exists());
+ }
+
+ public function testExistsWithNonExistentCondition()
+ {
+ $c = new ModelCriteria('bookstore', 'Book', 'b');
+ $c->where('b.Title = ?', 'jenexistepas');
+ $this->assertFalse($c->exists());
+ }
}
class TestableModelCriteria extends ModelCriteria
Something went wrong with that request. Please try again.