Skip to content

Instantly share code, notes, and snippets.

@clicman
Last active February 8, 2016 05:12
Show Gist options
  • Save clicman/367d053120b774ba828a to your computer and use it in GitHub Desktop.
Save clicman/367d053120b774ba828a to your computer and use it in GitHub Desktop.
This gist brings join functionality to Doctrine Mongo ODM framework.
<?php
/*
* Copyright (c) 2016, Viktor Sidochenko <viktor.sidochenko@gmail.com>
* All rights reserved.
*
* BSD License
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace Util;
use MongoId;
use ReflectionClass;
/**
* Description of MongoJoin
*
* @author sidochenko
*/
class MongoJoin {
/**
* Metod helps to join referenced objects in mongo ODM
* @param array $parentResult result set where reference started
* @param string $joinBy join field in $parentResult
* @param array $referencedDocuments array of referenced documents
* @param mixed $joinOwner document where $joinBy is primary key
* @return mixed modified $parentresult
*
* Example:
* $joinedInteractions = (new MongoJoin())->join($interactions->toArray(),
* 'accountId',
* [
* "account" => ['_id', 'ODM\Documents\Account'], //Key "account" is optional, if not set - collection name will used (see next string)
* ['accountId', 'ODM\Documents\User']
* ]
* , 'ODM\Documents\Account');
*/
public function join($parentResult, $joinBy, $referencedDocuments, $joinOwner) {
$dm = Application::getDocumentManager();
$joinValues = [];
//Parse join values from parend result
array_walk($parentResult, function (&$item) use (&$joinValues, $joinBy) {
array_push($joinValues, new MongoId($item->$joinBy));
});
//For each element of parent result
foreach ($parentResult as $parentOne) {
//For each referenced document
foreach ($referencedDocuments as $referencedAlias=>$reference) {
$referencedDocument=$reference[1];
$field = $joinBy;
$refField = $reference[0];
if ($referencedDocument === $joinOwner) {
$refField = 'id';
}
//Find documents by referenced field
$items = $dm->createQueryBuilder($referencedDocument)
->select()->field($refField)->in($joinValues)->getQuery()->execute();
$items = $items->toArray(FALSE);
//Modify parent document element
array_walk($items, function (&$item) use ($parentOne, $field, $refField, $referencedAlias) {
$className = new ReflectionClass(get_class($item));
if (is_int($referencedAlias)) {
$objToStore = strtolower($className->getShortName());
} else {
$objToStore=$referencedAlias;
}
if ($item->{$refField} === $parentOne->{$field}) {
$parentOne->{$objToStore} = $item;
}
});
}
}
return $parentResult;
}
}
@clicman
Copy link
Author

clicman commented Feb 8, 2016

This is concept only. And can have performance issues on large data.
But it can be easy improved to pass all of parent results as search criteria to referenced documents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment