Skip to content

Commit

Permalink
Use separate ODM query builder for metadata expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikola committed Jan 6, 2012
1 parent 3727989 commit 735b7c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
3 changes: 2 additions & 1 deletion DocumentManager/MessageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public function __construct(DocumentManager $dm, $class, $metaClass)
public function getNbUnreadMessageByParticipant(ParticipantInterface $participant)
{
$queryBuilder = $this->repository->createQueryBuilder();
$metaQueryBuilder = $this->dm->createQueryBuilder($this->metaClass);

return $queryBuilder
->field('metadata')->elemMatch($queryBuilder->expr()
->field('metadata')->elemMatch($metaQueryBuilder->expr()
->field('participant.$id')->equals(new \MongoId($participant->getId()))
->field('isRead')->equals(false)
)
Expand Down
23 changes: 8 additions & 15 deletions DocumentManager/ThreadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ public function findThreadById($id)
*/
public function getParticipantInboxThreadsQueryBuilder(ParticipantInterface $participant)
{
$queryBuilder = $this->repository->createQueryBuilder();

return $queryBuilder
return $this->repository->createQueryBuilder()
// the participant hasn't deleted the thread, and another user wrote a message
->field('metadata')->elemMatch($this
->getNotDeletedByParticipantExpression($queryBuilder, $participant)
->getNotDeletedByParticipantExpression($participant)
->field('lastMessageDate')->notEqual(null)
)
// the thread does not contain spam or flood
Expand Down Expand Up @@ -122,12 +120,10 @@ public function findParticipantInboxThreads(ParticipantInterface $participant)
*/
public function getParticipantSentThreadsQueryBuilder(ParticipantInterface $participant)
{
$queryBuilder = $this->repository->createQueryBuilder();

return $queryBuilder
return $this->repository->createQueryBuilder()
// the participant hasn't deleted the thread, and has written a message
->field('metadata')->elemMatch($this
->getNotDeletedByParticipantExpression($queryBuilder, $participant)
->getNotDeletedByParticipantExpression($participant)
->field('lastParticipantMessageDate')->notEqual(null)
)
/* TODO: Sort by date of the last message written by this
Expand Down Expand Up @@ -167,11 +163,9 @@ public function getParticipantThreadsBySearchQueryBuilder(ParticipantInterface $
// build a regex like (term1|term2)
$regex = sprintf('/(%s)/', implode('|', explode(' ', $search)));

$queryBuilder = $this->repository->createQueryBuilder();

return $queryBuilder
return $this->repository->createQueryBuilder()
// the thread is not deleted by this participant
->field('metadata')->elemMatch($this->getNotDeletedByParticipantExpression($queryBuilder, $participant))
->field('metadata')->elemMatch($this->getNotDeletedByParticipantExpression($participant))
// TODO: this search is not anchored and uses no indexes
->field('keywords')->equals(new \MongoRegex($regex))
/* TODO: Sort by date of the last message written by another
Expand Down Expand Up @@ -286,13 +280,12 @@ protected function createThreadMetadata()
* Creates an expression to match ThreadMetadata where the participant has
* not deleted the thread.
*
* @param Builder $queryBuilder
* @param ParticipantInterface $participant
* @return Doctrine\ODM\MongoDB\Query\Expr
*/
protected function getNotDeletedByParticipantExpression(Builder $queryBuilder, ParticipantInterface $participant)
protected function getNotDeletedByParticipantExpression(ParticipantInterface $participant)
{
return $queryBuilder->expr()
return $this->dm->createQueryBuilder($this->metaClass)->expr()
->field('participant.$id')->equals(new \MongoId($participant->getId()))
->field('isDeleted')->equals(false);
}
Expand Down

0 comments on commit 735b7c1

Please sign in to comment.