Switched to subqueries from manual fetching

This commit is contained in:
Marcin Kurczewski 2014-11-06 20:00:52 +01:00
parent a55bd7686c
commit f9528f8183

View file

@ -138,11 +138,13 @@ class PostDao extends AbstractDao implements ICrudDao
{ {
foreach ($requirement->getValue()->getValues() as $userName) foreach ($requirement->getValue()->getValues() as $userName)
{ {
$userId = $this->userDao->findByName($userName)->getId(); $sql = 'EXISTS (
$sql = 'EXISTS (SELECT 1 FROM favorites f WHERE f.postId = posts.id AND f.userId = ?)'; SELECT 1 FROM favorites f
WHERE f.postId = posts.id
AND f.userId = (SELECT id FROM users WHERE name = ?))';
if ($requirement->isNegated()) if ($requirement->isNegated())
$sql = 'NOT ' . $sql; $sql = 'NOT ' . $sql;
$query->where($sql, [$userId]); $query->where($sql, [$userName]);
} }
return; return;
} }
@ -151,11 +153,13 @@ class PostDao extends AbstractDao implements ICrudDao
{ {
foreach ($requirement->getValue()->getValues() as $userName) foreach ($requirement->getValue()->getValues() as $userName)
{ {
$userId = $this->userDao->findByName($userName)->getId(); $sql = 'EXISTS (
$sql = 'EXISTS (SELECT 1 FROM comments c WHERE c.postId = posts.id AND c.userId = ?)'; SELECT 1 FROM comments c
WHERE c.postId = posts.id
AND c.userId = (SELECT id FROM users WHERE name = ?))';
if ($requirement->isNegated()) if ($requirement->isNegated())
$sql = 'NOT ' . $sql; $sql = 'NOT ' . $sql;
$query->where($sql, [$userId]); $query->where($sql, [$userName]);
} }
return; return;
} }
@ -170,15 +174,14 @@ class PostDao extends AbstractDao implements ICrudDao
$values = $requirement->getValue()->getValues(); $values = $requirement->getValue()->getValues();
$userName = $values[0]; $userName = $values[0];
$score = $values[1]; $score = $values[1];
$userId = $this->userDao->findByName($userName)->getId();
$sql = 'EXISTS ( $sql = 'EXISTS (
SELECT 1 FROM scores SELECT 1 FROM scores
WHERE scores.postId = posts.id WHERE scores.postId = posts.id
AND scores.userId = ? AND scores.userId = (SELECT id FROM users WHERE name = ?)
AND scores.score = ?)'; AND scores.score = ?)';
if ($requirement->isNegated()) if ($requirement->isNegated())
$sql = 'NOT ' . $sql; $sql = 'NOT ' . $sql;
$query->where($sql, [$userId, $score]); $query->where($sql, [$userName, $score]);
return; return;
} }