Skip to content

Commit

Permalink
feat(Relationships): Add fetch methods to all Relationships
Browse files Browse the repository at this point in the history
Fetch methods such as `first` and `find` no work as expected
on relationships.  These are most useful on one to many relationships
like `hasMany` or `belongsToMany`.
  • Loading branch information
elpete authored May 29, 2019
1 parent f26a299 commit 61a6035
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
24 changes: 22 additions & 2 deletions models/Relationships/BaseRelationship.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,31 @@ component {
return variables.related.get();
}

function first() {
return variables.related.first();
}

function firstOrFail() {
return variables.related.firstOrFail();
}

function find( id ) {
return variables.related.find( arguments.id );
}

function findOrFail( id ) {
return variables.related.findOrFail( arguments.id );
}

function all() {
return variables.related.all();
}

/**
* get()
* @hint wrapper for getResults() on relationship types that have them, which is most of them. get() implemented for consistency with QB and Quick
*/
*/

function get() {
return getResults();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
newPost.setBody( "A new post by me!" );
var user = getInstance( "User" ).find( 1 );
newPost.setAuthor( user );
writeDump( var = newPost, top = 2, abort = true );
newPost.getAuthor();
newPost.getAuthor();
newPost.getAuthor();
newPost.getAuthor();
expect( newPost.retrieveAttribute( "user_id" ) ).toBe( user.getId() );
writeDump( var = variables.queries, top = 2 );
expect( variables.queries ).toHaveLength( 1, "Only one query should have been executed." );
} );

Expand Down
33 changes: 33 additions & 0 deletions tests/specs/integration/BaseEntity/Relationships/HasManySpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
expect( newPost.getBody() ).toBe( "A new post created directly here!" );
expect( user.fresh().getPosts() ).toHaveLength( 3 );
} );

it( "can first off of the relationship", function() {
var user = getInstance( "User" ).find( 1 );
var post = user.posts().first();
expect( post.keyValue() ).toBe( 1245 );
} );

it( "can firstOrFail off of the relationship", function() {
var user = getInstance( "User" ).find( 2 );
expect( function() {
var post = user.posts().firstOrFail( 7777 );
} ).toThrow( "EntityNotFound" );
} );

it( "can find off of the relationship", function() {
var user = getInstance( "User" ).find( 1 );
var post = user.posts().find( 523526 );
expect( post.keyValue() ).toBe( 523526 );
} );

it( "can findOrFail off of the relationship", function() {
var user = getInstance( "User" ).find( 1 );
expect( function() {
var post = user.posts().findOrFail( 7777 );
} ).toThrow( "EntityNotFound" );
} );

it( "can all off of the relationship", function() {
var user = getInstance( "User" ).find( 1 );
var posts = user.posts().all();
expect( posts ).toBeArray();
expect( posts ).toHaveLength( 3 );
} );
} );
}

Expand Down

0 comments on commit 61a6035

Please sign in to comment.