Skip to content

Commit

Permalink
Aliased properties are now dealt with correctly in queries
Browse files Browse the repository at this point in the history
Fixes issue a-musing-moose#9
  • Loading branch information
a-musing-moose committed Apr 30, 2011
1 parent 7c891ae commit 6c743dd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
26 changes: 26 additions & 0 deletions integration-tests/TestAliased.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,30 @@ public function testAliasedPropertyIsRestoredCorrectly()
$aliased->__setData($data);
$this->assertSame('Chris', $aliased->Name);
}

public function testSearchingAnAliasedProperty()
{
$aliased = new Aliased();
$aliased->Name = 'Chris';
$aliased->save();

$query = \morph\Query::instance()
->property('Name')->equals('Chris');

$found = \morph\Storage::instance()->findOneByQuery(new Aliased(), $query);
$this->assertEquals($aliased->id(), $found->id());
}

public function testSearchAnAliasedPropertyWithItsAlias()
{
$aliased = new Aliased();
$aliased->Name = 'Chris';
$aliased->save();

$query = \morph\Query::instance()
->property('n')->equals('Chris');

$found = \morph\Storage::instance()->findOneByQuery(new Aliased(), $query);
$this->assertEquals($aliased->id(), $found->id());
}
}
8 changes: 8 additions & 0 deletions src/morph/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ public function __getData()
}
return $data;
}

/**
* @return \morph\PropertSet
*/
public function __getPropertySet()
{
return $this->propertySet;
}

// ********************** //
// MAGIC ACCESS FUNCTIONS //
Expand Down
25 changes: 22 additions & 3 deletions src/morph/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function delete(Object $object)
*
* The results come packages up in a Morph_Iterator object
*
* @param Morph_Object $object Required to determine the correct collection query against
* @param \morph\Object $object Required to determine the correct collection query against
* @param Morph_IQuery $query
* @return Morph_Iterator
*/
Expand All @@ -216,7 +216,9 @@ public function findByQuery(Object $object, IQuery $query = null)
$class = get_class($object);

$query = (is_null($query)) ? new Query() : $query;
$cursor = $this->db->selectCollection($object->collection())->find($query->getRawQuery());

$rawQuery = $this->getRawQuery($object, $query);
$cursor = $this->db->selectCollection($object->collection())->find($rawQuery);

$limit = $query->getLimit();
if (!\is_null($limit)) {
Expand Down Expand Up @@ -251,9 +253,26 @@ public function findOneByQuery(Object $object, IQuery $query = null)
$class = \get_class($object);

$query = (is_null($query)) ? new Query() : $query;
$data = $this->db->selectCollection($object->collection())->findOne($query->getRawQuery());
$rawQuery = $this->getRawQuery($object, $query);
$data = $this->db->selectCollection($object->collection())->findOne($rawQuery);
return $this->setData($object, $data);
}

/**
* Ensures that aliased properties are correctly converted in query
*
* @param Object $object
* @param IQuery $query
*/
private function getRawQuery(Object $object, IQuery $query)
{
$rawQuery = array();
foreach ($query->getRawQuery() as $field => $value) {
$storageName = $object->__getPropertySet()->getStorageName($field);
$rawQuery[$storageName] = $value;
}
return $rawQuery;
}

/**
* Fetches a object representing a file in MongoDB
Expand Down

0 comments on commit 6c743dd

Please sign in to comment.