Skip to content

Commit

Permalink
fix(Relationships): Make mapping foreign keys optional
Browse files Browse the repository at this point in the history
Before, to use a relationships `associate`, `disassociate`,
or `save` methods, you needed map the foreign keys.
This is no longer required.
  • Loading branch information
elpete committed May 3, 2019
1 parent 995706b commit 708506d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
31 changes: 27 additions & 4 deletions models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,18 @@ component accessors="true" {
}, [] );
}

function clearAttribute( name, setToNull = false ) {
function forceClearAttribute( name, setToNull = false ) {
arguments.force = true;
return clearAttribute( argumentCollection = arguments );
}

function clearAttribute( name, setToNull = false, force = false ) {
if ( force ) {
if ( ! variables._attributes.keyExists( retrieveAliasForColumn( name ) ) ) {
variables._attributes[ name ] = name;
variables._meta.properties.append( { "name" = name } );
}
}
if ( setToNull ) {
variables._data[ name ] = javacast( "null", "" );
variables[ retrieveAliasForColumn( name ) ] = javacast( "null", "" );
Expand Down Expand Up @@ -197,9 +208,21 @@ component accessors="true" {
);
}

function assignAttribute( name, value ) {
guardAgainstNonExistentAttribute( name );
guardAgainstReadOnlyAttribute( name );
function forceAssignAttribute( name, value ) {
arguments.force = true;
return assignAttribute( argumentCollection = arguments );
}

function assignAttribute( name, value, force = false ) {
if ( force ) {
if ( ! variables._attributes.keyExists( retrieveAliasForColumn( name ) ) ) {
variables._attributes[ name ] = name;
variables._meta.properties.append( { "name" = name } );
}
} else {
guardAgainstNonExistentAttribute( name );
guardAgainstReadOnlyAttribute( name );
}
if ( ! isSimpleValue( arguments.value ) ) {
if ( ! structKeyExists( arguments.value, "keyValue" ) ) {
throw(
Expand Down
7 changes: 5 additions & 2 deletions models/Relationships/BelongsTo.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,18 @@ component extends="quick.models.Relationships.BaseRelationship" {

function associate( entity ) {
var ownerKeyValue = isSimpleValue( entity ) ? entity : entity.retrieveAttribute( variables.ownerKey );
variables.child.assignAttribute( variables.foreignKey, ownerKeyValue );
variables.child.forceAssignAttribute( variables.foreignKey, ownerKeyValue );
if ( ! isSimpleValue( entity ) ) {
variables.child.assignRelationship( variables.relationMethodName, entity );
}
return variables.child;
}

function dissociate() {
variables.child.clearAttribute( variables.foreignKey, true );
variables.child.forceClearAttribute(
name = variables.foreignKey,
setToNull = true
);
return variables.child.clearRelationship( variables.relationMethodName );
}

Expand Down
4 changes: 2 additions & 2 deletions models/Relationships/HasOneOrMany.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ component extends="quick.models.Relationships.BaseRelationship" accessors="true"

function buildDictionary( results ) {
return results.reduce( function( dict, result ) {
var key = invoke( result, "get#variables.foreignKey#" );
var key = result.retrieveAttribute( variables.foreignKey );
if ( ! structKeyExists( dict, key ) ) {
dict[ key ] = [];
}
Expand Down Expand Up @@ -75,7 +75,7 @@ component extends="quick.models.Relationships.BaseRelationship" accessors="true"
}

function setForeignAttributesForCreate( entity ) {
entity.assignAttribute(
entity.forceAssignAttribute(
getForeignKeyName(),
getParentKey()
);
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/app/models/Post.cfc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
component table="my_posts" extends="quick.models.BaseEntity" accessors="true" {

property name="post_pk";
property name="userId" column="user_id";
// property name="userId" column="user_id";
property name="body";
property name="createdDate" column="created_date";
property name="modifiedDate" column="modified_date";
Expand Down
6 changes: 0 additions & 6 deletions tests/specs/integration/BaseEntity/AttributeSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
expect( user.getUsername() ).toBe( "elpete" );
} );

it( "can get foreign keys just like any other column", function() {
var post = getInstance( "Post" ).find( 1245 );
expect( post.getPost_Pk() ).toBe( 1245 );
expect( post.getUser_Id() ).toBe( 1 );
} );

it( "can set the value of an attribute using the `setColumnName` magic methods", function() {
var user = getInstance( "User" ).find( 1 );
expect( user.getUsername() ).toBe( "elpete" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
var user = getInstance( "User" ).find( 1 );
newPost = user.posts().save( newPost );
expect( newPost.isLoaded() ).toBeTrue();
expect( newPost.retrieveAttribute( "user_id" ) ).toBe( user.getId() );
expect( newPost.getAuthor().getId() ).toBe( user.getId() );
} );
} );

it( "can create new related entities directly", function() {
Expand Down

0 comments on commit 708506d

Please sign in to comment.