Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed invalid match when trying to attach to a node whose id is 0 #360

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Query/Grammars/CypherGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for this fix, it's a gotcha! I don't know how we've gone so long without facing such issue 😅

Do you think we could do if (isset($attributes['end']['id']['value']) instead of the two checks? Haven't had the chance to try it yet sorry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if you have a running database it's unlikely to delete the very first node after a while. :)

I couldn't find the usecase when this is called without a value attribute, but I didn't want to break it anyhow. It's not absolutely clear how it is used, because this is already in an if (isset($attributes['end'])) check (line 870).

Also the current released check is also not completely notice free, because a $attribute['end']['id'] without a value key would throw an undefined index notice on the check.

So my best bet is, that it is unlikely that this method gets called in the way it is checked.

if ($endKey === 'id') {
// when it's 'id' it has to be numeric
$endKey = 'id('.$endNode.')';
Expand All @@ -883,7 +884,7 @@ public function compileMatchRelationship(Builder $query, $attributes)
}
}

$endCondition = (!empty($endId)) ? $endKey.'='.$endId : '';
$endCondition = (!is_null($endId)) ? $endKey.'='.$endId : '';

$query .= ", ($endNode$endLabel)";
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Vinelab/NeoEloquent/Query/GrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}