Skip to content

Commit

Permalink
ManyMany tests now pass #24
Browse files Browse the repository at this point in the history
  • Loading branch information
TRPB committed Aug 7, 2015
1 parent c8b5772 commit 7680632
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
39 changes: 18 additions & 21 deletions maphper/maphper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function count($group = null) {
}

public function current() {
return $this->wrap($this->array[$this->iterator]);
return $this->wrap($this->createNew($this->array[$this->iterator]));
}

public function key() {
Expand Down Expand Up @@ -76,14 +76,13 @@ private function processFilters($value) {
}

public function offsetSet($offset, $value) {

$value = $this->processFilters($value);

$pk = $this->dataSource->getPrimaryKey();
if ($offset !== null) $value->{$pk[0]} = $offset;

$this->dataSource->save($value);

$value = $this->wrap($value);
}

Expand All @@ -99,7 +98,7 @@ public function offsetUnset($id) {
public function offsetGet($offset) {
if (isset($offset)) {
if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey());
return $this->wrap($this->dataSource->findById($offset));
return $this->wrap($this->createNew($this->dataSource->findById($offset)));
}
else {
$obj = $this->createNew();
Expand All @@ -108,38 +107,36 @@ public function offsetGet($offset) {
}
}

public function createNew() {
return (is_callable($this->settings['resultClass'])) ? call_user_func($this->settings['resultClass']) : new $this->settings['resultClass'];
public function createNew($data = []) {
$obj = (is_callable($this->settings['resultClass'])) ? call_user_func($this->settings['resultClass']) : new $this->settings['resultClass'];
$writeClosure = function($field, $value) { $this->$field = $value; };
$write = $writeClosure->bindTo($obj, $obj);
foreach ($data as $key => $value) $write($key, $this->dataSource->processDates($value));
return $obj;
}

private function wrap($object) {

if (is_array($object)) {
foreach ($object as &$o) $this->wrap($o);
return $object;
}
else if (is_object($object)) {
//Is it the right class?
if (get_class($object) != $this->settings['resultClass']) {
$new = $this->createNew();
$writeClosure = function($field, $value) { $this->$field = $value; };
$write = $writeClosure->bindTo($new, $new);
foreach ($object as $key => $value) $write($key, $this->dataSource->processDates($value));
}
else $new = $object;

//see if any relations need overwriting
foreach ($this->relations as $name => $relation) {
if (isset($object->$name)) {
//After overwriting the relation, does the parent object ($object) need overwriting as well?
if ($relation->overwrite($new, $new->$name)) $this[] = $new;
if ($relation->overwrite($object, $object->$name)) $this[] = $object;
}
$new->$name = $relation->getData($new);
$object->$name = $relation->getData($object);
}

$new->__maphperRelationsAttached = $this;
return $new;
// if (isset($object->debug)) return;
//$new->__maphperRelationsAttached = $this;

return $object;
}
return $object;
//return $object;
}

public function getErrors() {
Expand Down
23 changes: 17 additions & 6 deletions maphper/relation/manymany.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function getData($parentObject) {
}

public function overwrite($parentObject, &$data) {
$this->results = $data;
$this->object = $parentObject;
foreach ($data as $d) $this[] = $d;
foreach ($data as $dt) $this[] = $dt;
}

//bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship.
Expand Down Expand Up @@ -86,7 +87,9 @@ public function valid() {
public function rewind() {
$this->iterator = 0;
list ($relatedField, $valueField, $relatedMapper) = $this->getOtherFieldNameInfo();
$this->results = iterator_to_array($this->intermediateMapper->filter([$valueField => $this->object->$relatedField]), false);

$x = $this->intermediateMapper->filter([$valueField => $this->object->$relatedField]);
$this->results = iterator_to_array($x, false);
}

public function offsetExists($name) {
Expand All @@ -101,19 +104,27 @@ public function offsetGet($id) {
return $items[0]->{$this->name};
}

public function offsetSet($name, $value) {
public function offsetSet($name, $value) {
list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo();
if ($this->autoTraverse) {
$record = new \stdClass;
$record->{$this->parentField} = $value->{$this->localField};
$record->$valueField = $this->object->{$relatedField};
$this->intermediateMapper[] = $record;

}
else {
$record = $value;
$record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField};
$record->$valueField = $this->object->{$relatedField};
$this->intermediateMapper[] = $record;
if (isset($record->{$this->parentField}) && isset($value->{$this->intermediateName}) && $record->{$this->parentField} == $value->{$this->intermediateName}->{$this->localField} && $record->$valueField == $this->object->{$relatedField}) {
return;
}
else {
$record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField};
$record->$valueField = $this->object->{$relatedField};
$this->intermediateMapper[] = $record;
return true;
}

}
}

Expand Down
Binary file added test.db
Binary file not shown.
10 changes: 7 additions & 3 deletions tests/MySqlDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ public function testManyManyGet() {
$this->assertEquals($actors[1]->movies->item(1)->title, 'Movie 3');

}


private function setupMoviesActorsReal() {
$cast = new \Maphper\Maphper($this->getDataSource('cast', ['movieId', 'actorId'], ['editmode' => true]));
Expand All @@ -892,6 +893,7 @@ private function setupMoviesActorsReal() {


public function testManyManySaveIntermediate() {

$this->dropTable('actor');
$this->dropTable('movie');
$this->dropTable('cast');
Expand All @@ -909,17 +911,17 @@ public function testManyManySaveIntermediate() {
$movie->mid = 8;
$movie->title = 'Pulp Fiction';


//save the movie
$movies[] = $movie;

$role = new \stdClass;
$role->characterName = 'Jules Winnfield';
$role->movie = $movie;


$actor->roles[] = $role;


$this->assertEquals(count($cast), 1);

//Recreate mappers to clear caches
Expand All @@ -946,6 +948,7 @@ public function testManyManySaveIntermediateMultiple() {
$actor = new \stdClass;
$actor->aid = 123;
$actor->name = 'Samuel L. Jackson';
$actor->roles = [];



Expand Down Expand Up @@ -992,8 +995,9 @@ public function testManyManySaveIntermediateMultiple() {
$actor = $actors[123];

$this->assertEquals($actor->name, 'Samuel L. Jackson');

$actor->roles->rewind();

$this->assertEquals(2, count($actor->roles));
$role = $actor->roles->current();
$this->assertEquals('Jules Winnfield', $role->characterName);
$this->assertEquals('Pulp Fiction', $role->movie->title);
Expand Down

0 comments on commit 7680632

Please sign in to comment.