Skip to content

Commit

Permalink
Merge branch 'release/v0.7.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
crodas committed Sep 25, 2016
2 parents c8fbc1d + 3e8b6b8 commit 3435a90
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 9 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
"description": "Mongo abstraction",
"require": {
"php": ">=5.4.0",
"crodas/service-provider": "^0.1",
"crodas/notoj": ">=1.0.5",
"crodas/simple-view-engine": "^0.1",
"crodas/file-util": ">=0.1.14",
"crodas/cli": "^0.1",
"crodas/validator": ">=0.2.1",
"crodas/build": "^0.2",
"keyvanakbary/slugifier": "^3.0"
},
"require-dev": {
"crodas/simple-view-engine": "^0.1"
},
"license": "BSD-4-Clause",
"version": "v0.7.1",
"version": "v0.7.2",
"authors": [
{
"name": "César D. Rodas",
Expand Down
2 changes: 1 addition & 1 deletion lib/ActiveMongo2/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class_exists("crodas\FileUtil\File"); // preload class
$code = Template\Templates::get('documents')
->render($args, true);

if (strlen($code) >= 1024*1024) {
if (strlen($code) >= 1024*1024 || !class_exists('crodas\SimpleView\FixCode')) {
File::write($config['loader'], $code);
} else {
File::write($config['loader'], FixCode::fix($code));
Expand Down
45 changes: 44 additions & 1 deletion lib/ActiveMongo2/Template/Documents.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,48 @@ public function ensureIndex($background)
@end
}

protected function compareObjects($a, $b)
{
if ($a === $b) {
return true;
}

if (is_array($a) && is_array($b)) {
$keysA = array_keys($a);
$keysB = array_keys($b);
if ($keysA !== $keysB) {
return false;
}
foreach ($keysA as $key) {
if (!$this->compareObjects($a[$key], $b[$key])) {
return false;
}
}
return true;
}

if (!is_object($a) || !is_object($b)) {
return false;
}

$class = get_class($b);
if (!($a instanceof $class)) {
return false;
}

if ($a instanceof \MongoBinData || $a instanceof \MongoId) {
return $a->__toString() === $a->__toString();
}

if ($a instanceof \MongoDate) {
return $a->sec === $b->sec && $b->usec === $b->usec;
}


return false;
}


@foreach ($collections as $collection)

/**
Expand Down Expand Up @@ -650,8 +692,9 @@ protected function update_{{sha1($collection->getClass())}}(Array &$current, Arr
} else if (!array_key_exists({{@$prop.''}}, $old)) {
$change['$set'][{{@$prop.''}}] = {{$prop->getPHPVariable('$current')}};
$has_changed = true;
} else if ({{$prop->getPHPVariable('$current')}} !== $old[{{@$prop.''}}]) {
} else if (!$this->compareObjects({{$prop->getPHPVariable('$current')}}, $old[{{@$prop.''}}])) {
$has_changed = true;

@if ($prop->getAnnotation()->has('Inc'))
if (empty($old[{{@$prop.''}}])) {
$prev = 0;
Expand Down
8 changes: 4 additions & 4 deletions lib/ActiveMongo2/Template/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public function _render(Array $vars = array(), $return = false)
echo " }\n skip_";
echo $next . ":\n";
}
echo " }\n\n";
echo " }\n\n protected function compareObjects(\$a, \$b)\n {\n if (\$a === \$b) {\n return true;\n }\n\n if (is_array(\$a) && is_array(\$b)) {\n \$keysA = array_keys(\$a);\n \$keysB = array_keys(\$b);\n if (\$keysA !== \$keysB) {\n return false;\n }\n foreach (\$keysA as \$key) {\n if (!\$this->compareObjects(\$a[\$key], \$b[\$key])) {\n return false;\n }\n }\n return true;\n }\n\n if (!is_object(\$a) || !is_object(\$b)) {\n return false;\n }\n\n \$class = get_class(\$b);\n if (!(\$a instanceof \$class)) {\n return false;\n }\n\n if (\$a instanceof \\MongoBinData || \$a instanceof \\MongoId) {\n return \$a->__toString() === \$a->__toString();\n }\n\n if (\$a instanceof \\MongoDate) {\n return \$a->sec === \$b->sec && \$b->usec === \$b->usec;\n }\n\n\n var_dump(\$a, \$b);exit;\n }\n\n\n";
foreach($collections as $collection) {

$this->context['collection'] = $collection;
Expand Down Expand Up @@ -580,10 +580,10 @@ public function _render(Array $vars = array(), $return = false)
var_export($prop.'');
echo ", \$old)) {\n \$change['\$set'][";
var_export($prop.'');
echo "] = " . ($prop->getPHPVariable('$current')) . ";\n \$has_changed = true;\n } else if (";
echo $prop->getPHPVariable('$current') . " !== \$old[";
echo "] = " . ($prop->getPHPVariable('$current')) . ";\n \$has_changed = true;\n } else if (!\$this->compareObjects(";
echo $prop->getPHPVariable('$current') . ", \$old[";
var_export($prop.'');
echo "]) {\n \$has_changed = true;\n";
echo "])) {\n \$has_changed = true;\n\n";
if ($prop->getAnnotation()->has('Inc')) {
echo " if (empty(\$old[";
var_export($prop.'');
Expand Down
31 changes: 31 additions & 0 deletions tests/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,4 +757,35 @@ public function testBinary()
$this->assertTrue($object['base64'] instanceof MongoBinData);
}

public function testReferenceUpdateBug()
{
$conn = getConnection();
$doc = new BinaryDoc;
$doc->content = file_get_contents(__FILE__);
$doc->base64 = base64_encode("cesar");
$conn->save($doc);


$ref = new ReferenceHub;
$ref->ref = $doc;

$y = $GLOBALS['x_updates'];
$conn->save($ref);

$this->assertEquals($y+1, $GLOBALS['x_updates']);

$ref->ref = BinaryDoc::findOne(['_id' => $doc->id]);
$conn->save($ref);
$this->assertEquals($y+1, $GLOBALS['x_updates']);

$doc = new BinaryDoc;
$doc->content = file_get_contents(__FILE__);
$doc->base64 = base64_encode("cesar");
$conn->save($doc);

$ref->ref = BinaryDoc::findOne(['_id' => $doc->id]);
$conn->save($ref);
$this->assertEquals($y+2, $GLOBALS['x_updates']);
}

}
2 changes: 2 additions & 0 deletions tests/docs/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
class BinaryDoc
{
use ActiveMongo2\Query;

/** @Id */
public $id;

Expand Down
21 changes: 21 additions & 0 deletions tests/docs/Reference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

$GLOBALS['x_updates'] = 0;

/** @Persist */
class ReferenceHub
{
use ActiveMongo2\Query;

/** @Id */
public $id;

/** @Reference */
public $ref;

/** @postSave */
public function didSave()
{
++$GLOBALS['x_updates'];
}
}

0 comments on commit 3435a90

Please sign in to comment.