Skip to content

Commit

Permalink
merged correctly, i hope
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrandi committed Jun 2, 2011
2 parents 5a2b528 + 7713f90 commit 491fe27
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 57 deletions.
37 changes: 37 additions & 0 deletions integration-tests/TestComposeMany.php
Expand Up @@ -55,5 +55,42 @@ public function testStoresParentAndChild()

$this->assertDocumentPropertyEquals($expected, 'ComposeManyParent', 'Children', $parent->id());
}

public function testStoresAddedChild()
{
$parent = new ComposeManyParent();
$parent->Name = 'Compose Many Parent';

$child1 = new Child();
$child1->Name = 'Child1';
$parent->Children[] = $child1;

$parent->save();
$this->assertCollectionExists('ComposeManyParent');
$this->assertCollectionDoesNotExist('Child');

$this->assertDocumentExists('ComposeManyParent', $parent->id());

$expected = array (
array('_ns'=>'Child', 'Name'=>'Child1', 'Age' => null),
);

$this->assertDocumentPropertyEquals($expected, 'ComposeManyParent', 'Children', $parent->id());


$child2 = new Child();
$child2->Name = 'Child2';
$parent->Children[] = $child2;

$parent->save();

$expected = array (
array('_ns'=>'Child', 'Name'=>'Child1', 'Age' => null),
array('_ns'=>'Child', 'Name'=>'Child2', 'Age' => null)
);

$this->assertDocumentPropertyEquals($expected, 'ComposeManyParent', 'Children', $parent->id());
}


}
26 changes: 26 additions & 0 deletions integration-tests/TestHasMany.php
Expand Up @@ -52,5 +52,31 @@ public function testStoresParentAndChildren()
$this->assertDocumentExists('Child', $child2->id());

}

public function testStoresAddChild()
{
$parent = new HasManyParent();
$parent->Name = 'Has Many Parent';

$child1 = new Child();
$child1->Name = 'Child1';

$parent->Children[] = $child1;

$parent->save();
$this->assertCollectionExists('HasManyParent');
$this->assertCollectionExists('Child');

$this->assertDocumentExists('HasManyParent', $parent->id());
$this->assertDocumentExists('Child', $child1->id());

$child2 = new Child();
$child2->Name = 'Child2';

$parent->Children[] = $child2;
$parent->save();
$this->assertDocumentExists('Child', $child2->id());

}

}
Expand Up @@ -84,7 +84,7 @@ protected function customFailureDescription($other, $description, $not)
$this->property,
$other,
$this->toString(),
\print_r($this->found)
\print_r($this->found, true)
);
}
}
1 change: 1 addition & 0 deletions make_phar.php
Expand Up @@ -28,3 +28,4 @@
$phar->setMetadata($metadata);
$phar->compressFiles(Phar::GZ);
echo "All done!\n";

2 changes: 2 additions & 0 deletions src/bootstrap.php
Expand Up @@ -46,6 +46,8 @@ class MorphAutoloader
'morph\\property\\Integer32' => 'phar://Morph/property/Integer32.php',
'morph\\property\\Integer64' => 'phar://Morph/property/Integer64.php',
'morph\\property\\Regex' => 'phar://Morph/property/Regex.php',
'morph\\property\\Complex' => 'phar://Morph/property/Complex.php',
'morph\\property\\StatefulCollection'=> 'phar://Morph/property/StatefulCollection.php',
'morph\\query\\Property' => 'phar://Morph/query/Property.php',
'morph\\format\\Collection' => 'phar://Morph/format/Collection.php',
'morph\\exception\\ObjectNotFound' => 'phar://Morph/exception/ObjectNotFound.php',
Expand Down
39 changes: 18 additions & 21 deletions src/morph/Object.php
Expand Up @@ -24,14 +24,6 @@ class Object
*/
protected $id;

/**
* The current state of this object
*
* one of Morph_Enum::STATE_*
* @var string
*/
protected $state;

/**
* The data associated with this object
* @var \morph\PropertySet
Expand All @@ -50,7 +42,6 @@ class Object
*/
public function __construct($id = null)
{
$this->state = Enum::STATE_NEW;
$this->id = $id;
$this->propertySet = new PropertySet();
$this->validators = array();
Expand Down Expand Up @@ -95,7 +86,7 @@ public function id()
*/
public function state()
{
return $this->state;
return $this->propertySet->getState();
}

/**
Expand Down Expand Up @@ -131,9 +122,8 @@ public function __setData(array $data, $state = Enum::STATE_DIRTY)
unset($data['_ns']);
}
foreach ($data as $propertyName => $value) {
$this->propertySet->__setRawPropertyValue($propertyName, $value);
$this->propertySet->__setRawPropertyValue($propertyName, $value, $state);
}
$this->state = $state;
return $this;
}

Expand Down Expand Up @@ -194,9 +184,6 @@ public function __set($propertyName, $propertyValue)
{
if (\array_key_exists($propertyName, $this->propertySet)) {
$this->propertySet[$propertyName]->setValue($propertyValue);
if ($this->state == Enum::STATE_CLEAN) {
$this->state = Enum::STATE_DIRTY;
}
}else{
$this->addProperty(new \morph\property\Generic($propertyName, $propertyValue));
\trigger_error("The property $propertyName was not found in object of class " . \get_class($this) . ' but I have added it as a generic property type', E_USER_WARNING);
Expand Down Expand Up @@ -230,18 +217,17 @@ public function loadById($id)
{
return Storage::instance()->fetchById($this, $id);
}

/**
* Attempts to load the current object with data from the document id specified
* This is meant for if the _id is a ObjectId
*
* @param mixed $id
* @return Morph_Object
*/
public function loadByObjectId($id)
{
return Storage::instance()->fetchByObjectId($this, $id);
}
public function loadByObjectId($id)
{
return Storage::instance()->fetchByObjectId($this, $id);
}

/**
* Fetch multiple objects by their ids
Expand All @@ -253,6 +239,17 @@ public function findByIds(array $ids)
{
return Storage::instance()->fetchByIds($this, $ids);
}

/**
* Fetch multiple objects by their ids
*
* @param array $ids
* @return Morph_Iterator
*/
public function findByObjectIds(array $ids)
{
return Storage::instance()->fetchByObjectIds($this, $ids);
}

/**
* Find objects by query
Expand Down
19 changes: 17 additions & 2 deletions src/morph/PropertySet.php
Expand Up @@ -69,13 +69,28 @@ public function __getRawPropertyValue($name)
* @param $name
* @param $value
*/
public function __setRawPropertyValue($name, $value)
public function __setRawPropertyValue($name, $value, $state = null)
{
$name = $this->reverseStorageName($name);
if(!isset($this[$name])) {
$this[$name] = new \morph\property\Generic($name);
}
$this[$name]->__setRawValue($value);
$this[$name]->__setRawValue($value, $state);
}

public function getState()
{
$state = \morph\Enum::STATE_NEW;
foreach ($this as $n => $property) {
$propertyState = $property->getState();
if ( \morph\Enum::STATE_DIRTY == $propertyState) {
$state = $propertyState;
break;
} elseif ( \morph\Enum::STATE_CLEAN == $propertyState) {
$state = $propertyState;
}
}
return $state;
}

/**
Expand Down
60 changes: 43 additions & 17 deletions src/morph/Storage.php
Expand Up @@ -94,7 +94,10 @@ public function getDatabase()
/**
* Retrieves the contents of the specified $id
* and assigns them into $object
* This shoudld be used if the _id is not necessarily a ObjectId() (or the php version, MongoId() )
*
* This shoudld be used if the _id is not necessarily a ObjectId()
* (or the php version, MongoId() ). You can always just wrap the id string
* in new \MongoId() as well
*
* @param Morph\\Object $object
* @param mixed $id
Expand All @@ -107,23 +110,23 @@ public function fetchById(Object $object, $id)
return $this->setData($object, $data);
}

/**
* Retrieves the contents of the specified $id
* and assigns them into $object
* This function is meant for if the _id is a ObjectId (the default)
*
* @param Morph\\Object $object
* @param mixed $id
* @return Morph\\Object
*/
public function fetchByObjectId(Object $object, $id) {

$objId = new \MongoId($id);

$query = array('_id' => $objId);
/**
* Retrieves the contents of the specified $id
* and assigns them into $object
* This function is meant for if the _id is a ObjectId (the default)
*
* @param Morph\\Object $object
* @param mixed $id
* @return Morph\\Object
*/
public function fetchByObjectId(Object $object, $id)
{
if (is_string($id)) {
$id = new \MongoId($id);
}
$query = array('_id' => $id);
$data = $this->db->selectCollection($object->collection())->findOne($query);
return $this->setData($object, $data);

}

/**
Expand All @@ -139,6 +142,29 @@ public function fetchByIds(Object $object, array $ids)
$query->property('_id')->in($ids);
return $this->findByQuery($object, $query);
}

/**
* Returns all objects with an _id in $ids
*
* @param Morph\\Object $object
* @param array $Ids
* @return Morph\\Iterator
*/
public function fetchObjectByIds(Object $object, array $ids)
{
$query = new Query();
$wrappedIds = array();
foreach ($ids as $id) {
if (is_string($id)) {
$wrappedIds[] = new \MongoId($id);
} else {
$wrappedIds[] = $id;
}
}

$query->property('_id')->in($ids);
return $this->findByQuery($object, $query);
}

/**
* Retrieves the contents of the specified $dbRef
Expand Down Expand Up @@ -340,4 +366,4 @@ private function setData(Object $object, $data)
$object->__setData($data, Enum::STATE_CLEAN);
return $object;
}
}
}
34 changes: 34 additions & 0 deletions src/morph/property/Complex.php
@@ -0,0 +1,34 @@
<?php
/**
* @package Morph
* @subpackage Property
* @author Jonathan Moss <xirisr@gmail.com>
* @copyright Jonathan Moss 2009
*/
namespace morph\property;
/**
* Class to represent a property within a table
*
* See Morph_Property_* for more specific implementations
*
* If you wish to create a more specialised property type you can extend
* this class. Overloading getValue() and setValue() allow you to
* provide automatic marshalling of data.
*
* @package Morph
* @subpackage Property
*/
class Complex extends Generic
{
/**
* Called when the property changes
*
* @return void
*/
public function _onChange()
{
if ($this->state == \morph\Enum::STATE_CLEAN) {
$this->state = \morph\Enum::STATE_DIRTY;
}
}
}

0 comments on commit 491fe27

Please sign in to comment.