Vinelab / NeoEloquent

The Neo4j OGM for Laravel
MIT License
634 stars 200 forks source link

Composite naming of relation method may cause error in cypher grammar #132

Closed KinaneD closed 4 years ago

KinaneD commented 8 years ago

Using dynamic properties in builder causes methods like orderBy to fail.

something like:

$articles = $author->publishedArticles()->skip($offset)->take($limit)->->orderBy('updated_at')->get();

will result in this query:

MATCH (author:`Author`), (author)-[rel_approved_approvedArticles:APPROVED]->
(approvedArticles:`Article`) WHERE id(author) = {authorid} RETURN approvedArticles, 
rel_approved_approvedArticles ORDER BY approvedarticles.updated_at ASC SKIP 0 LIMIT 10

Resulting in this exception : Neoxygen\NeoClient\Exception\Neo4jException: Neo4j Exception with code "Neo.ClientError.Statement.InvalidSyntax" and message "approvedarticles not defined

The problem here is that the orderBy receives the node identifier in lower case approvedarticles.updated_at.

I believe these are the places where to start looking for the issue Vinelab\NeoEloquent\Query\Builder::matchRelation() , Vinelab/NeoEloquent/Query/Grammars/CypherGrammar::CompileComponent()

I tried to set the the parent['node'] and related['node'] to lower case

$this->matches[] = array(
            'type'         => 'Relation',
            'property'     => $property,
            'direction'    => $direction,
            'relationship' => $relationship,
            'parent' => array(
                'node'   => mb_strtolower($parentNode),
                'labels' => $parentLabels
            ),
            'related' => array(
                'node'   => mb_strtolower($relatedNode),
                'labels' => $relatedLabels
            )
        );

As result the query is transformed to something like this:

MATCH (author:`Author`), (author)-[rel_approved_approvedarticles:APPROVED]->
(approvedarticles:`Article`) WHERE id(author) = {authorid} RETURN approvedArticles, 
rel_approved_approvedarticles ORDER BY approvedarticles.updated_at ASC SKIP 0 LIMIT 10

Where the RETURN statement still uses the old presentation, i.e. approvedArticles.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.