diff --git a/src/Query/Grammars/CypherGrammar.php b/src/Query/Grammars/CypherGrammar.php index e14e81e6..55279d71 100644 --- a/src/Query/Grammars/CypherGrammar.php +++ b/src/Query/Grammars/CypherGrammar.php @@ -871,8 +871,9 @@ public function compileMatchRelationship(Builder $query, $attributes) $endKey = $attributes['end']['id']['key']; $endNode = 'rel_'.$this->modelAsNode($attributes['label']); $endLabel = $this->prepareLabels($attributes['end']['label']); + $endId = null; - if ($attributes['end']['id']['value']) { + if ($attributes['end']['id']['value'] || $attributes['end']['id']['value'] === 0) { if ($endKey === 'id') { // when it's 'id' it has to be numeric $endKey = 'id('.$endNode.')'; @@ -883,7 +884,7 @@ public function compileMatchRelationship(Builder $query, $attributes) } } - $endCondition = (!empty($endId)) ? $endKey.'='.$endId : ''; + $endCondition = (!is_null($endId)) ? $endKey.'='.$endId : ''; $query .= ", ($endNode$endLabel)"; } diff --git a/tests/Vinelab/NeoEloquent/Query/GrammarTest.php b/tests/Vinelab/NeoEloquent/Query/GrammarTest.php index 3205268c..80c5f70d 100644 --- a/tests/Vinelab/NeoEloquent/Query/GrammarTest.php +++ b/tests/Vinelab/NeoEloquent/Query/GrammarTest.php @@ -131,4 +131,40 @@ public function testReplacingIdProperty() $this->assertEquals('idcola', $this->grammar->getIdReplacement('id')); $this->assertEquals('iddd', $this->grammar->getIdReplacement('id(dd)')); } + + public function testCompileMatchRelationship() + { + $builder = M::mock('Vinelab\NeoEloquent\Query\Builder'); + $attributes = [ + 'label' => 'WROTE', + 'start' => [ + 'id' => ['key' => 'id', 'value' => 12], + 'label' => ['Author'], + ], + 'end' => [ + 'id' => ['key' => 'id', 'value' => 11], + 'label' => ['Book'], + ], + ]; + + $this->assertEquals('MATCH (author:`Author`), (rel_wrote:`Book`) WHERE id(author)=12 AND id(rel_wrote)=11', $this->grammar->compileMatchRelationship($builder, $attributes)); + } + + public function testCompileMatchRelationshipWithZeroEndId() + { + $builder = M::mock('Vinelab\NeoEloquent\Query\Builder'); + $attributes = [ + 'label' => 'WROTE', + 'start' => [ + 'id' => ['key' => 'id', 'value' => 12], + 'label' => ['Author'], + ], + 'end' => [ + 'id' => ['key' => 'id', 'value' => 0], + 'label' => ['Book'], + ], + ]; + + $this->assertEquals('MATCH (author:`Author`), (rel_wrote:`Book`) WHERE id(author)=12 AND id(rel_wrote)=0', $this->grammar->compileMatchRelationship($builder, $attributes)); + } }